Yes the questions sounds stupid, I am new to Plugins. I viewed into other plugins to solve it, but they are too complicated, when you are new.
All I want is to call a function on a WordPress page /example using a shortcode. The called function should use sub files where other functions are defined.
<?php
/*
* Plugin Name: My Plugin
* Plugin URI: https://example.com
* Description: A brief description of my plugin
* Version: 1.0.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: my-plugin
* Domain Path: /languages
*/
function iaAddScripts() {
wp_enqueue_style('my-plugin-style', plugins_url('css/iaStyle.css', __FILE__));
wp_enqueue_script('my-plugin-script', plugins_url('js/iaScript.js', __FILE__));
}
add_action('wp_enqueue_scripts', 'iaAddScripts');
function my_shortcode_function() {
require (plugin_dir_path( __FILE__ ) . '/functions/nameToCall.php');
//return "<p class='testRed'>Hello world!</p>"; //THIS WORKS AND SHOWS Hello World! ON THE PAGE
}
add_shortcode('my_shortcode', 'my_shortcode_function');
Here the content of the nameToCall.php
<?php
function myFunc(){
return "<p class='testRed'>Hello world 2!!</p>";
}
myFunc();
I would expect, that the function myFunc returns "Hello world 2!!" on the page /demo. But it does not. The text is only shown, when I return it in the main file. (Here with // disabled)
So for me is totally unclear how I can put functions in sub files and show text from there.
Thank you. Chris
CodePudding user response:
This can be done via:
bar.php
<?php
function bar(){
return 'bar';
}
return bar();
<?php
function foo () {
return include 'bar.php';
}
var_export(foo());
CodePudding user response:
To display the output of a function on a WordPress page using a shortcode, you need to call the function inside the shortcode function and return its output.
Here's how you can modify your code to achieve this:
<?php
/*
* Plugin Name: My Plugin
* Plugin URI: https://example.com
* Description: A brief description of my plugin
* Version: 1.0.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: my-plugin
* Domain Path: /languages
*/
function iaAddScripts() {
wp_enqueue_style('my-plugin-style', plugins_url('css/iaStyle.css', __FILE__));
wp_enqueue_script('my-plugin-script', plugins_url('js/iaScript.js', __FILE__));
}
add_action('wp_enqueue_scripts', 'iaAddScripts');
function my_shortcode_function() {
require (plugin_dir_path( __FILE__ ) . '/functions/nameToCall.php');
// Call the function from the included file and return its output
return myFunc();
}
add_shortcode('my_shortcode', 'my_shortcode_function');
And here's the content of the nameToCall.php file:
<?php
function myFunc(){
return "<p class='testRed'>Hello world 2!!</p>";
}
Now, when you use the [my_shortcode] shortcode in your WordPress page, it will display the output of the myFunc() function, which is "Hello world 2!!".
I hope this helps! Let me know if you have any further questions.