php - Self deactivate plugins after an action occurs

admin2025-06-05  2

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749111744a316458.html

最新回复(0)