1

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.

3
  • $tag is just a variable, nothing more. You can also think of it as "first argument passed to add_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, like add_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
  • Thank you. And in your example, it is 'init'. But how can I get 'init' value? I mean I can find it it which file? or it is a function in somewhere? Commented Sep 16, 2017 at 11:33
  • You just set it, there are many standard hooks/filters and good plugins include them as well. I really recommend you read more up on WP (either in the doc or using a tutorial online)
    – kero
    Commented Sep 16, 2017 at 11:34

2 Answers 2

1

These 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.org/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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.