Effective PHP Scripts to use for your WordPress blog

useful php scripts

WordPress plugins are effective and easier to use. They do what they are meant to. You can get WP plugins for almost any tweak you want to do with your site. But there are still a few tweaks that can’t be done by plugins. Or, if they could be done — plugins might build seriously more load on your site. To solve this, you can always place some PHP scripts which run into your WordPress’ site’s front-end to offer wanted results. This process doesn’t need installation of plugins and hence strengthens your site’s security and keeps bloat away.

This article was brought to you by Intellipaat, the team behind an awesome Hadoop developer program. You can explore the provided hyperlinks for more details!

Here is the list of some highly useful PHP scripts to use for your WordPress blog. This list is expandable and will surely expand to larger number of scripts as time moves on. All the codes listed below are error-proof and tested by me on my blogs.

Batch remove all featured images from your WordPress posts and pages

Place the given PHP code in between php and ?> inside your theme’s functions.php file. If there is no ending ?>, it is recommended to place the code at the end-part of the file.

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

After you finished placing the code, save and update the functions.php file. Viola! You see that all featured images are gone. Now you can remove above php code from functions.php as it is just a one time job. Please note that you will not be able to set featured images until you remove the mentioned code.

Credit: Kaiser

Redirect Author Archives to ‘About’ page

For a blog with single author, there is no need of Author archivesas the same can be achieved by regular archives. It’s better suggest to redirect Author archives to your own about page. Put the following code inside your theme’s functions.php file:

add_filter( 'author_link', 'my_author_link' );
function my_author_link() {
 return home_url( 'about' );
 }

Here highlighted text ‘about’ is the slug at which your about page is located.

Credit: WPHub

 

Show Estimated Reading Time at Post Meta

Want to tell your readers, how long will it take an article to read completely? Insert the following code into right file (e.g., single.php or page.php) at the right place (above post meta and below post title) in your theme. It counts total words in an article and calculates time by estimating that on average a reader can read 200 words in a minute.

$mycontent = $post->post_content;
            $word = str_word_count(strip_tags($mycontent));
            $m = floor($word / 200);
            $s = floor($word % 200 / (200 / 60));
            $est = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's');
?>

Estimated Reading Time:

  • Least reading time is 0 minutes and 1 second for blog post with almost zero words.

no words estimated time

  • A huge article may have reading time as long as half an hour:

some content

Credit: Brian Cray

Replace default WordPress Login Logo with your site’s custom header image

There are few plugins for that, but you can also do the same by inserting these brief lines of code to your theme’s functions.php file:

function namespace_login_style(){if( function_exists('get_custom_header')){
        $width = get_custom_header()->width;
        $height = get_custom_header()->height;}else{
        $width = HEADER_IMAGE_WIDTH;
        $height = HEADER_IMAGE_HEIGHT;}
    echo ''.PHP_EOL;}

 

Hide from readers that you are using WordPress or WooCommerce or any other Custom Frameworks

Do you use WordPress or WooCommerce or even a custom framework but still don’t want others to know what CMS platform you really use? Input the following codes in functions.php file and let no one know where you are blogging at:

function complete_version_removal(){return'';}
add_filter('the_generator','complete_version_removal');

Limit the maximum number of post revisions stored on Database

This reduces load on your web-host by changing infinite post revision limits into a finite number. For example if you want to save no more than 10 post revisions of posts/pages in your dashboard, place the following code inside functions.php. :

if(!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS',10);

This works only if you have not already set post revisions’ limit in your WordPress installation’s wp-config.php file.

Load WooCommerce Scripts only When Needed

WooCommerce is useful, but a very heavy plugin. It enqueues its files on every page of your site, whether needed or not. By simply copying & pasting the following code into your theme‘s functions.php file, you can make sure that WooCommerce scripts load only on WooCommerce pages.

add_action('wp_enqueue_scripts', 'myown_remove_scripts');

function myown_remove_scripts(){
if ( function_exists( 'is_woocommerce' ) ) {
 //dequeue scripts and styles from not WC pages
 if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
 wp_dequeue_style( 'woocommerce-layout');
 wp_dequeue_style( 'woocommerce-smallscreen');
 wp_dequeue_style( 'woocommerce-general');
 wp_dequeue_script( 'wc_price_slider' );
 wp_dequeue_script( 'wc-single-product' );
 wp_dequeue_script( 'wc-add-to-cart' );
 wp_dequeue_script( 'wc-cart-fragments' );
 wp_dequeue_script( 'wc-checkout' );
 wp_dequeue_script( 'wc-add-to-cart-variation' );
 wp_dequeue_script( 'wc-single-product' );
 wp_dequeue_script( 'wc-cart' );
 wp_dequeue_script( 'wc-chosen' );
 wp_dequeue_script( 'woocommerce' );
 wp_dequeue_script( 'prettyPhoto' );
 wp_dequeue_script( 'prettyPhoto-init' );
 wp_dequeue_script( 'jquery-blockui' );
 wp_dequeue_script( 'jquery-placeholder' );
 wp_dequeue_script( 'fancybox' );
 wp_dequeue_script( 'jqueryui' );
   }
  }
}

Adding Support for Post Thumbnails & Custom Thumbnail Sizes

This one too goes inside theme’s functions.php :

// adding theme support for post thumbnails
add_theme_support( 'post-thumbnails' );
// adding certain sizes

 add_image_size( 'own-id', '1000', '700', false );
 add_image_size( 'another-id-here', '500', '350', true );

‘true’ and ‘false’ are answers of argument, whether to force-crop the image or not.

One comment

  1. Kulvir Singh · July 5

    Nice collection of PHP scripts for WordPress use. Thanks Gaurav for this.