Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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');
share|improve this question
    
Is there any particular reason that you're using output buffering? –  Chip Bennett Apr 20 '12 at 15:43
    
Chip- No reason it was in the snippet I found so I assumed it was required. –  Eric Downs Apr 20 '12 at 17:14

2 Answers 2

up vote 1 down vote accepted

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.

//create the shortcode [include] that accepts a filepath and query string
//this function was modified from a post on www.amberpanther.com you can find it at the link below:
//http://www.amberpanther.com/knowledge-base/using-the-wordpress-shortcode-api-to-include-an-external-file-in-the-post-content/
//BEGIN amberpanther.com code
function include_file($atts) {
     //if filepath was specified
     extract(
          shortcode_atts(
               array(
                    'filepath' => 'NULL'
               ), $atts
          )
     );

     //BEGIN modified portion of code to accept query strings
     //check for query string of variables after file path
     if(strpos($filepath,"?")) {
          $query_string_pos = strpos($filepath,"?");
          //create global variable for query string so we can access it in our included files if we need it
          //also parse it out from the clean file name which we will store in a new variable for including
          global $query_string;
          $query_string = substr($filepath,$query_string_pos + 1);
          $clean_file_path = substr($filepath,0,$query_string_pos);     
          //if there isn't a query string
     } else {
          $clean_file_path = $filepath;
     }
     //END modified portion of code

     //check if the filepath was specified and if the file exists
     if ($filepath != 'NULL' && file_exists(get_stylesheet_directory_uri() . "/" . $clean_file_path)){
          //turn on output buffering to capture script output
          ob_start();
          //include the specified file
          include(TEMPLATEPATH.$clean_file_path);
          //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');
//END amberpanther.com code
//shortcode with sample query string:
//[include filepath="/get-posts.php?format=grid&taxonomy=testing&term=stuff&posttype=work"]

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.

share|improve this answer
1  
You shouldn't use TEMPLATEPATH or STYLESHEETPATH. Use get_template/stylesheet_dir() instead or the *_url() equivalent for pathes/URis –  kaiser Apr 21 '12 at 10:04
    
ericissocial Thanks so much! I'm going to plug it all in and give it a try, I'll let you know how it goes. –  Eric Downs Apr 21 '12 at 20:27
    
No problem, you should follow kaiser's recommendation and use the GET_template functionality instead of the TEMPLATEPATH that I had in the code, as well. Kaiser, thanks for the heads up, I'll edit the code in my answer when I'm not viewing this on my phone. –  Eric Allen Apr 21 '12 at 20:33
    
I updated the TEMPLATEPATH to get_stylesheet_directory_uri() –  Eric Allen Apr 22 '12 at 3:29

this Plugin works fine: http://wordpress.org/extend/plugins/shortcode-exec-php/

Wolfram

share|improve this answer
1  
So, what's different between the OP's code and the linked Plugin? –  Chip Bennett Apr 20 '12 at 15:59
    
Wolfram, Thanks for the plugin link, if possible I was just hoping to do it without the use of a plugin. –  Eric Downs Apr 20 '12 at 17:15
    
Will you improve this answer? Or should I convert it to a comment? –  toscho Apr 21 '12 at 9:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.