简体   繁体   中英

Pass variable in function parameter value Wordpress

Here is what I am trying to do:

In Wordpress, I am passing a variable from php to an external javascript file via wp_localize_script() and this part seems to be working, but I want to use the variables values as keyed parameters in a jquery function.

Using alert, the correct values seem to be passed to the javascript, but I don't know how to reference them inside the function's parameters. (Specifically WideSliderParams.slideshow and speed.)

Here's the JavaScript:

alert (WideSliderParams.slideshow);      // alerts false
alert (WideSliderParams.animationSpeed); // alerts 5

var speed = 1000 * WideSliderParams.animationSpeed;

alert( speed); //alerts 5000

jQuery('#featured-wide').flexslider({
    animation:'slide',
    controlNav:false,
    animationLoop:true,
    slideshow:WideSliderParams.slideshow,
    useCSS:false,
    smoothHeight:true,
    animationSpeed:speed,
    sync: "#featured-wide-thumbnav",
    manualControls: '.wide-custom-controls li a',
    controlsContainer: '.wide-slider',

    before: function(slider) { 
   Etc.

In case it matters, here is the piece from functions.php:

function wpbpp_wide_slider() {
  wp_enqueue_script( 'flex-script-main', get_stylesheet_directory_uri().'/js/flex-script-main.js', array( 'jquery' ), '1.0' );

  $wpbpp_options = get_option ('wpbpp_settings') ;

  $ss = $wpbpp_options['slideshow'];
  $as = $wpbpp_options['animationSpeed'];

  $wpbpp_slider_options = array (
                            'slideshow' => $ss,
                            'animationSpeed' => $as
                            );

  wp_localize_script('flex-script-main', 'WideSliderParams', $wpbpp_slider_options );

}

add_action('wp_enqueue_scripts', 'wpbpp_wide_slider');

OK, after some more testing, I do think it was a type problem.

The value I was passing to the js coming out of my html/php form was a string "true" or "false", and both were being read as boolean true .

Now that I am casting them as boolean, the function seems to work. Thanks all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM