Title: Execute PHP Code (not a WordPress Function) on specific side
Last modified: April 11, 2020

---

# Execute PHP Code (not a WordPress Function) on specific side

 *  Resolved [docb7](https://wordpress.org/support/users/docb7/)
 * (@docb7)
 * [6 years, 1 month ago](https://wordpress.org/support/topic/execute-php-code-not-a-wordpress-function-on-specific-side/)
 * Hi all,
    I love code Snippets and I managed to do some amazing things with it(
   for my little knowledge). But I am not able to execute my php code on a specific
   site. I read through this forum and got the idea to use is_single but I don’t
   get how to execute my code and displaying the result (Title of the page I want
   to try that is “tmv”). With this code nothing happens 🙁
 *     ```
       if ( is_single('tmv') ){
       function dir_size($directory) {
           $size = 0;
           foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
               $size += $file->getSize();
           }
           return $size;
       }
       function format_size($size) {
           $mod = 1024;
           $units = explode(' ','B KB MB GB TB PB');
           for ($i = 0; $size > $mod; $i++) {
               $size /= $mod;
           }
           return round($size, 2) . ' ' . $units[$i];
       }
   
       $oldsize=dir_size("/var/www/cm");
       echo "Ergebnis:".$oldsize;
       }
       echo "Hallo Welt 2";
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [6 years ago](https://wordpress.org/support/topic/execute-php-code-not-a-wordpress-function-on-specific-side/#post-12782695)
 * Hi [@docb7](https://wordpress.org/support/users/docb7/),
 * I would recommend adding this code as a shortcode, like so:
 *     ```
       add_shortcode( 'tmv', function () {
       	$directory = "/var/www/cm";
   
       	$size = 0;
       	foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
       		$size += $file->getSize();
       	}
   
       	$mod = 1024;
       	$units = explode(' ','B KB MB GB TB PB');
       	for ($i = 0; $size > $mod; $i++) {
       		$size /= $mod;
       	}
       	$formatted_size = round($size, 2) . ' ' . $units[$i];
   
       	return "Ergebnis:".$formatted_size;
       } );
       ```
   
 * Once you’ve added this as an activated snippet, you can just use the `[tmv]` 
   shortcode within the post or page where you want the code to run.
 *  Thread Starter [docb7](https://wordpress.org/support/users/docb7/)
 * (@docb7)
 * [6 years ago](https://wordpress.org/support/topic/execute-php-code-not-a-wordpress-function-on-specific-side/#post-12784725)
 * Perfect, thank you so much!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Execute PHP Code (not a WordPress Function) on specific side’ is closed
to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [docb7](https://wordpress.org/support/users/docb7/)
 * Last activity: [6 years ago](https://wordpress.org/support/topic/execute-php-code-not-a-wordpress-function-on-specific-side/#post-12784725)
 * Status: resolved