I have add this code to functions.php but startwordpress_scripts() function didn't run.
function startwordpress_scripts() {
wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css', array(), '3.3.6' );
wp_enqueue_style( 'blog', get_template_directory_uri() . '/css/blog.css' );
wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '3.3.6', true );
}
add_action( 'wp_enqueue_scripts', 'startwordpress_scripts' );
So I added following code in header.php and they worked well.
<?php wp_head(); ?>
Now I want to know the magic role of wp_head() function. Thanks.
If you remove wp_head() function then all action add_action('wp_head','your_custom_action'); is not working also plugin also use wp_head action action to add css or js.
If you remove wp_head function from header.php file then below function not add any JS in header.
If you want to remove wp_head() function then you need to add like below.
See Link for wp_enqueue_script function and args
There are no errors in your code shown above and I've double checked to see it working correctly in a fresh WP install. It should not be the cause of your errors.
Take a look at the DEBUG section of the codex to change that. This might need to be dropped into your wp-config.php.
In WordPress,
actions
andfilters
are considered as hooks. Hooks allow us to modify the WordPress default behaviour without modifying the code found in the core.Anytime you have an
add_action('xxx', 'callback')
, thecallback
function will be called whendo_action('xxx')
is executed.In other words: when you have
add_action( 'wp_enqueue_scripts', 'startwordpress_scripts' );
, it means that WordPress will runstartwordpress_scripts()
whendo_action('wp_enqueue_scripts')
is executed.Now, let's look at the code in the WordPress core.
If you look at its function definition,
wp_head()
is a "shortcut" todo_action( 'wp_head' );
In other words,
wp_head()
will execute all callbacks that were defined withadd_action('wp_head')
.If you now look at the
wp-includes/default-filters.php
file, you'll see:It means that when
wp_head()
is encountered in your template, a function calledwp_enqueue_scripts()
is being executed, since it is hooked towp_head
, shown in the line of code above.the function definition of
wp_enqueue_scripts()
is:The
do_action( 'wp_enqueue_scripts' );
above is what tells WordPress to execute the callback function of alladd_action('wp_enqueue_scripts', 'callback')
that are defined.In short:
wp_head()
callsdo_action('wp_head')
do_action('wp_head')
executes callback functions of alladd_action('wp_head','callback')
wp_enqueue_scripts()
is one callback ofadd_action('wp_head','callback')
wp_enqueue_scripts()
callsdo_action('wp_enqueue_scripts')
do_action('wp_enqueue_scripts')
executes callback functions of alladd_action('wp_enqueue_scripts','callback')
startwordpress_scripts()
is one callback ofadd_action('wp_enqueue_scripts','callback')
startwordpress_scripts()
are included