Skip to content

Instantly share code, notes, and snippets.

@germanny
Last active September 30, 2022 12:37
Show Gist options
  • Save germanny/9420496 to your computer and use it in GitHub Desktop.
Save germanny/9420496 to your computer and use it in GitHub Desktop.
Shortcode to include files in WP Pages
<?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');
?>
@germanny
Copy link
Author

germanny commented Mar 7, 2014

Above code is for Genesis Child Theme. Could use TEMPLATEPATH or whatever for regular theme.

@germanny
Copy link
Author

germanny commented Mar 7, 2014

use like this:

[include filepath='/includes/inc-test.php']

@1968-LT
Copy link

1968-LT commented Apr 29, 2019

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