Collection of useful PHP code snippets –

Collection of useful PHP code snippets - ManageWP

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.

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'] . ')' . "rn";
//PHP version
echo 'PHP Version ' . phpversion() . "rn";
//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']) &amp;&amp; $_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.

Keep reading the article at ManageWP. The article was originally written by Predrag Zdravkovic on 2022-08-05 08:47:46.

The article was hand-picked and curated for you by the Editorial Team of WP Archives.

Disclosure: Some of the links in this post are "affiliate links." This means if you click on the link and purchase the product, We may receive an affiliate commission.

Leave a Comment

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

Show Your ❤️ Love! Like Us
Scroll to Top