I am a beginner with WordPress. Studying hook, add_action() and add_filer() and see in the documentation that
add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
But I dont know how to get $tag and where to get it to fill into add_action() or add_filter() function? Thank you.
I am a beginner with WordPress. Studying hook, add_action() and add_filer() and see in the documentation that
add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
But I dont know how to get $tag and where to get it to fill into add_action() or add_filter() function? Thank you.
Share Improve this question asked Sep 16, 2017 at 11:01 Ngan NguyenNgan Nguyen 111 silver badge2 bronze badges 3 |2 Answers
Reset to default 1These 2 function add_action()
and add_filter()
are magics of WP and do not directly affect your code. In WordPress, they use hooks
for mocking into existing functions, methods without changing it's code.
The $tag
you are referring is the name of the hook that you want to use. List of core hooks can be found here https://codex.wordpress/Plugin_API/Hooks. However you can easily create for own hooks. E.g.:
- When you create a theme, and you want to allow others to add some additional HTML after your header, you may create this snippet:
in functions.php (or in you lib)
function get_custom_header($tag){
get_header($tag);
do_action('after_template_header', $tag); // Add $tag as param, this $tag is not
}
in index.php (in theme) or in any theme file
<?php get_custom_header(); // instead of get_header(); ?>
For hooking, others need to add this: ($tag
you are asking is 'after_template_header' for this case)
add_action('after_template_header', 'hook_to_header', 10, 1); // 4th param tell WP to use 1 arguments, if you put it 0, you will not need to pass $tag to function
function hook_to_header($tag) {
echo "After header with tag '$tag' text here";
}
Hope this can help you learning WP well my fellow. Good understand on hooks will help you to do advanced development on WP.
You are asking how to replace your $tag
in
add_action
function.
Actually the $tag
can replace with Hooks ( terminology used by WordPress )
and in function add_action()
they write general syntax and use tag instead of Hook. These hooks have Names which are String type and can be replaced according to your need.
Here is a Hook after_header_theme which can be used as a tag in add_action() as
add_action('after_setup_theme','title_helper');
after_setup_theme
is a Hook provided by WordPress ($tag
according to function syntax )title_helper
is custom function
Means upon setting up the theme title_helper
function will call and you can fill title_helper
function according to your need.
$tag
is just a variable, nothing more. You can also think of it as "first argument passed toadd_action()
". So when the text references$tag
, it means the first argument .. It often helps (me) to see the code examples where this is used, likeadd_action('init', 'wpse_my_init_function');
, etc. Anyway, this is probably too broad/off topic for this plattform – kero Commented Sep 16, 2017 at 11:29