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

时间:2012-11-08 04:39:20

标签: php jquery wordpress action hook

我有一个函数可以初始化我的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%',
        thumbnails: true,
        time: ,
        fx: '',
        transPeriod: ,
        autoAdvance: ,
        minHeight: '50px',
        mobileNavHover: false,
        imagePath: 'http://www.brainbuzzmedia.com/themes/simplybusiness/wp-content/themes/simplybusiness/images/'
    });
});
</script>

我如何传递这些变量?

3 个答案:

答案 0 :(得分:3)

您无法向wp_head添加参数,因为do_action('wp_head');函数调用wp_head()时,没有任何参数传递给您的钩子函数。 add_action()的参数是

  1. 要挂钩的操作,在您的情况下为“wp_head”
  2. 您要执行的功能,在您的情况下为“slideshowSettings”
  3. 执行的优先级,默认为10
  4. 您的函数接受的参数数量(这些参数必须由do_action传递)
  5. 如果您需要能够将钩住函数外部的这些值传递给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 );
    

答案 1 :(得分:3)

将PHP变量传递给Javascript的正确方法是使用wp_localize_script

检查article by Otto。这是Q&A at WordPress StackExghange


在你的问题中,你没有显示这些变量的来源,因此它们正在打印......没有。

我刚做了一个测试。这将在数​​据库中创建选项值。

add_action( 'init', 'so_13282503_init' );

function so_13282503_init() 
{
    if( !get_option('pause_time') )
    {
        update_option('pause_time', '50');
        update_option('transition_effect', 'elastic.easeIn');
        update_option('transition_speed', '400');
        update_option('auto_advance', '4');
    }
}

这是您的代码的改编版本:

add_action( 'wp_head', 'slideshowSettings' );

function slideshowSettings () 
{
    $pause_time = get_option('pause_time');
    $transition_effect = get_option('transition_effect');
    $transition_speed = get_option('transition_speed');
    $auto_advance = get_option('auto_advance');
    $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
        });
    });
</script>
";

    echo $code;
}

这导致将其打印在网站的<head>中:

result of code in the rendered html

答案 2 :(得分:1)

您可以在add_action()

中的优先级参数之后传递参数数量
 <?php add_action( $tag, $function_to_add, $priority, $accepted_args ); ?> 

wordpress codex有一些关于它的例子。

您应该注意,您的函数实际上仅使用参数,并且您在该函数中使用了未定义的变量,例如$transition_effect$transition_speed$auto_advance