Reputation: 335
Something that I just can't understand is how the Wordpress functions are loaded in. When I am adding an action to a Wordpress hook like this.
<?php
function test() {
echo "Test";
}
add_action('wp_enqueue_scripts','test');
?>
This code works for what I want to do but where is this add_action function coming from. I know Wordpress is taking care of it somehow but I just don't understand how I am able to call it without actually including a file. I tried to include the file in another file that would be included before this file but then I would get a function that is not defined error. I just really want to know the logic behind this.
Upvotes: 1
Views: 128
Reputation: 95
It's work like, the first file that runs on open WordPress site is index.php which required wp-blog-header.php
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
then wp-blog-header.php require wp-load.php and template-loader.php
// Load the WordPress library.
require_once( dirname(__FILE__) . '/wp-load.php' );
// Set up the WordPress query.
wp();
// Load the theme template.
require_once( ABSPATH . WPINC . '/template-loader.php' );
here wp-load.php file require wp-config.php
if ( file_exists( ABSPATH . 'wp-config.php') ) {
/** The config file resides in ABSPATH */
require_once( ABSPATH . 'wp-config.php' );
}
and wp-config.php file require wp-settings.php
require_once(ABSPATH . 'wp-settings.php');
and wp-settings loads wp-includes/plugin.php file
define( 'WPINC', 'wp-includes' );
// Include files required for initialization.
require( ABSPATH . WPINC . '/load.php' );
require( ABSPATH . WPINC . '/default-constants.php' );
require_once( ABSPATH . WPINC . '/plugin.php' );
and wp-includes/plugin.php file have add_action function in it
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
return add_filter($tag, $function_to_add, $priority, $accepted_args);
}
and wp-includes/template-loader.php load the theme template.
require_once( ABSPATH . WPINC . '/template-loader.php' );
Upvotes: 1
Reputation: 252
You will find this function initialized in wp-includes/plugin.php :
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
return add_filter($tag, $function_to_add, $priority, $accepted_args);
}
You can use the function wherever this file is included.
Wordpress has this documentation and more @ https://developer.wordpress.org/reference/functions/add_action/
Upvotes: 1