Collection of useful PHP code snippets

Code Snippets are one of less know but very powerful tools in the ManageWP arsenal. It lets you change plugin settings, clear cache or remove dashboard items on multiple websites at once. Your PHP skills are the limit!

We know that plugins can be used to extend the functionality of WordPress. But what if you can do some smaller things in WordPress without installing them? Say, you dislike the admin bar at the top and wish to eliminate it? You quickly want to check the PHP versions across your websites? All of that can be accomplished by means of PHP code snippets for WordPress.

We’ve put together a collection of useful PHP snippets from around the web that you can run through ManageWP Code snippet feature, all of which can make your life with WordPress just a little bit easier.

Code Snippets Section

Hint: All of the snippets can be saved and used later on.

ManageWP friendly PHP code snippets

Selected snippets will not 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.

Check WordPress & PHP versions across websites

This code snippet allows you to check for PHP and WordPress versions across the selected websites. You can easily exclude the info you don’t need from the code and just focus on the data that interest you.

<?php
// Website name and domain
echo get_bloginfo('name'). ' ('. $_SERVER['HTTP_HOST'] . ')' . "\r\n";
//PHP version
echo 'PHP Version ' . phpversion() . "\r\n";
//WordPress version 
echo 'WordPress version ' ; bloginfo('version');
?>

Check Free Space on Your Server

<?php
$df = round(disk_free_space("/var/www") / 1024 / 1024 / 1024);
print("Free space: $df GB");
?>

Note: Some servers might disable this command for security reasons.

Quick HTTPS check

Here is code to quickly run through your list of websites and check if they are running over http or https.

<?php
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'
|| $_SERVER['SERVER_PORT'] == 443) {
//https
echo 'https';
} else {
//http
echo 'http';
}

Flushing cache with Code Snippets

Since WordPress doesn’t include caching by default, we can instead use plugins like WP Rocket or W3 Total Cache. Without manually clearing the cache, it will only expire after a set amount of time. Here are a few code snippets that will empty the cache on your demand.

Clear WP Rocket cache

<?php 
 
// Load WordPress environment.
require 'wp-load.php';

// Define some constants.
if ( ! defined( 'COOKIEHASH' ) ) {
	$siteurl = get_site_option( 'siteurl' );
	if ( $siteurl ) {
		define( 'COOKIEHASH', md5( $siteurl ) );
	} else {
		define( 'COOKIEHASH', '' );
	}
}
if ( ! defined( 'LOGGED_IN_COOKIE' ) ) {
	define( 'LOGGED_IN_COOKIE', 'wordpress_logged_in_' . COOKIEHASH );
}

// Instatiate the WP_Rewrite class and store it in $wp_rewrite.
$GLOBALS['wp_rewrite'] = new WP_Rewrite();

// Load WP Rocket environment.
require 'wp-content/plugins/wp-rocket/wp-rocket.php';
require 'wp-content/plugins/wp-rocket/inc/functions/i18n.php';
require 'wp-content/plugins/wp-rocket/inc/functions/formatting.php';
require 'wp-content/plugins/wp-rocket/inc/functions/options.php';
require 'wp-content/plugins/wp-rocket/inc/API/preload.php';
require 'wp-content/plugins/wp-rocket/inc/3rd-party/3rd-party.php';

// Clear the cache.
if ( function_exists( 'rocket_clean_domain' ) ) {
	rocket_clean_domain();
	echo 'Cache cleared';
}
 
// Check if sitemap preload is enabled.
if ( function_exists( 'get_rocket_option' ) ) { 
    $sitemap_preload = get_rocket_option( 'sitemap_preload');
}

// Preload the cache.
if( 1 == $sitemap_preload ) { 
	// run sitemap preload.
	if ( function_exists( 'run_rocket_sitemap_preload' ) ) { 
	   run_rocket_sitemap_preload(); 
	   echo ' - Sitemap Preload started';
	}
}

Clear W3 total cache

// purge entire cache
if(function_exists('w3tc_pgcache_flush')) { w3tc_pgcache_flush(); }

// flush individual post/page
if (function_exists('w3tc_pgcache_flush_post')) { w3tc_pgcache_flush_post($post_id); }

This is particularly useful if you want to purge cache across multiple websites or you just want to refresh a single post/page and see any changes you made in the meantime.

Conclusion

We hope that you will find these code snippets useful or at least spark your interest to further explore their use.

In case you want to experiment with some code snippets on your website, check out some of our previous articles on the subject:

You already using PHP code snippets? Share what work best for you in the comments below.

Predrag Zdravkovic

If you read Alice in Wonderland you might remember him as the majestic caterpillar making yourself question who YOU really are. Unlike his literary twin, he has more than enough patience and sometimes even a kind word or two. Special educator and SLP by design, getting down to the cause of the problem is what he does best.

0 Comments

Leave a Reply

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

Over 65,000 WordPress professionals are already using ManageWP

Add as many websites as you want for free, no credit card required. Sign up and start saving time!

Have questions? Get in touch!

Over 65,000 WordPress professionals are already using ManageWP

Add as many websites as you want for free, no credit card required. Sign up and start saving time!



Have questions? Get in touch!

Over 65,000 WordPress professionals are already using ManageWP

Add as many websites as you want for free, no credit card required. Sign up and start saving time!



Have questions? Get in touch!

Over 65,000 WordPress professionals are already using ManageWP

Add as many websites as you want for free, no credit card required. Sign up and start saving time!



Have questions? Get in touch!