8 Useful Code Snippets for Customizing WordPress

8 Useful Code Snippets for Customizing WordPress

WordPress is a highly flexible platform that allows users to customize the UI, site structure and code. By using code snippets, it is possible to add features that are not available in the theme and even replace some paid features. In this article, we will shareeightVery useful code snippets to help you customize every aspect of your WordPress site.

How to Add Code Snippets in WordPress

It is important to understand how to add code snippets to WordPress. Code can be added to the theme's functions.php file, or use a specialized plugin to manage code snippets, such as the Code Snippets Plug-ins.

Using the Code Snippets plugin

8 Useful Code Snippets for Customizing WordPress
  1. Install and activate the Code Snippets plugin.
  2. In the WordPress dashboard, go to "Snippets">"Add New".
  3. Paste the code into the code box and name the snippet.
  4. Select appropriate run conditions (e.g., front-end only).
  5. Save and activate the code snippet.

This approach is more efficient than directly editing functions.php The file is more secure because it avoids the risk of overwriting when the theme is updated.

1. Disable the WordPress admin bar

Sometimes it may be desirable to disable the WordPress admin bar, especially for specific user roles. Below is the relevant code snippet:

Disable the admin bar for all users

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

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 'admin_bar').
  // 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' );

draw attention to sth.: Make sure to save the code type as PHP Snippet (in some cases).

PHP code snippet

2. Allow contributors to upload images

By default, WordPress does not allow contributor accounts to upload images. The following code snippet provides permission for contributor accounts to upload images:

function grant_upload_permission_to_contributors() {
  $contributor_role = get_role('contributor');
  if ( ! $contributor_role->has_cap('upload_files') ) {
    $contributor_role->add_cap('upload_files');
  }
}
add_action('admin_init', 'grant_upload_permission_to_contributors');

3. Disable automatic e-mail updates

Whenever WordPress automatically updates plugins and themes, you may receive a large number of notification emails. The following code snippet disables these email notifications:

// 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 plugins.

// Disable auto-update emails for themes
add_filter('auto_theme_update_send_email', '__return_false'); // Disable auto-update emails for themes.
8 Useful Code Snippets for Customizing WordPress

4. Update"How are you?"Management column messages

If one feels that the default "Howdy"The greeting is inappropriate and can be replaced with a customized greeting:

function wpcode_snippet_replace_howdy( $wp_admin_bar ) {
    $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);
8 Useful Code Snippets for Customizing WordPress

5. Disabling the search function

If your site doesn't need the search feature, you can disable it completely using the following code snippet:

function fb_filter_query( $query, $error = true ) {
  if ( is_search() ) {
    $query->is_search = false;
    $query->query_vars[s] = false;
    $query->query[s] = false;
    if ( $error == true )
      $query->is_404 = true;
  }
}
add_action('parse_query', 'fb_filter_query');
add_filter('get_search_form', '__return_null');

6. Disable automatic garbage emptying

8 Useful Code Snippets for Customizing WordPress

WordPress automatically removes content that is more than 30 days old from your trash folder. The following code disables this feature:

add_action('init', function() {
  // Disable scheduled post deletion
  remove_action('wp_scheduled_delete', 'wp_scheduled_delete');
});

7. Allow SVG uploads

The SVG format is perfect for logo files, but by default WordPress does not allow SVG uploads. the following code snippet enables SVG uploads:

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');

8. Disable automatic updates

Automatic updates can sometimes cause compatibility issues. The following code snippet disables automatic updates for WordPress core, plugins and themes:

// 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');
8 Useful Code Snippets for Customizing WordPress

reach a verdict

Using code snippets can help WordPress users customize a website exactly to their needs without the need for in-depth coding knowledge. The eight code snippets provided in this article cover everything from feature enhancements to user experience optimization. By carefully integrating these code snippets, you can improve the overall performance and usability of your website.


Contact Us
Can't read the article? Contact us for a free answer! Free help for personal, small business sites!
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
Posted by photon fluctuations, retweeted with attribution:https://www.361sale.com/en/14769/

Like (0)
Previous July 26, 2024 4:18 pm
Next July 26, 2024 5:13 pm

Recommended

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

020-2206-9892

QQ咨询:1025174874

E-mail: info@361sale.com

Working hours: Monday to Friday, 9:30-18:30, holidays off

Customer Service
In order to facilitate global user registration and login, we have canceled the telephone login function. If you encounter login problems, please contact our customer service for assistance in binding your email address.