Placeholder canvas

8 Useful Codes for Code Snippets to Customize WordPress 

When it comes to WordPress, customization can be done in every aspect of the platform, and that’s its USP. WordPress gives its users the freedom to customize the UI, site structure, and codes, especially when it comes to codes.

Codes can be used to customize features that are not available in the theme, or maybe they are paid features, but if you wish to use that feature, a code snippet is a great way.

In this article, I’ll be sharing some of the most useful codes for code snippets.

So dive right in!

How to Add Codes in the Code Snippet and Update functions.php file to Avoid Overwriting Theme

There are multiple ways to add codes in code snippets; most of them are simple, and we have already written a simple guide to installing and adding code.

Code Snippets Ultimate Guide: What, How, and Why to Use Code Snippets in WordPress

Furthermore, your functions.php file is highly customized to your theme. If you decide to change themes later and have functions that are not theme-specific, you will have to start over. 

Codes for Code Snippets

In addition, if you make modifications to your parent theme and then update, your changes will be overridden. Make sure you’re using the child theme.

And to do that, in this video, you will learn faster:

Now that’s dealt with, let’s explore useful codes for code snippets.

 8 Useful Codes for Code Snippets

  1. Disable WordPress Admin Bar

Do you ever wish to disable your admin bar for screenshot or want to disable specific user roles? (e.g., editors, subscribers). In that case, this tip is very useful.

Here is the code (for all users):

// Disable the WordPress admin bar for all logged-in users
add_filter('show_admin_bar', '__return_false');

If you only want to disable the admin bar for specific user roles:

function my_disable_admin_bar($show_admin_bar) {
  // Get the current user object
  $current_user = wp_get_current_user();
  // Disable admin bar for specific roles (replace 'editor' and 'subscriber' with desired roles)
  if ( in_array( 'editor', $current_user->roles ) || in_array( 'subscriber', $current_user->roles ) ) {
    return false;
  }
  return $show_admin_bar;
}
add_filter( 'show_admin_bar', 'my_disable_admin_bar' );

Here is a before and after screenshot:

Before Disable WordPress Admin Bar
After Disable WordPress Admin Bar

Tip: Make sure you save the code type as PHP Snippet (In some cases).

PHP Snippet
  1. Allow Contributors to Upload Images

By default, WordPress does not allow contributor accounts to upload images. You can, of course, upgrade that account to Author or Editor, which will provide them the power to upload and change photographs, as well as extra permissions, such as the ability to publish their own articles (as opposed to submitting for review).

This code snippet enables contributor accounts to contribute photographs to their articles without providing any additional privileges or rights. 

Paste it into the functions.php or Code snippet file of your theme:

function grant_upload_permission_to_contributors() {

  $contributor_role = get_role( 'contributor' );

  // Check if the contributor role has the 'upload_files' capability

  if ( ! $contributor_role->has_cap( 'upload_files' ) ) {

    $contributor_role->add_cap( 'upload_files' );

  }

}

add_action( 'admin_init', 'grant_upload_permission_to_contributors' );
  1. Disable AutoUpdate Emails

Are you tired of getting an update email whenever WordPress automatically updates WordPress plugins and themes? If so, then you can use this code to get rid of it.

Here is a code:

// Disable auto-update emails for WordPress core

add_filter( 'auto_core_update_send_email', '__return_false' );

// Disable auto-update emails for plugins

add_filter( 'auto_plugin_update_send_email', '__return_false' );

// Disable auto-update emails for themes

add_filter( 'auto_theme_update_send_email', '__return_false' );
Disable Auto Update Emails
  1. Update ‘Howdy’ Admin Bar Message

When you connect into your WordPress dashboard, your display name will show up in the top right hub of the screen, followed by the words “Howdy.”

You could find this greeting a little awkward, out of date, or even annoying. It is just a form of greeting which some of you will find nice and welcoming. Anyway, you can replace it with a custom greeting like ‘This is Awesome.’ This is how you can add it: 

function wpcode_snippet_replace_howdy( $wp_admin_bar ) {

    //To change the text that appears in the admin bar instead of "Howdy," edit the line below..

    $new_howdy = 'This is Awesome,';

    $my_account = $wp_admin_bar->get_node( 'my-account' );

    $wp_admin_bar->add_node(

        array(

            'id'    => 'my-account',

            'title' => str_replace( 'Howdy,', $new_howdy, $my_account->title ),

        )

    );

}

add_filter( 'admin_bar_menu', 'wpcode_snippet_replace_howdy', 25 );
Howdy Admin Bar Message Example
  1. Disable the Search Function

If you don’t require the search tool on your WordPress site, you may disable it completely using this code snippet. 

The custom function disables the search functionality, including the search line in your sidebar and menu. 

This is useful if you have a low-spec server and your website does not require searching. To do so, add the code to the functions.php file.

function fb_filter_query( $query, $error = true ) {

if ( is_search() ) {

$query->is_search = false;

$query->query_vars[s] = false;

$query->query[s] = false;

// to error

if ( $error == true )

$query->is_404 = true;

}

}

add_action( 'parse_query', 'fb_filter_query' );

add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
  1. Disable Automatic Trash Emptying

WordPress deletes any content that has been in the trash for more than 30 days, including posts, pages, and media files.

However, some users may prefer not to clear their trash automatically, allowing them to restore deleted data at any moment.

add_action( 'init', function() {

  // Disable scheduled post deletion

  remove_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );

} );
Posts
  1. Allow SVG Uploads

The SVG format is increasingly popular, particularly for logo files. Of course, you may enable this capability using a plugin or by using this code snippet.

WordPress does not allow SVG uploads by default for security concerns, but our code snippet allows site administrators to do so.

function enable_svg_upload_for_admins( $mime_types ) {

  // Allow SVG uploads only for administrators

  if ( current_user_can( 'administrator' ) ) {

    $mime_types['svg'] = 'image/svg+xml';

    $mime_types['svgz'] = 'image/svg+xml';

  }

  return $mime_types;

}

add_filter( 'upload_mimes', 'enable_svg_upload_for_admins' );
  1. Disable Automatic Updates

WordPress’s core software, plugins, and themes are automatically updated to prevent security concerns, malware infections, website breaches, and data theft.

However, in rare cases, automatic updates can cause compatibility difficulties or even crash your website.

In that scenario, use the following code snippet to prevent automatic updates:

// Disable auto-updates for WordPress core, plugins, and themes

function disable_all_auto_updates() {

  add_filter( 'auto_update_core', '__return_false' );

  add_filter( 'auto_update_plugin', '__return_false' );

  add_filter( 'auto_update_theme', '__return_false' );

}

add_action( 'admin_init', 'disable_all_auto_updates' );

Final Say

Using code snippets can empower WordPress users to tailor their websites precisely to their needs without extensive coding knowledge. 

The eight provided codes offer practical solutions for customizing various aspects of a WordPress site, from design elements to functionality enhancements. By integrating these snippets thoughtfully, website owners can elevate user experience and streamline site management. 

Embracing code snippets is a testament to the flexibility and adaptability of the WordPress platform, allowing users to create unique and impactful online presences.

FAQs on Codes for Code Snippets

Will these code snippets work with all WordPress themes?

While most code snippets should work with different WordPress themes, compatibility can vary depending on the theme structure and development methods. It is recommended that you test snippets on a staging site to ensure they work seamlessly with your specific theme.

Can I change these code snippets to meet my specific needs?

Yes, you can adapt the offered code snippets to meet your individual requirements by adjusting settings or adding new functionality. However, in order to customize effectively, you must first master the fundamentals of PHP and WordPress code.

Are these code snippets optimized for performance?

The performance impact of code snippets is determined by aspects like complexity and efficient implementation. While short snippets usually have modest performance impacts, it’s a good idea to examine your site’s performance after making any big modifications.

Are the code snippets safe to use?

When obtained from credible sources and checked for security best practices, the code snippets supplied are generally safe. To prevent any security threats, use caution and update WordPress core, themes, and plugins on a regular basis.

Want faster WordPress?

WordPress Speed Optimization

Try our AWS powered WordPress hosting for free and see the difference for yourself.

No Credit Card Required.

Whitelabel Web Hosting Portal Demo

Launching WordPress on AWS takes just one minute with Nestify.

Launching WooCommerce on AWS takes just one minute with Nestify.