add_action()添加动作

功能:

add_action函数用来添加一个函数到指定的动作上,当指定的动作被调用的时候,这个函数也就被调用了。

用法:

<?php wp_head();?>

参数:

$hook

(字符串)(必须)挂载函数的动作名称。

默认值:None

$function_to_add

(回调函数)(必须)挂载的函数,直接填写字符串形式的函数名即可,如果你要挂载类函数可以参考:https://www.endskin.com/hook-object-function/

默认值:None

$priority

(整数)(可选)动作执行的优先级,数值越小越先被执行。

默认值:10

$accepted_args

(整数)(可选)回调函数接收几个参数。

默认值:1

返回值:

(布尔)始终返回 True.

所在位置:

add_action()函数包含在wp-includes/plugin.php中.

源码:

/**
 * Hooks a function on to a specific action.
 *
 * Actions are the hooks that the WordPress core launches at specific points
 * during execution, or when specific events occur. Plugins can specify that
 * one or more of its PHP functions are executed at these points, using the
 * Action API.
 *
 * @since 1.2.0
 *
 * @param string   $tag             The name of the action to which the $function_to_add is hooked.
 * @param callback $function_to_add The name of the function you wish to be called.
 * @param int      $priority        Optional. Used to specify the order in which the functions
 *                                  associated with a particular action are executed. Default 10.
 *                                  Lower numbers correspond with earlier execution,
 *                                  and functions with the same priority are executed
 *                                  in the order in which they were added to the action.
 * @param int      $accepted_args   Optional. The number of arguments the function accept. Default 1.
 * @return bool Will always return true.
 */
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
	return add_filter($tag, $function_to_add, $priority, $accepted_args);
}

示例:

function test_add_action() {
    echo '<meta name="keywords" content="关于我们" />';
}
add_action( 'wp_head', 'test_add_action' );

相关函数:

do_action()
remove_action()
add_filter()

(完)

版权声明WordPress头条正在使用WordPress文章版权插件
如未注明,均为原创,转载需注明出处
本文链接地址:http://www.wptoutiao.com/functions/26.html

动作的相关文章

钩子的相关文章

还可以分享文章给好友:

发表评论

电子邮件地址不会被公开。 必填项已用*标注