Useful Snippets


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at

RSS Latest posts from my personal blog

Most viewed posts

Subscribe to RSS feed

InfiniteWP snippet library for Code Snippets Addon

Stanislav KhromovStanislav Khromov

InfiniteWP Logo

InfiniteWP Code Snippets Addon is a great tool to easily run code snippets on all your wordpress sites simultaneously.

Below you will find a couple of useful snippets for it. I’m going to add more snippets shortly. In the mean time feel free to post any good snippets you have in the comments!

Show blog title

echo get_bloginfo();    

Here is what that snippet looks in InfiniteWP. (More snippets after the image)

Code snippet example

Basic database interaction, getting the site URL

global $wpdb;
$table_name = $wpdb->prefix . "options";
$sql = "SELECT option_value FROM ".$table_name." WHERE option_name = 'siteurl';";
$root_url = $wpdb->get_var($sql);
echo $root_url;

This will return your site URL from the database.

Show server load averages

$loads = '';
foreach(sys_getloadavg() as $load)
    $loads .= $load . ', ';
echo substr($loads, 0, -2);

Show disk space (total/free) on your host

function decodeSize($bytes)
    $types = array( 'B', 'KB', 'MB', 'GB', 'TB' );
    for( $i = 0; $bytes >= 1024 && $i < ( count( $types ) -1 ); $bytes /= 1024, $i++ );
    return( round( $bytes, 2 ) . " " . $types[$i] );

function freeSpacePercentage($folder = ".")
    return round((decodeSize(disk_free_space("."))/decodeSize(disk_total_space("."))*100)) . "%";

function usedSpacePercentage($folder = ".")
    return round(((100*decodeSize(disk_free_space("."))/decodeSize(disk_total_space(".")))-100)*-1) . "%";

echo "Disk space: " . decodeSize(disk_free_space(".")) . " of " . decodeSize(disk_total_space("."));
echo " - ";
echo freeSpacePercentage() . ' free.';

Note 1: Some shared hosts have disabled the disk_total_space(). In those circumstances the snippet does not work.
Note 2: Free space is free space on the physical drive where WordPress is installed. On shared hosts this does not accurately represent your disk quota.

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
Twitter Profile
Visit my other blog

Comments 5
  • Alex Almeida
    Posted on

    Alex Almeida Alex Almeida

    Reply Author

    where to insert this code snipets?

  • IMGBob
    Posted on

    IMGBob IMGBob

    Reply Author

    I love the idea of creating a library of IWP Code Snippets. I see this post it a few years old. Have you moved your list elsewhere?

    We just figured out how to clear the WordFence Cache and add update WordFence settings across all of our sites with IWP Code Snippets. I am now curious about what other neat tricks people have found for this addon.

  • Ryan Lindsey
    Posted on

    Ryan Lindsey Ryan Lindsey

    Reply Author

    Set WordFence settings using a Snippet:
    try {
    } catch(Exception $e){
    $errorMsg = $e->getMessage();
    //Do something intelligent with the error you received from Wordfence.
    echo $errorMsg;
    } //Otherwise if the catch block doesn’t execute the function has succeeded.

    Disable comments on website:
    update_option( ‘default_comment_status’, ‘closed’ );