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']) && $_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(

[…]

 



This article was written by Predrag Zdravkovic and originally published on ManageWP.

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