Safe And Useful Code Snippets

The Code Snippets add-on is probably the most under appreciated feature in the ManageWP arsenal. If you know how to use it, there’s virtually no limit to the ways you can put it to good use. But, what if you’ve never used code snippets before? I’ve put together a list of basic code snippets that you can play with and get familiar with the add-on. They are read only and can’t harm your server or website in any way, so don’t be afraid to try them!

 

code-snippet-featured

List Files

This snippet will list files in your root folder or if you set the folder path it would list files from a specific one.

echo `du -sch ./*`;
echo `ls -la ./wp-content/managewp/*`;

File Content

File content snippet will reveal an entire content of a specific file, you can easily change the path and the filename and you are done.

echo `cat ./wp-config.php`;

Time To Load Page

This snippet will print the loading time of the website’s home page.

$start = time();
// put a long operation in here
sleep(2);
$diff = time() - $start;
print "This page needed $diff seconds to load :-)";

Check File Size

You can easily check the size of any file on your server by running this snippet and all you need to do is to set the correct path and filename.

$filename = 'EXAMPLE/EXAMPLE.JPG';
echo $filename . ': ' . filesize($filename) . ' bytes';

Check Free Space on Your Server

Very useful snippet that will save you a lot of time needed for logging into your cPanel since you don’t have to leave ManageWP to check the free space on your server.

$bytes = disk_free_space(".");
$si_prefix = array( 'B', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
$base = 1024;
$class = min((int)log($bytes , $base) , count($si_prefix) - 1);
echo $bytes . '<br />';
echo sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class] . '<br />';

Check File Readability

By using this snippet, you will be able to see if the file on your server is readable or not.

$filename = 'FILENAME.EXTENSION';
if (is_readable($filename)) {
echo 'The file is readable';
} else {
echo 'The file is not readable';
}

Check Database Stats

This snippet will print out the Database statistics, just replace default values with the real ones.

$link = mysql_connect('HOST', 'USERNAME', 'PASSWORD');
$status = explode(' ', mysql_stat($link));
print_r($status);

Get Database Size

If you are using our Backup add-on, it might be redundant to use this snippet, but if you aren’t, you can check the database size by using this simple snippet.

$link = mysql_connect('host', 'username', 'password');

$db_name = "your database name here";
$tables = array();

mysql_select_db($db_name, $link);
$result = mysql_query("SHOW TABLE STATUS");

while($row = mysql_fetch_array($result)) {
/* We return the size in Kilobytes */
$total_size = ($row[ "Data_length" ] +
$row[ "Index_length" ]) / 1024;
$tables[$row['Name']] = sprintf("%.2f", $total_size);
}

print_r($tables);

That’s It Folks!

Please note that we have selected only the snippets that can’t harm your server and you can try them out with the knowledge that nothing will go wrong. Hopefully these snippets will help you in your daily work tasks. These are just some of our suggestions, but we are sure you have plenty of cool snippets up your sleeves. Share them with us, and let’s make the public snippet library a ton richer!

Aleksandar Savković Avatar

20 responses

  1. Ian Avatar
    Ian

    This is so rad! You’re right, I’ve definitely been under-utilizing this feature (aka. not using it at all ?). Thanks for the writeup and samples to try!

    Is there really a current public snippet library? That’d be pretty handy.

  2. ellegaard Avatar
    ellegaard

    Well, at least you should be able to find a lot of snippets in the old ManageWP! 🙂

    I’m still looking for a solution to one very specifik problem – I need a list of the names of all installed (activated) plugins. I have managed to get a list of all their folder names, but that not quite the same.

    I use it for updating the documentation for each website, so it would a great timesaver to just run the snippet and copy/paster the resulting list.

    Still, this is one of my favorit functions of Orion now. 😉

  3. David Avatar
    David

    How does one use code snippets to change, for example, attributes in WP? For example if I wanted to use a code snippet to turn on or off “Discourage search engines” how would I do that. Also, can a code snippet change an attribute of a plug in? Again, turning on or off something?

  4. ral Avatar
    ral

    Hello Aleksander, I am a new user, I do not have access to the old classic wp. I am looking for a code snippet to clean up a site,
    which has inserted hidden links ( buyviagra, buypills.. etc..) , a code was mentioned in this article https://managewp.com/clean-link-injections-hacked-websites
    but I can not find it. please can you provide it

    Best Regards,

    Ral

  5. Carlos Avatar
    Carlos

    I have 30 websites using the identical theme and layout. The only thing different is the content. I need to update the layout on each site. Is there a way for that to be done? In other words, I need to rearrange paragraphs and images to different places, but identical on each site.

  6. managewp@bkjproductions.com Avatar
    managewp@bkjproductions.com

    Could you give an example showing how to list the titles of all custom posts? For example, in each website I have a knowledgebase which has a custom Post Type called “article” and would like to list all of the titles of these Articles. (You used to have a feature in the old version of ManageWP that would let me bulk-post Posts to multiple websites from the ManageWP Dashboard, I think, so this is sort of where I am headed; I would like to gather up the KB of all of my websites into one grand master KB… so I think if I could get the Titles then I could also get the content as well…)

  7. Andy Warren Avatar
    Andy Warren

    I read or saw somewhere while looking into the code snippets functionality that you could sync plugin settings across sites with it, but it didn’t provide actual snippet needed to do so. Would you be able to point me in the right direction for that? Thanks!

  8. Gavin Avatar
    Gavin

    I am using fastest cache on all my sites. Could this code snippets me used to trigger a clear cache on all sites?
    also i use wpsweep on all site could i use this to to a sweep all on all sites?

    How would i find the codes if this can be done?

  9. Nathan Avatar
    Nathan

    Hi Aleksandar,
    This is such a great feature that we are jumping into using a lot more, along with a lot of other MWP features.
    As an agency we would love to run a script for installing a default set of plugins that we use regularly. I know for new sites that can be accomplished in a Template, but for existing sites it would be great. Would that be possible? Could you point me in the right direction for something like that?

  10. Thomas Tremain Avatar
    Thomas Tremain

    Your database snippets are using the depricated mysql_* commands. You should be using $wpdb instead.

  11. keenan Avatar
    keenan

    Hello,

    I am using fastest cache on all my sites.

    Could this code snippets me used to trigger a clear cache on all sites?

  12. David Bartholomew Avatar
    David Bartholomew

    Would you have a snippet that can review WooCommerce Orders to just display a count of orders for a period of time. Or just pull a list of order numbers, total$, order date, and order status?

    We have a lot of sites that operate with commissions that require us to open the site every week or month and this would be excellent if we could do via a snippet.

Leave a Reply

Your email address will not be published. Required fields are marked *