• Resolved docb7

    (@docb7)


    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

    (@bungeshea)

    Hi @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

    (@docb7)

    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.