$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Self deactivate plugins after an action occurs|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Self deactivate plugins after an action occurs

matteradmin9PV0评论

I'm developing a WordPress plugin.

I want to download a file from a server, then unzip and put it inside the main directory of WordPress after the plugin is activated. The process of download and unzip of the file run correctly, I have some trouble to deactivate the plugin after this process is done. How can I fix it?

class WP_X_Plugin{

public function __construct(){
    register_activation_hook( __FILE__, array( $this , 'activate' ) );

    $url = ".zip";
    file_put_contents("dist.zip", file_get_contents($url));

    $plugin_zip = new ZipArchive;

    $plugin_zip->open('dist.zip');
    $plugin_zip->extractTo(ABSPATH);
    $plugin_zip->close();

    rename('dist.php', ABSPATH.'/wp-script.php');
    if( unlink('dist.zip') ){
      // if i call the deactivate_plugins() function of wordpress, I will have an error logged in console PHP Fatal error:  Uncaught Error: Call to undefined function deactivate_plugins().
      deactivate_plugins( plugin_basename(__FILE__) );
      header('Location: wp-script.php');
    }
}

}

$wp_x = new WP_X_Plugin;

UPDATE

I've solved by requiring the plugin.php file that is inside the wp-admin/includes folder. I've used this solution after reading some questions here related to the same issue. As I've understand, the plugins functions are available only if wp has already loaded this file at the runtime.

I'm developing a WordPress plugin.

I want to download a file from a server, then unzip and put it inside the main directory of WordPress after the plugin is activated. The process of download and unzip of the file run correctly, I have some trouble to deactivate the plugin after this process is done. How can I fix it?

class WP_X_Plugin{

public function __construct(){
    register_activation_hook( __FILE__, array( $this , 'activate' ) );

    $url = "http://mysitedomain/script/dist.zip";
    file_put_contents("dist.zip", file_get_contents($url));

    $plugin_zip = new ZipArchive;

    $plugin_zip->open('dist.zip');
    $plugin_zip->extractTo(ABSPATH);
    $plugin_zip->close();

    rename('dist.php', ABSPATH.'/wp-script.php');
    if( unlink('dist.zip') ){
      // if i call the deactivate_plugins() function of wordpress, I will have an error logged in console PHP Fatal error:  Uncaught Error: Call to undefined function deactivate_plugins().
      deactivate_plugins( plugin_basename(__FILE__) );
      header('Location: wp-script.php');
    }
}

}

$wp_x = new WP_X_Plugin;

UPDATE

I've solved by requiring the plugin.php file that is inside the wp-admin/includes folder. I've used this solution after reading some questions here related to the same issue. As I've understand, the plugins functions are available only if wp has already loaded this file at the runtime.

Share Improve this question edited Dec 10, 2018 at 12:17 user9741470 asked Dec 9, 2018 at 17:12 user9741470user9741470 1115 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The deactivate_plugins() function is available only after the admin_init action is executed, you will either need to register 'admin_init' filter and run the deactivate_plugins() function there or manually include the wp-admin/includes/plugin.php file.

I updated your code with the second solution below.

class WP_X_Plugin{

    public function __construct(){
        register_activation_hook( __FILE__, array( $this , 'activate' ) );

        $url = "http://localhost/dist.zip";
        file_put_contents("dist.zip", file_get_contents($url));

        $plugin_zip = new ZipArchive;

        $plugin_zip->open('dist.zip');
        $plugin_zip->extractTo(ABSPATH);
        $plugin_zip->close();

        rename('dist.php', ABSPATH.'/wp-script.php');
        if( unlink('dist.zip') ){
            include_once ABSPATH . '/wp-admin/includes/plugin.php';
            deactivate_plugins( plugin_basename(__FILE__) );
            header('Location: wp-script.php');
        }
    }  

    public function activate() {

    }
}

$wp_x = new WP_X_Plugin;

BTW. Instead of header() function you might consider using the wp_redirect() function instead or at least add 'exit;' after header() call.

Also, right now your code will run on each page load including WP_X_Plugin activation and will disable itself so the redirect might be ignored.

Post a comment

comment list (0)

  1. No comments so far