Viewing 6 replies - 1 through 6 (of 6 total)
  • No, you should use simply

    register_activation_hook( __FILE__, 'my_callback_function' );

    or

    add_action('action_'.plugin_basename(__FILE__), 'my_callback_function');

    Thread Starter ace0930

    (@ace0930)

    @threadi You should look at the documentation ( https://developer.www.ads-software.com/reference/functions/register_activation_hook/ ).

    The hook should be activate_sampleplugin/sample.php, and ‘plugin_basename(__FILE__)’ will return only the file name ( sample.php ) without the folder name ( sampleplugin ).

    And that’s the question I was asking, why can’t I just use add_action( 'activate_' . basename(__DIR__) . '/' . basename(__FILE__), 'my_callback_function' ); instead of ‘register_activation_hook( __FILE__, ‘my_callback_function’ );’?

    I have explained in detail that my suggestion is better because it is shorter.

    You could do it, yes. If it works for you, it’s fine. There are several paths to the goal here. However, the 2 examples I gave are much shorter and more readable than your line – I think.

    Thread Starter ace0930

    (@ace0930)

    @threadi I’ll not consider the original method register_activation_hook because it uses the plugin_basename which does a lot more than my/your suggestion ( if they work ).

    May I know why 'action_' . plugin_basename(__FILE__) works?

    plugin_basename(__FILE__) will return only the file name, right?

    WordPress expects to have the folder name too according to the documentation?

    Do I misunderstood the doc or you are missing something?

    Just try to output it via var_dump(), then you can see what happens:

    var_dump(__FILE__);

    Output:
    string(98) "/var/www/clients/client0/web1/web/wp-content/plugins/audio-on-every-block/audio-on-every-block.php"

    And:
    var_dump(basename(__DIR__) . '/' . basename(__FILE__));

    Output:
    string(45) "audio-on-every-block/audio-on-every-block.php"

    And:
    var_dump(plugin_basename(__FILE__));

    Output:
    string(45) "audio-on-every-block/audio-on-every-block.php"

    And 'action_' . plugin_basename(__FILE__) is exactly what happens inside the register_activation_hook() function, you’ve linked the manual with the code several times now:

    $file = plugin_basename( $file );
    add_action( 'activate_' . $file, $callback );

    Shorthand would be exactly what I mentioned above:

    add_action( 'activate_' . plugin_basename( $file ), $callback );

    EDIT: just examples with my own plugin as name ??

    • This reply was modified 2 years, 4 months ago by threadi.
    Thread Starter ace0930

    (@ace0930)

    @threadi Sorry because I misread plugin_basename( $file ) as basename(__FILE__)

    But I think you still misunderstood my point, I’m not comparing plugin_basename( $file ) with basename(__DIR__) . '/' . basename(__FILE__), of course the former is shorter.

    I’m looking at what plugin_basename does and it’s so much longer than my code basename(__DIR__) . '/' . basename(__FILE__) if you look at the source.

    Below is the full code of plugin_basename:

    
    function plugin_basename( $file ) {
    	global $wp_plugin_paths;
    
    	// $wp_plugin_paths contains normalized paths.
    	$file = wp_normalize_path( $file );
    
    	arsort( $wp_plugin_paths );
    
    	foreach ( $wp_plugin_paths as $dir => $realdir ) {
    		if ( strpos( $file, $realdir ) === 0 ) {
    			$file = $dir . substr( $file, strlen( $realdir ) );
    		}
    	}
    
    	$plugin_dir    = wp_normalize_path( WP_PLUGIN_DIR );
    	$mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
    
    	// Get relative path from plugins directory.
    	$file = preg_replace( '#^' . preg_quote( $plugin_dir, '#' ) . '/|^' . preg_quote( $mu_plugin_dir, '#' ) . '/#', '', $file );
    	$file = trim( $file, '/' );
    	return $file;
    }
    

    So comparing the above code with my code basename(__DIR__) . '/' . basename(__FILE__), which one is shorter? LOL

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Alternative of register_activation_hook’ is closed to new replies.