Last active
September 30, 2022 12:37
-
-
Save germanny/9420496 to your computer and use it in GitHub Desktop.
Shortcode to include files in WP Pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// SHORTCODE - Include File | |
// http://www.amberpanther.com/knowledge-base/using-the-wordpress-shortcode-api-to-include-an-external-file-in-the-post-content/ | |
function include_file($atts) { | |
//check the input and override the default filepath NULL | |
//if filepath was specified | |
extract(shortcode_atts(array('filepath' => 'NULL'), $atts)); | |
//check if the filepath was specified and if the file exists | |
if ($filepath!='NULL' && file_exists(CHILD_SS_DIR.$filepath)){ | |
//turno on output buffering to capture script output | |
ob_start(); | |
//include the specified file | |
include(CHILD_SS_DIR.$filepath); | |
//assign the file output to $content variable and clean buffer | |
$content = ob_get_clean(); | |
//return the $content | |
//return is important for the output to appear at the correct position | |
//in the content | |
return $content; | |
} | |
} | |
//register the Shortcode handler | |
add_shortcode('include', 'include_file'); | |
?> |
Sorry, if you can explain to a newbie, what part of the code do I need to insert this fragment of [include filepath='/child-theme/templates/my-file.php?'] Ich bin ein großer Idiot!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use like this: