$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'); ?>variables - Plugin. Html code in Template|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)

variables - Plugin. Html code in Template

matteradmin7PV0评论

perhaps i am blind or something but i searched alot, here and also wordpress codex and similar. In example i wrote a plugin that reads something from database and print someting. But how can i archive that i dont output in the plugin itself? The idea was put the variables to the theme and do the rest in the theme. Is this only possible with do_shortcode when you dont wanna use global variables? When i have 8 variables in example, how can i do that with a single shortcode? As i said, perhaps i am blind and didnt find the right thing. Or what is the best practice to do that simple thing? Perhaps someone have a small code example?

Regards and thanks

perhaps i am blind or something but i searched alot, here and also wordpress codex and similar. In example i wrote a plugin that reads something from database and print someting. But how can i archive that i dont output in the plugin itself? The idea was put the variables to the theme and do the rest in the theme. Is this only possible with do_shortcode when you dont wanna use global variables? When i have 8 variables in example, how can i do that with a single shortcode? As i said, perhaps i am blind and didnt find the right thing. Or what is the best practice to do that simple thing? Perhaps someone have a small code example?

Regards and thanks

Share Improve this question asked Oct 20, 2018 at 9:27 BurnhardoBurnhardo 111 bronze badge 2
  • What exactly is the problem in retrieving 8 variables and displaying them via shortcode? You need to be more specific. Since you can use shortcodes in both the editor and your template files AND you can modify the output using arguments, it's usually a pretty flexible solution, in my opinion. – Hans Commented Oct 20, 2018 at 12:04
  • What do you mean by "put the variables to the theme"? – Eduardo Escobar Commented Oct 20, 2018 at 13:44
Add a comment  | 

1 Answer 1

Reset to default 1

One way to do that is to define a function, which either echos or returns stuff, in your plugin file and then use that function in your theme files when needed.

For example in your my-awesome-plugin.php

function hello_world() {
    echo 'Hello world!';
}

And then in your theme file, let's say single.php

// safety check if your plugin ever gets disabled
if ( function_exists( 'hello_world' ) ) {
    hello_world();
}

This would result in Hello world! on your single post.

Then it's up to you to decide what kind of stuff you want to return or echo with the function you've defined in the plugin file. It could be a string of html, integer, array, object, whatnots...

Do these examples help you with your situation?

EDIT Now as I read again your question right after posting, I realized that I might have misunderstood it and answered it improperly.

Perhaps you can define a class in your plugin file and store data in it. Then have a function return single instance of it. Same way that WooCommerce does it.

From https://github/woocommerce/woocommerce/blob/master/woocommerce.php

function wc() {
    return WooCommerce::instance();
}

Regarding class initialization there's good answers here, Best way to initiate a class in a WP plugin?

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far