Teno
Teno

Reputation: 2632

shell_exec() Does Not Work Used in Admin Panel of WordPress

I'm using shell_exec() to run a background process in my WordPress plugin and found that WordPress somehow gets confused when shell_exec() is used in an Admin option page. It seems that inclusion using relative paths do not process correctly.

This is the sample plugin I wrote to demonstrate the problem:

/* Plugin Name: Sample ShellExec */

add_action('admin_menu', 'sample_shellexec_menu');
function sample_shellexec_menu() {
    add_options_page(
        'Sample Shell Exec', 
        'Sample Shell Exec', 
        'manage_options',
        'sample_shell_exec', 
        'sample_shellexec_admin');
}
function sample_shellexec_admin() {
    ?>
    <div class="wrap">
    <?php
        $phppath = 'php';
        // $phppath = 'Z:\xampp\php\php.exe'; // in my case
        echo ABSPATH . '<br />';
        $output = shell_exec($phppath . ' "' . ABSPATH . '/index.php" 2>&1');       
        echo $output;
    ?>
    </div>
    <?php
}

Go to the option page and you'll see an error message like,

Warning: require(./wp-blog-header.php): failed to open stream: No such file or directory in [path to the WordPress]\index.php on line 17 Call Stack: 0.0002 320664 1. {main}() [path to the WordPress]\index.php:0 Fatal error: require(): Failed opening required './wp-blog-header.php' (include_path='.;\xampp\php\PEAR') in [path to the WordPress]\index.php on line 17 Call Stack: 0.0002 320664 1. {main}() [path to the WordPress]\index.php:0

The error does not occur if shell_exec() is used outside the admin panel. I'd like to know why.

Upvotes: 1

Views: 2748

Answers (1)

Alain
Alain

Reputation: 36984

You may try to force the environnement to run your command in the good path:

shell_exec("cd " . escapeshellarg(ABSPATH) . "; " . $phppath . ' index.php 2>&1');

I'm used to Linux systems so I don't know if this works on Window.

Upvotes: 2

Related Questions