Generating a WordPress plugin activation link url
June 23, 2014
As I might need it again I've put together a gist for a WordPress function that will generate a plugin activation url like the ones WordPress will generate in the plugins administration screen [caption id="attachment_1107" align="aligncenter" width="1024"][](http://theaveragedev.local/wordpress/wp-content/uploads/2014/06/2014-06-18-at-10.40.png) Activate link[/caption] The function will require a string input like
my-plugin/my-plugin.php
which can be hard-coded knowing a specific plugin folder and main plugin file information or using another function that will check for an installed plugin using its title.
/**
* Generate an activation URL for a plugin like the ones found in WordPress plugin administration screen.
*
* @param string $plugin A plugin-folder/plugin-main-file.php path (e.g. "my-plugin/my-plugin.php")
*
* @return string The plugin activation url
*/
function generatePluginActivationLinkUrl($plugin)
{
// the plugin might be located in the plugin folder directly
if (strpos($plugin, '/')) {
$plugin = str_replace('/', '%2F', $plugin);
}
$activateUrl = sprintf(admin_url('plugins.php?action=activate&plugin=%s&plugin_status=all&paged=1&s'), $plugin);
// change the plugin request to the plugin to pass the nonce check
$_REQUEST['plugin'] = $plugin;
$activateUrl = wp_nonce_url($activateUrl, 'activate-plugin_' . $plugin);
return $activateUrl;
}