Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a WordPress plugin install link

I'm created a plugin that replaces a previous plugin I wrote and adds some extra stuff too. I want to create an alert in the settings page of my old plugin with a link to download and install my new plugin and inform them that the old plugin is no longer supported by its developer. Currently, from the plugin page of my admin panel, install links look like this:

http://www.example.com/wp-admin/network/update.php?action=install-plugin&plugin=wptouch&_wpnonce=a8d64aa295

It seems to me there are at least two things too keep in mind when creating the URI:

  1. Check whether or not this is a multi-site setup
  2. Make sure I generate the correct nonce.

Does anybody know how to properly do this? Is there anything else I need to consider when generating this URI?

Thanks in advance for your help.

like image 381
Philip Walton Avatar asked Dec 27 '22 23:12

Philip Walton


1 Answers

I've found an answer. In wp-admin/import.php, there's a great example of how this work.

Here is my implementation:

$plugin_name = 'my_plugin_name';
$install_link = '<a href="' . esc_url( network_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $plugin_name . '&TB_iframe=true&width=600&height=550' ) ) . '" class="thickbox" title="More info about ' . $plugin_name . '">Install ' . $plugin_name . '</a>';

Then just output that link tag anywhere in your admin pages.

If you want to get fancy and add the thickbox modal window effect, just use the function add_thickbox() and attach it to an action that's called in a typical admin request, such as:

add_action('admin_menu', 'my_plugin_add_thickbox');
function my_plugin_add_thickbox() {
    add_thickbox();
}

That should do it!

like image 121
Philip Walton Avatar answered Jan 28 '23 22:01

Philip Walton