$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Using $variable in shortcode|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Using $variable in shortcode

matteradmin10PV0评论

I will use an $variable who is outside the function. In the shortcode function.

This is what i will do:

function shortcodevariable( $atts ){
    return 'echo $variable';
}
add_shortcode('variable', 'shortcodevariable');

I think we need an array but I dont now how, can somebody help?

Thank you very much.

I will use an $variable who is outside the function. In the shortcode function.

This is what i will do:

function shortcodevariable( $atts ){
    return 'echo $variable';
}
add_shortcode('variable', 'shortcodevariable');

I think we need an array but I dont now how, can somebody help?

Thank you very much.

Share Improve this question edited Mar 18, 2018 at 11:31 mmm 3,8893 gold badges16 silver badges22 bronze badges asked Mar 18, 2018 at 11:29 Jan WestdijkJan Westdijk 11 silver badge1 bronze badge 4
  • from where come the data ? edit your questions to give more details. – mmm Commented Mar 18, 2018 at 11:31
  • The data variable is included in the page.php in Wordpress – Jan Westdijk Commented Mar 18, 2018 at 11:33
  • It is for a wheather site, a Meteobridge uploads an php file with some $variable like: temperature, moisture, windspeed. This file with the $variable is included in page.php in Wordpress. Now I will shortcode the data in the wordpress pages. – Jan Westdijk Commented Mar 18, 2018 at 11:38
  • 1 It would probably be better to include that file directly inside the shortcode's callback function. Hard to say without an actual example of what's in that PHP file. It might provide global variables, functions or a class, that would all be used differently. – Jacob Peattie Commented Mar 18, 2018 at 11:45
Add a comment  | 

3 Answers 3

Reset to default 1

If you're using PHP > 5.3, then you can use a closure on the the_content filter. This filter needs to be added after the $variable has been defined and before the the_content filter has fired.

add_filter( 'the_content', function( $content ) use ( $variable ) {
  return str_replace( '[variable][/variable]', $variable, $content );
} );

Shortcodes are process by core on the_content hook at a priority of 11. So any priority 10 or less will be run before that. If you want the callback to be run before wpautop, use a priority less than 10.

There's no reason to add_shortcode() because this code replaces the shortcode with the variable before do_shortcode() is run.

Filters should ideally be placed in the themes functions.php file, but if for some reason $variable isn't available to functions.php, then this little hack should work.

I just discovered the solution to use external variables within shortcodes.

function show_shortcode_variable(){
    global $variable;
    return $variable;
}
add_shortcode('variable', 'show_shortcode_variable');

So if your $variable = "foo"; and someone types this in Wordpress:

[variable]

... Then they'll see this output on their page or post:

foo

Your code is mostly okay, but you don't need the echo at the end, and you need to define the variable as global within the function.

Does that help?

You have missing shortcode_atts and passing arguments in a shortcode. E.g

function shortcodevariable( $atts ){

   $data = shortcode_atts(              array(
                        'attribute-1' => '',
                        'attribute-2' => '',    
            ), $data            );

  return $data['attribute-1'];
}
add_shortcode('variable', 'shortcodevariable');

Usage

In a post, pages etc.

[variable attribute-1='okay']

In php file

echo do_shortcode ( "[variable attribute-1='<?Php echo $variable; ?>']" );

Recently I have created the post Create a simple shortcode in WordPress.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far