Sometimes you need an action link to activate or deactivate a plugin. How do you do that?
I have written a simple function that can help you achieve this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php /** * Get activation or deactivation link of a plugin * * @author Nazmul Ahsan <mail@nazmulahsan.me> * @param string $plugin plugin file name * @param string $action action to perform. activate or deactivate * @return string $url action url */ function na_action_link( $plugin, $action = 'activate' ) { if ( strpos( $plugin, '/' ) ) { $plugin = str_replace( '\/', '%2F', $plugin ); } $url = sprintf( admin_url( 'plugins.php?action=' . $action . '&plugin=%s&plugin_status=all&paged=1&s' ), $plugin ); $_REQUEST['plugin'] = $plugin; $url = wp_nonce_url( $url, $action . '-plugin_' . $plugin ); return $url; } |
How to use?
This function requires 2 parameters; first one is the plugin file name
, and second one is action type
.
Plugin file name is the base file resides in wp-content/plugins
directory. If you have a plugin called My Super Plugin and the base file is wp-content/plugins/my-super-plugin/my-super-plugin.php
, then the file name will be my-super-plugin/my-super-plugin.php
And second parameter is the action type, either activate
or deactivate
.
So, if you want to activate My Super Plugin, you have to write-
1 |
na_action_link( 'my-super-plugin/my-super-plugin.php', 'activate' ); |
And to deactivate-
1 |
na_action_link( 'my-super-plugin/my-super-plugin.php', 'deactivate' ); |
WPpeople works mainly on WordPress based projects.
It specializes in developing Plugins, Themes and WP based apps. Well, you can count SEO and maintenance jobs too.
© WPpeople 2021 - Made with &