[wp-hackers] Friendly plugin URL entrypoints?

Ryan Boren ryan at boren.nu
Sat Feb 4 19:55:35 GMT 2006


Owen Winkler wrote:
> Would it be worthwhile to have a dedicated friendly-style URL entry 
> point for plugins?  For example...
> 
> I have a couple plugins that employ Ajax.  I have a couple that generate 
> javascript based on a request.  It would be useful to have a URL that 
> was an entry point to those features that didn't include three 
> directories of depth of wp organization.
> 
> Instead of requesting:
> http://example.com/wp-content/plugins/myplugin/myplugin.php?entrypoint=1
> 
> I could request something like:
> http://example.com/_plugin/myplugin/1
> 
> And I could generate that URL by calling something like:
> get_plugin_entrypoint('myplugin', 1);
> 
> And just maybe a request of that URL could fire a hook:
> do_action('entrypoint_myplugin', 1);
> 
> Which I would register by calling:
> register_entrypoint('myplugin', 1, 'my_entrypoint_function');
> 
> Yes, I can modify the rewrite rules in my plugin to handle this.  But 
> I'll do it in every plugin I write that uses javascript like this, which 
> would repeat a bunch of code.  If some other developer does it, we'll be 
> cluttering the site's URL landscape with plugin entrypoints, and it 
> might be nice to put them all in one place.  Something like this could 
> make it much easier to implement Ajax (and other javascript techniques) 
> via plugin to provide a more rich user experience.
> 
> Does this seem worthwhile?  Does it seem doable?
> 
> I've been writing a couple of new things that use a few complicated 
> javascript techniques, and it occurs to me that this is simply not poetry:
> 
> class AjaxPlugin {
>     function AjaxPlugin () {
>         if(defined('ABSPATH')) {
>             add_action('wp_header',
>             array(&$this, 'wp_header'));
>         }
>     }
>     function solo() {
>         // Do things when the plugin file is loaded by
>         // itself but that also require WP functions/data
>     }
>     function include_up() {
>         // This function finds a file in
>         // progressive parent directories
>         return $file_location;
>     }
>     function wp_header() {
>         // Create a reference to this file in a script tag
>         echo '<script type="text/javascript" src="'
>         . $this->pluginuri . '"></script>';
>     }
>     function pluginuri() {
>         // Return the URI of this file in the plugins dir
>         return get_settings('siteurl')
>         . '/wp-content/plugins/'
>         . plugin_basename(__FILE__);
>     }
> }
> $ajaxplugin = new AjaxPlugin();
> if(!defined('ABSPATH')) {
>     // Must include wp-config.php at the global scope.
>     include($ajaxplugin->include_up('wp-config.php');
>     $ajaxplugin->solo();
> }
> 
> 
> Suggestions for prettier alternatives are welcome.

I don't worry with pretty URIs, but here's what I do to set an entry 
point.  It takes advantage of get_plugin_page_hookname() behavior to 
create one entrypoint that is triggered when admin.php?page=mypluginpage 
loads.  I usually have that entry point act as a dispatcher for the plugin.

function get_plugin_page_link($file) {
	$name = plugin_basename($file);
	$args = array('page' => $name, 'noheader' => 1);
	return add_query_arg($args, get_settings('siteurl') . 
"/wp-admin/admin.php");
}

function myplugin_get_plugin_page_link() {
	return get_plugin_page_link(__FILE__);
}

function register_plugin_page_hook($file, $hook) {
	$hookname = plugin_basename($file);
	$hookname = preg_replace('!\.php!', '', $hookname);
	// get_plugin_page_hookname() in admin-functions.php creates
	// a _page_ hook if the requested plugin page is not registered
	// as a menu handler.  admin.php will do_action on this hook.
	$hookname = '_page_' . $hookname;
	add_action($hookname, $hook);
}

function myplugin_register_plugin_page_hook() {
	register_plugin_page_hook(__FILE__, 'myplugin_plugin_page_handler');
}


More information about the wp-hackers mailing list