0

In Wordpress, lets say i have a custom Shortcode function, like:

function foobar_func( $atts ){
    //whatever required processings here...
    $foo = "the value is foo!";
    $bar = "the value is bar!";
}
add_shortcode( 'myshortcode', 'foobar_func' );
//now .. echo the value of $foo here? <----------

As you can see, how can i get the processed value of a variable out from the function please? (as in the last line)

OR

How to pass the variable from this Function to the outside, please?

  • I think you're doing something wrong here, you want to echo the value directly in your functions.php file, before you render the theme? – birgire Apr 14 '14 at 10:11
  • Its not even in the functions.php, but just in the Template File. Whatever, i just need to do processing with the Shortcode (in the backend) instead of even printing the things out to Display. (In short, i need to get the value of a variable inside the function, from the outside of it.) – 夏期劇場 Apr 14 '14 at 10:22
  • Wrap it in a class and use $this->foo to access it within the class or through a getter to access it externally. – birgire Apr 14 '14 at 10:34
  • hi @birgire can you give/code it as an answer please? Cos i still do not get what to do :( – 夏期劇場 Apr 14 '14 at 10:44
  • But can you use the callback function directly? Why do you need a shortcode if you're not using it? Can you perhaps explain it a little bit more what you are trying to achieve - the big picture? – birgire Apr 14 '14 at 11:07
1

There are a lot of ays to do the trick, one possible is use a function that use a static variable.

However, before you can be able to get the arguments set in shortcode, the shortcode must be processed...

function foobar_func( $atts = array(), $out = FALSE ){
   static $args = array(
     'foo' => 'default foo',
     'bar' => 'default bar'
   );
   if ( $out ) return $args;
   $args = shortcode_atts( $args, $atts, 'myshortcode' );
   echo 'The value for "foo" argument set in shortcode is: ' . $args['foo'] . '<br>';
   echo 'and the value for "bar" argument set in shortcode is: ' . $args['bar'];
}
add_shortcode( 'myshortcode', 'foobar_func' );

And after the shortcode is processed, you can get all the arguments calling again the function with second arguments set to true:

$shortcode_args = foobar_func( NULL, TRUE );

If called before the shortcode is processed it always return default values.

Another way, probably more reliable, is to trigger a custom action, and use it to pass the shortcode arguments:

function foobar_func( $atts = array() ){
   $defaults = array(
     'foo' => 'default foo',
     'bar' => 'default bar'
   );
   $args = shortcode_atts( $defaults, $atts, 'myshortcode' );
   echo 'The value for "foo" argument set in shortcode is: ' . $args['foo'] . '<br>';
   echo 'and the value for "bar" argument set in shortcode is: ' . $args['bar'];
   // custom action
   do_action( 'myshortcode_processed', $args );
}
add_shortcode( 'myshortcode', 'foobar_func' );

and then hook the action to get arguments and use them:

add_action( 'myshortcode_processed', function( $shortcode_args ) {
  // do whatever you want with $shortcode_args
  var_dump( $shortcode_args );
});
  • Solution #1 is (potentially) Doing It Wrong. From the add_shortcode() Codex page: "Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results." – Pat J Apr 14 '14 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.