I am trying to include php files inside of my wordpress page by using shortcodes. Is there a way to include specific files or even entire folders just using shortcodes?
I found some code that potentially links to other files.
/* SHORTCODE TO HAVE FILES */
function sc_include($atts, $inc) {
return get_include_contents($inc);
}
// .trim removes wihtespace from both sides of a string
function get_include_contents($filename) {
ob_start();
// you can change the directory
$filename = get_theme_root().'/'.get_template().'/'.trim($filename);
if (is_file($filename)) {
@include($filename);
}
return ob_get_clean();
}
add_shortcode('include', 'sc_include');
Im a bit confused with get_theme_root() and get_template(). I may be wrong, but if get_theme_root shows the theme I am working with, why should I worry about a template? Is there some other way I could be able to showcase specific folders or files?
The shortcode runs a PHP function you define, which among other things can include files.
By the way, what your attached function does is include a file but wraps that in a call to ob_start
and ob_get_clean
thus returning the output as HTML.
get_theme_root()
gets the folder of all themes. Inside of it there's your theme. You could use another function(s) like get_template_directory_uri
to get the correct path to the required file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.