[wp-hackers] Change in plugin Options page handling

Morgan Doocy morgan at doocy.net
Thu Feb 3 17:02:17 GMT 2005


Owen has already explained some or most of this, but just for 
clarification I'll do it too.

On Feb 3, 2005, at 7:26 AM, Carthik Sharma wrote:
> Now, do plugins need to have a totally seperate options page? (what's
> ml_options_page?)

ml_options_page was the function that I used specifically for my 
plugin. I should have titled the example 'my_options_page_function' for 
clarity. This is the function you use to output your Options page, and 
it's just a normal action function just like any other.

> How would a plugin developer load his option page, and how would an
> options page be added for all the different plugins?

1. Use add_options_page() to add the link to the Options submenu
2. Click the plugin's Options submenu item, which directs the user to 
admin.php?page=pluginname.php
3. WordPress looks at the '?page=pluginname.php', strips the '.php', 
and looks for an action hook called 'options_page_pluginname'
4. If a hook for that has been specified by the plugin, WordPress 
executes that action; if not, it loads pluginname.php just like it used 
to.

> I am sorry if I got it wrong, but to add_actions(something) don't you
> need a "something" hook? Does WordPress automatically add hooks like
> options_page_<name-of-plugin-file> each time a new plugin is
> activated?

WordPress doesn't add these automatically. A plugin author simply hooks 
into it like they would any other action:

add_action('options_page_pluginname', 'my_options_page_function');

> An example of how this is to be used, with a mention of how it works
> internally within WordPress, would be great.

I'll write a Hello World plugin, which echoes "Hello World!" in the 
admin header like Hello Dolly does, to illustrate. First, the old way:

<?php
// hello-world.php
if (is_plugin_page()) {	?>
<div class="wrap">
<h2>Hello World Options</h2>
<?php
	// ... (rest of options page)
}
else {
	function hw_hello_world() {
		echo "Hello World!";
	}
	function hw_admin_menu() {
		add_options_page('Hello World Options', 'Hello World', 5, 
'hello-world.php');
	}
	add_action('admin_header', 'hw_hello_world');
	add_action('admin_menu', 'hw_admin_menu');
}
?>
...then the new way:

<?php
// hello-world.php
function hw_options_page() { ?>
<div class="wrap">
<h2>Hello World Options</h2>
<?php
	// ... (rest of options page)
}
function hw_hello_world() {
	echo "<h3>Hello World!</h3>";
}
function hw_admin_menu() {
	add_options_page('Hello World Options', 'Hello World', 5, 
'hello-world.php');
}
add_action('options_page_hello-world', 'hw_options_page');
add_action('admin_header', 'hw_hello_world');
add_action('admin_menu', 'hw_admin_menu');
?>

You can see that the only change that has been made is the elimination 
of the if (is_plugin_page()) { } else { } nonsense. The Options page 
stuff is still there, it's just been moved to an action hook like all 
the rest of the API uses. It's just cleaner all the way around, I 
think, and more consistent with the way the rest of the API works. This 
is_plugin_page() nonsense was a real oddity.

> Thanks for all your work with WordPress lately :)

It's kind of self-serving, actually. I'm just submitting patches which 
make my life easier writing plugins! :-)

Morgan



More information about the hackers mailing list