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
1 Answer
Reset to default 1One 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?