Useful Code Snippets for WordPress

How to disable admin toolbar?

//Disable WordPress admin toolbar for  all users who are currently logged in
add_filter('show_admin_bar', '__return_false');

How to show post thumbnails in RSS Feed in the beginning?

//It will add the featured image to WordPress RSS feed content in the beginning
add_filter('the_content', 'toobs_featured_image_in_rss_feed');
function toobs_featured_image_in_rss_feed( $feed_content ) {
  global $post;
  if( is_feed() ) {
    if ( has_post_thumbnail( $post->ID ) ){
      $prepend_featured_image = '
‘ . get_the_post_thumbnail( $post->ID, ‘medium’, array( ‘style’ => ‘margin-bottom: 15px;’ ) ) . ‘
';
      $feed_content = $prepend_featured_image . $feed_content;
    }
  }
  return $feed_content;
}

How to change read more text for excerpts?

// Changing excerpt more
function toobs_change_excerpt_more_text( $more ){
  global $post;
  return '… '.'Read More »'.'';
}
add_filter('excerpt_more', 'toobs_change_excerpt_more_text');

How to add another admin user in WordPress?

//Create another admin user
function toobs_create_admin_user(){
  $username = 'yourusername';
  $password = '12345678';
  $email = 'yourname@something.com';
  if ( !username_exists( $username ) && !email_exists( $email ) ) {
    $userid = wp_create_user( $username, $password, $email );
    $user = new WP_User( $userid );
    $user->set_role( 'administrator' );
  }
}
add_action('init', 'toobs_create_admin_user');

How to enable shortcodes in text widgets?

add_filter('widget_text', 'do_shortcode');

How to add a custom dashboard logo to the top left corner?

//Adds a custom logo to the top left corner of the WordPress admin
function toobs_custom_logo_wp_dashboard() {

  $style= "
    #wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
      background-image: url('" . get_bloginfo('stylesheet_directory') . "https://cdn.smartwp.com/admin-icon.png');
      background-size: contain;
      background-position: 0 0;
      color:rgba(0, 0, 0, 0);
    }
    #wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon {
      background-position: 0 0;
    }
    ";
   echo $style;
   //Do not forget to enclose it in html style tag
   
}
add_action('wp_before_admin_bar_render', 'toobs_custom_logo_wp_dashboard');

How to allow SVG upload in media library?

//Enable SVG upload
function toobs_enable_svg_upload( $mimes ) {
  //Should be done only by admins
  if ( !current_user_can( 'administrator' ) ) {
    return $mimes;
  }
  $mimes['svg']  = 'image/svg+xml';
  $mimes['svgz'] = 'image/svg+xml';
  
  return $mimes;
}
add_filter('upload_mimes', 'toobs_enable_svg_upload');

How to disable XML-RPC in WordPress for security resaons?

//Disable XML-RPC
add_filter('xmlrpc_enabled', '__return_false');

How to remove jQuery migrate to make WordPress page loading time faster?

function toobs_remove_jquery_migrate( $scripts ) {
  if ( !is_admin() && !empty( $scripts->registered['jquery'] ) ) {
    $scripts->registered['jquery']->deps = array_diff( $scripts->registered['jquery']->deps, ['jquery-migrate'] );
  }
}
add_action('wp_default_scripts', 'toobs_remove_jquery_migrate');

useful code snippets for wordrress
disable admin toolbar
show post thumbnails in rss feed
change read more text for excerpts
add admin user