[wp-hackers] Setting default options for plugin

Johannes Ruthenberg johannes at bolarus.de
Sat Sep 1 13:52:01 GMT 2007


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. ;-)

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

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

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

Greetings,
Johannes



More information about the wp-hackers mailing list