Here is what I have
I am not having any lucking finding how to simply include a file in the content editor using a shortcode.
For Example if I wanted to include form.php inside my Contact Page how would I make this happen using a shortcode?
Below is was attempting to work with to no avail.
Any help would be appreciated!
// Shortcode implementation
function magic_stuff($atts) {
// turn on output buffering to capture script output
ob_start();
// include file (contents will get saved in output buffer)
include(TEMPLATEPATH.'/wp-content/themes/grainandmortar/inc_static/test.php');
// save and return the content that has been output
$content = ob_get_clean();
return $content;
}
//register the Shortcode handler
add_shortcode('magic', 'magic_stuff');
I modified some code from an old blog post to do this, and allow for query-strings to be attached to the file as well.
Original credit goes to amberpanther.com, and it turns out they made a plug-in out of this, too.
I set mine to pull from the stylesheet uri (so it will work with child themes and such), but you could adjust that code easily to pull from anywhere (including plug-in directories), or remove it altogether and just use the full path when including the file. You could even add a conditional with a trigger character at the beginning that tells it to use a specific path based on what the first character of the file name is, like using a “#” for the template directory, etc.
I use it to pull in a file called get-posts.php that lives in my template directory and formats the output of various posts based on a series of parameters provided in the query string. It saves me from needing special templates because I can include the file, send it a format and it will output the posts with the markup I’ve specified in the get-posts.php file.
It also allows clients to pull custom post types into actual blog-posts in specific formats and is quite handy.
Let me know if you need clarification on anything.
Here’s another way to do it, utilizing
get_template_part
of wordpressexamples:
[include slug="form"]
[include slug="sub-folder/filename_without_extension"]
There is an error in the solution provided by @adedoy, since $slug is never defined. This worked for me:
Usage: [include path=”path/from/root”]
Ok, first I’d ditch the output buffering.
Second change:
To
Finally,
Reading the documentation here: https://codex.wordpress.org/Shortcode_API
You need to return something, if your test.php doesn’t output something in a returnable fashion, you’re going to have a bad time.
So make sure that test.php does something along the lines of:
Then after you include your test.php file — you just return
$output
or do something likereturn test();
.I found that the include code originally written by the AmberPanther folks didn’t work so well for me, so I found another wordpress plugin that does pretty much the same thing. It’s called Include Me, by Stefano Lissa. The usage is pretty much that you write your path to the file, starting from the root directory of your site.
So for example, within your WordPress page you’d write:
and within your functions.php file you’d include:
of course, I’d also recommend just installing the plugin so you can take advantage of updates. https://wordpress.org/plugins/include-me/