你不能为wp_head添加参数,因为当do_action(‘wp_head’)时,没有任何参数传递给你的钩子函数;由wp_head()函数调用. add_action()的参数是
>挂钩的动作,在你的情况下“wp_head”
>您要执行的功能,在您的情况下“幻灯片设置”
>执行的优先级,默认值为10
>函数接受的参数数量(但必须通过do_action传递)
如果你需要能够将钩子函数外部的这些值传递给wp_head,我会使用apply_filters来修改一个值:
function slideshowSettings(){
// set up defaults
$settings = array('pause_time'=>10, 'other'=>999);
$random_text = "foo";
// apply filters
$settings = apply_filters('slideshow_settings', $settings, $random_text);
// set array key/values to variables
extract( $settings );
// will echo 1000 because value was updated by filter
echo $pause_time;
// will echo "foobar" because key was added/updated by filter
echo $random_text;
// ... more code
}
add_action( 'wp_head', 'slideshowSettings' );
function customSettings($settings, $random_text){
// get your custom settings and update array
$settings['pause_time'] = 1000;
$settings['random_text'] = $random_text . "bar";
return $settings;
}
// add function to filter, priority 10, 2 arguments ($settings array, $random_text string)
add_filter( 'slideshow_settings', 'customSettings', 10, 2 );
本文介绍如何在WordPress wp_head钩子函数中通过apply_filters传递参数,并演示了如何创建一个名为'slideshow_settings'的自定义设置函数,允许外部传递值并动态更新。通过customSettings函数,可以定制幻灯片设置,如暂停时间和随机文本。

9755

被折叠的 条评论
为什么被折叠?



