I'm new to Wordpress development (and php in general) so I'm confused about the execution of functions in my plugin's php file. I've seen multiple guides that show to create a register_activation_hook
function, that is executed when the plugin is activated. However, after the activation hook function, there is always an add_action('init')
call that adds a bunch of options to the database.
So my question is: when Wordpress evaluates my plugin's php file, and finds the register_activation_hook
call, why does it not execute the add_action('init')
call until after the plugin is activated? If Wordpress evaluates my plugin's php file, and there is a function call to add_action('init')
in the file, it seems like that function call would execute when the page is evaluated, because it's not inside a control structure.
I know Wordpress is evaluating this file before it's activated, because it knows my plugin name, and author, and all that before it's activated. So if it's evaluating the file, why is it not executing the add_action('init')
call?
Does my question make sense? Can anyone help me to understand why things are working this way?
Here's an example of what I'm talking about:
register_activation_hook(__FILE__, 'halloween_store_install');
function halloween_store_install() {
$hween_options_arr = array(
'currency_sign' => '$'
);
//save our default option values
update_option( 'halloween_options', $hween_options_arr );
}
add_action( 'init', 'halloween_store_init' ); // <-- why is this not executed until my plugin is activated?
function halloween_store_init(){
$labels = array(
'name' => __( 'Products', 'halloween-plugin' ),
'singular_name' => __( 'Product', 'halloween-plugin' ),
'add_new' => __( 'Add New', 'halloween-plugin' ),
'add_new_item' => __( 'Add New Product', 'halloween-plugin' ),
'edit_item' => __( 'Edit Product', 'halloween-plugin' ),
'new_item' => __( 'New Product', 'halloween-plugin' ),
'all_items' => __( 'All Products', 'halloween-plugin' ),
'view_item' => __( 'View Product', 'halloween-plugin' ),
'search_items' => __( 'Search Products', 'halloween-plugin' ),
'not_found' => __( 'No products found', 'halloween-plugin' ),
'not_found_in_trash' => __( 'No products found in Trash', 'halloween-plugin' ),
'menu_name' => __( 'Products', 'halloween-plugin' )
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_ui_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' )
);
register_post_type( 'halloween-products', $args);
}
add_action('init')
call is in the same file as my plugin name and theregister_activation_hook
call, why is it not getting called when the rest of the file is being evaluated?plugins_loaded
and not automatically via the include the file.add_action
function is not run while being in the same file asregister_activation_hook
. Because nothing is run at the include level in WP, everything depends on the hook sequence. So basically it is evaluated when PHP reads that file, but it will be executed when the hook is called. This makes it possible to build plugins and call functions conditionally and also respect a certain sequence logic in the application.add_action
,add_filter
andregister_activation_hook
(and others) are PHP'scall_user_func
but with some added context so it's easier to understand what is being called and when. Hope this makes more sense to you