Category: Code Snippets

How to echo field name as CSS class in Advanced Custom Fields (ACF) ?

Advanced Custom Field (ACF) is a free WordPress plugin to create custom field meta box in WordPress without having to code anything. If you have used ACF, you know how easy it is to use and create custom meta boxes in WordPress. If you want to echo an ACF field value then you use use following code: [php] <?php the_field(‘field_name’); ?> [/php] If you want to get the value of […]

How to make colored admin table by Post Status in WordPress ?

Sometime the default WordPress Admin panel can become a bit boring. If you want to jazz it up and want to show custom background color for each post status then following snippet of code can help. Just paste it in your theme’s functions.php and change the color accordingly [php] function whe_posts_status_color(){ ?> <style> .status-draft{background: #FCE3F2 !important;} .status-pending{background: #87C5D6 !important;} .status-publish{/* default color works best here */} .status-future{background: #C6EBF5 !important;} .status-private{background:#F2D46F;} […]

How to Add Custom Body Classes in WordPress

The “body_class()”  in WordPress adds classes to the body tag of all the pages in your WordPress site. By default a WordPress theme should use the function “body_class()” in their theme files so that WordPress can automatically output correct classes to each page/post and archive. The following example shows how to implement the body_class template tag into a theme. [php] <body <?php body_class(); ?>> [/php] The actual HTML output might […]

How to Fetch and Show Posts from a RSS Feed in WordPress

RSS (Rich Site Summary) is a format for delivering regularly changing web content. Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it. If you would like to use RSS feed of some other site to show their latest posts into your site/blog then WordPress has a handy function which you can use to pull the posts from another site. Following code helps you […]

How to Add Unique Image Class in WordPress

If you want to dynamically add your own class to every image added from WordPress Editor then use following code. Add the code into your theme’s functions.php file and change the class according to your requirement. [php] function wphe_image_class_filter($classes) { return $classes . ‘ your_class_name_here’; //make sure to add a space in front of the class name } add_filter(‘get_image_tag_class’, ‘wphe_image_class_filter’); [/php]

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. […]

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: [php] <a href="<?php the_permalink(); ?>"> // adds a hyperlink to the image <?php if ( has_post_thumbnail() ) { //check if featured image is […]

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. [php] //Disable WordPress Admin Bar for all users. */ show_admin_bar(false); [/php] If you want to disable the Admin Bar for end user but […]

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 […]