官术网_书友最值得收藏!

Referencing hooks via add_action() and add_filter()

The final vital component in a WordPress plugin is the hook, which defines when the plugin is executed. This is arguably the most confusing component of a plugin, so we will be thorough in our explanations. Just as in pop-music, the term "hook" is sometimes ambiguous—different people use the term to refer to different things. Technically, the term "hook" should refer to a WordPress event, such as get_header or the_content, but sometimes it is used generally to refer to the add_action() or add_filter() functions which reference the hook. Pay attention to the context, and it should be clear which meaning was intended. The most important thing to understand here is that you determine when your functions execute by attaching them to a WordPress event by using the add_action() or add_filter() functions. Remember: hooks are events.

The syntax for both functions is exactly the same. We'll discuss the reasoning for this shortly, but for now, just compare the two:

add_filter( string $hook, mixed $your_function_name [, int $priority = 10 [, int $accepted_args = 1]])

versus

add_action( string $hook, mixed $your_function_name [, int $priority = 10 [, int $accepted_args = 1]])

In practice, the most common usage includes only the name of the WordPress event and the name of your function, such as:

add_action('admin_footer', 'hello_dolly');

In "Hello Dolly", admin_footer is the action or event, and hello_dolly is the name of the function which we want WordPress to run when the admin_footer event occurs. Note that we have intentionally digressed from WordPress' official terminology for the sake of clarity.

主站蜘蛛池模板: 靖西县| 正安县| 芮城县| 锡林郭勒盟| 中山市| 麻阳| 姜堰市| 友谊县| 林周县| 蛟河市| 孟连| 宜都市| 灯塔市| 虞城县| 尉犁县| 三穗县| 穆棱市| 巫山县| 敦化市| 台州市| 南雄市| 疏勒县| 子长县| 商都县| 巴楚县| 昭苏县| 赞皇县| 敦煌市| 大丰市| 和静县| 济宁市| 通河县| 沙河市| 奈曼旗| 滕州市| 南开区| 卓尼县| 西丰县| 巢湖市| 福海县| 锦州市|