php – 如何将变量传递给动作钩子函数?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何将变量传递给动作钩子函数?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个@L_777_2@将初始化我的wordpress主题中的图像滑块,但是我无法将 PHP变量传递给它.这是代码

function slideshowSettings ($pause_time) {
$code = "<script>
jquery(function(){
    jQuery('#camera_wrap_3').camera({
        height: '40%',thumbnails: true,time: ".$pause_time.",fx: '".$transITion_effect."',transPEriod: ".$transition_speed.",autoAdvance: ".$auto_advance.",minHeight: '50px',mobileNavHover: false,imagePath: '".get_template_directory_uri()."/images/'
    });
});
</script>";

echo $code;
}
add_action('wp_head','slideshowSettings');

变量分配在函数上方,但我从函数得到的输出如下所示:

<script>
jQuery(function(){

    jQuery('#camera_wrap_3').camera({
        height: '40%',time:,fx: '',transPeriod:,autoAdvance:,imagePath: 'http://www.brainbuzzmedia.COM/themes/simplybusiness/wp-content/themes/simplybusiness/images/'
    });
});
</script>

怎样才能传递这些变量?

解决方法

你不能为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 );

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何将变量传递给动作钩子函数?全部内容,希望文章能够帮你解决php – 如何将变量传递给动作钩子函数?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。