[wp-hackers] Setting default options for plugin

Peter Westwood peter.westwood at ftwr.co.uk
Sat Sep 1 14:25:11 GMT 2007

On 1 Sep 2007, at 14:52, Johannes Ruthenberg wrote:

> Hello Peter!
> I'm relatively new to WordPress, but I've been reading this list  
> for some weeks now. This seems like a good opportunity to delurk. ;-)

Firstly, thank you for the feedback.

> Peter Westwood wrote:
>> What is wrong with the following documentation that means it  
>> doesn't  explain how register_activation_hook works?
>> <current_docs>
>> activate_{plugin-filename}
>> activate_{plugin-filename} is triggered when a plugin file of  
>> that  name is activated. Instead of using it directly, use   
>> register_activation_hook($file, $function) where $fileis the  
>> plugin  file (don't hardcode it... use __FILE__) and $function is  
>> the  function you want to be run on plugin activation.
>> Context:
>> File: wp-admin/plugins.php
>> 	if ('activate' == $_GET['action']) {
>> 		$current = get_settings('active_plugins');
>> 		if (!in_array($_GET['plugin'], $current)) {
>> 			$current[] = trim( $_GET['plugin'] );
>> 			sort($current);
>> 			update_option('active_plugins', $current);
>> 			include(ABSPATH . 'wp-content/plugins/' . trim( $_GET 
>> ['plugin'] ));
>> 			do_action('activate_' . trim( $_GET['plugin'] ));
>> 		}
>> </current_docs>
>> It makes reasonable sense to me - if you have any feedback on  
>> making  it more meanful let me know and I will get it updated.
> Actually, this brief explanation had me quite confused when I tried  
> to use the hook some weeks earlier. One thing I do remember is,  
> that the given example is for a plugin file directly in the plugin  
> directory. My file was in a sub-directory and it took me a while to  
> figure out how to write this correctly.
> So, an example like the following could be helpful (correct me if I  
> get any of this wrong):
> For the hook directly:
> activate_{plugin-filename}  =>  activate_myplugin.php
> activate_{folder-name/plugin-filename}  =>  activate_myfolder/ 
> myplugin.php

This looks like good information to add.

> And an example for using the recommended function:
> register_activation_hook(plugin_basename(__FILE__), 'myfunction');

You should pass __FILE__ directly to register_activation_hook - it  
calls plugin_basename itself (http://trac.wordpress.org/browser/trunk/ 

I think the example on wphooks needs so more example code as well. I  
will see about getting it updated.

> I think, I had other problems in getting this to work, but I didn't  
> take notes and it's been some weeks... ;-)

It only works correctly for IIS on trunk at present ( due to issues  
with plugin_basename).

Peter Westwood <peter.westwood at ftwr.co.uk>
Blog: http://blog.ftwr.co.uk/
WordPress Plugins: http://blog.ftwr.co.uk/wordpress/

More information about the wp-hackers mailing list