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!

The post Safe And Useful Code Snippets appeared first on ManageWP.

Leave a Reply

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