Get blog info
See WordPress version and number of posts
global $wpdb;
$query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type='post' AND post_status='publish'";
$post_count = $wpdb->get_var($query);
echo get_bloginfo('name').' is using WordPress '.get_bloginfo('version').' and has '.$post_count.' published posts.';
Example response:
Example Blog is using WordPress 5.4.1 and has 3 published posts.
[ Back to Top ]
List plugins
List files in Plugins directory
$files = scandir("wp-content/plugins");
foreach($files as $file) {
echo $file."\n";
}
Example response:
.
..
elementor
index.php
responsive-lightbox
yoast
[ Back to Top ]
Get theme directory in WordPress
WordPress comes with a number of useful functions you can use to get the current theme path or URL. You can run the following PHP code to list their values:
echo 'The URL of the current theme (parent):' . PHP_EOL;
echo get_template_directory_uri() . PHP_EOL;
echo 'The URL of the current theme (child):' . PHP_EOL;
echo get_stylesheet_directory_uri() . PHP_EOL;
echo 'the URL of the current theme stylesheet URL' . PHP_EOL;
echo get_stylesheet_uri() . PHP_EOL;
echo 'the URL of the theme directory.' . PHP_EOL;
echo get_theme_root_uri() . PHP_EOL;
echo 'the URL of the file path of the themes directory without a trailing slash' . PHP_EOL;
echo get_theme_root() . PHP_EOL;
echo 'Array of themes located in the themes directory' . PHP_EOL;
echo get_theme_roots() . PHP_EOL;
echo 'The full file path to the current theme directory' . PHP_EOL;
echo get_stylesheet_directory() . PHP_EOL;
echo 'The full file path to the current theme directory' . PHP_EOL;
echo get_template_directory() . PHP_EOL;
Example response:
The URL of the current theme (parent):
https://example.com/wp-content/themes/twentytwenty
The URL of the current theme (child):
https://example.com/wp-content/themes/twentytwenty
the URL of the current theme stylesheet URL
https://example.com/wp-content/themes/twentytwenty/style.css
the URL of the theme directory.
https://example.com/wp-content/themes
the URL of the file path of the themes directory without a trailing slash
/home/www/html/example.com/wp-content/themes
Array of themes located in the themes directory
/themes
The full file path to the current theme directory
/home/www/html/example.com/wp-content/themes/twentytwenty
the full file path to the current theme directory
/home/www/html/example.com/wp-content/themes/twentytwenty
Reference: Get Theme Directory In WordPress – Paulund
[ Back to Top ]
Estimate time to load page
$start = time();
// put a long operation in here
sleep(2);
$diff = time() - $start;
print "This page needed $diff seconds to load :-)";
[ Back to Top ]
Check file size
$filename = 'EXAMPLE/EXAMPLE.JPG';
echo $filename . ': ' . filesize($filename) . ' bytes';
[ Back to Top ]
Check free space on your server
Very useful snippet 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;
echo sprintf('%1.2f' , $bytes / pow($base,$class)) . ' ' . $si_prefix[$class];
Example response:
138288971776
128.79 GB
[ Back to Top ]
Check file readability
$filename = 'FILENAME.EXTENSION';
if (is_readable($filename)) {
echo 'The file is readable';
} else {
echo 'The file is not readable';
}
[ Back to Top ]
Get the URL of the WP upload directory
echo wp_get_upload_dir()['baseurl'];
Example response:
https://example.com/wp-content/uploads
[ Back to Top ]
Rename files with garbled filename
The command ls -i
shows the inode number of files
echo `ls -i ./filepath`;
The command find -inum
returns the filepath of a specific inode in the file system
echo `find ./filepath -inum 55340444 `;
With the parameter and the placeholder -exec {}
, the command apply the second part to rename the specific inode file
echo `find ./filepath -inum 55340444 -exec mv {} newfilepath \;`;
[ Back to Top ]
Get database size
$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);
[ Back to Top ]
Get database stats
$link = mysql_connect('HOST', 'USERNAME', 'PASSWORD');
$status = explode(' ', mysql_stat($link));
print_r($status);
[ Back to Top ]
If you enjoy this post, please share it on Facebook and Twitter. You might also support us by making a donation through Ko-fi.