How to pass a variable to an action function?

I have a function that initializes the image slider in my WordPress theme, however I cannot get PHP variables to be passed to it. Here is the code:

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'); 

Variables are assigned over the function, but the output I get from the function looks like this:

 <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> 

How to pass these variables?

+4
source share
3 answers

You cannot add arguments to wp_head because none of them are passed to your connected function when do_action('wp_head'); called by wp_head() . The arguments for add_action() are

  • Action to connect to your wp_head case
  • The function you want to execute is in your case "slideshowSettings"
  • Execution Priority, default 10
  • The number of arguments your function takes (they should only be passed do_action )

If you need to pass these values ​​outside of your connected function before wp_head , I would use apply_filters to change the value:

 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 ); 
+3
source

The correct way to pass PHP variables to Javascript is using wp_localize_script .

Mark Otto's article . And this is Q&A in WordPress StackExghange .


In your Question, you are not showing where these variables come from, so they print ... nothing.

I just did a test. This creates parameter values ​​in the database.

 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'); } } 

And this is an adapted version of your code:

 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; } 

As a result, this is printed in the <head> site:

result of code in the rendered html

+3
source

You can pass the number of arguments after the priority argument to add_action ()

  <?php add_action( $tag, $function_to_add, $priority, $accepted_args ); ?> 

Wordpress codex has a few examples.

You should notice that your function only accepts an argument, and you use undefined variables such as $transition_effect , $transition_speed and $auto_advance in this function.

+1
source

Source: https://habr.com/ru/post/1444723/


All Articles