How to Bulk Remove Featured Images in WordPress

In WordPress, you can only remove post featured image one by one by editing each post and clicking on the “Remove Featured Image” link, which  is easier to do if you have only few posts but if you have hundreds of posts then removing them one by one can be a daunting task.  But fear not, there is a simpler and easier way to bulk remove  featured image in WordPress.

Copy the following code and paste it in your theme’s functions.php file.

global $wpdb;
$wpdb->query( "
    DELETE FROM $wpdb->postmeta 
    WHERE meta_key = '_thumbnail_id'
" );

Note: This code will only remove the featured image from the posts, It will not delete the images from your Media Library or from your uploads folder.

Copy the code and open your site’s Dashboard  and it will immediately remove all the featured image.  After that, please go back to your theme’s functions.php and remove the code from the file or else it will keep removing the featured images afterwards.

Hope this article helped you.

How to Show Default Image if Featured Image is not Added

The “the_post_thumbnail()” function for featured image in WordPress does not shows anything if a featured image is not attached to the post. In case you want to show a default image in your theme if a user has not attached any featured image then use following script:

     <a href="<?php the_permalink(); ?>"> // adds a hyperlink to the image
        <?php
        if ( has_post_thumbnail() ) { //check if featured image is added or not
            the_post_thumbnail( 'medium', array( 'class' => 'img-responsive' )  );  //echos featured image if added
        } 
        else {
        ?>
        <img src="<?php echo get_template_directory_uri(); ?>/images/defaultimg.png" class="img-responsive" alt="">  // outputs default image if not . change the url to match your image.                              
        <?php } ?>
    </a>                            

How to Hide Admin Bar for Logged in Users

If you have a WordPress site where end users have to login and you do not want to show the Admin bar to those users then you can disable the Admin bar globally for all users by adding following line of code into your theme’s functions.php file.

//Disable WordPress Admin Bar for all users. */
show_admin_bar(false);

If you want to disable the Admin Bar for end user but still want to show it for Administrator then use following code:

//Disable WordPress Admin Bar for all users except admins */
function wphelp_remove_adminbar() {
if (!current_user_can('administrator') && !is_admin()) {
  show_admin_bar(false);
}
}
add_action('after_setup_theme', 'wphelp_remove_adminbar');

How to add DNS Prefetch in a WordPress Theme without using Plugin

Go to Google, search for any keyword, hold two seconds and then click the first link, that site will open instantly on your browser without showing you any loading progress.  That is called DNS Preftech.

Google defines it as:

DNS prefetching is an attempt to resolve domain names before a user tries to follow a link. This is done using the computer’s normal DNS resolution mechanism; no connection to Google is used.

So, basically what is happening is that your browser downloads and saves the URL in its buffer and as soon as you click that link it shows you the page from its buffer rather than pulling it from the server. As a developer you cannot know which link your user might click but on certain pages you can anticipate which link a user might click.

For an example, on a blog archive page – a user might click the first news whereas on the single post/page a user might click the home link or the logo to visit the homepage on your site. So, you can add DNS Prefetching for these links based on what you think your user might click next.  You can use the following code snippet and and paste it your theme’s functions.php to enable DNS Prefetching in single post .

if( ! function_exists( 'wphelp_dns_prefetch' ) ) :

	function wphelp_dns_prefetch(){
		
		if ( is_singular() ) { 
                  echo '<link rel="prefetch" href="' .esc_url( home_url() ) . '">';				
                   echo '<link rel="prerender" href="' .esc_url( home_url() ) . '">';				
		}

	}

endif;

add_action('wp_head', 'wphelp_dns_prefetch');

This snippet will enable DNS Prefetching on single post, so when a user is on single post or page or any custom post type and clicks on the link for homepage then the homepage will open without any delay.

Note: DNS Prefetching only works with HTTP2. So if your server does not supports HTTP2 then this will not work.

How to Remove WordPress Dashboard Widgets

Working on a recent project, a client asked me to clean up all the widgets that shown on default WordPress Dashboard as soon as you login. They were not using any of those widgets so wanted to see a clean dashboard upon logging in.

Removing default Dashboard Widgets is easy by adding few lines of codes into your theme’s functions.php file.

Copy the following code and paste it in your functions.php.

function remove_dashboard_meta() {
        remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
        remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
        remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');//since 3.8
}
add_action( 'admin_init', 'remove_dashboard_meta' );

Source

How to display all custom fields (name AND value) in a post?

You are working on a custom WordPress theme that uses lots and lots of custom fields and you suddenly want to see what all custom fields are assigned to any post.  You can do by going into the dashboard of the site and editing the specific post but what if you want to show it on the front-end of site on the single post template?

What if you have tons of custom fields and just want to show them all on the single post without specifying specific custom field name/key?

In that case you can use the snippet below inside the post loop to show all the custom fields that are attached to a post:

<?php
$custom_fields = get_post_custom();

foreach ( $custom_fields as $field_key => $field_values ) {
	foreach ( $field_values as $key => $value )
		echo $field_key . ' - ' . $value . '<br />';
}
?>

Source