0

I've been searching around for a fix to this and while I definitely have a much better understanding of my issue (thanks to similar queries posted by others) I am still not sure of the solution.

I am writing a shortcode as follows:

function cup_bc_shortcode(){

$html = file_get_contents('/html/cup_bc_layout.php', true);
return $html;
}
add_shortcode('budgetcalc', 'cup_bc_shortcode');

The file 'cup_bc_layout.php' contains html and PHP. The PHP code is being rendered as text on output rather than being processed by the browser. Is there anyway to include the PHP file in a manner similar to this?

I appreciate that the shortcode must return the output in order to work but I'd prefer not to have to build this up as a single string from within this function if I can help it.

Also I am quite new to PHP so even the most obvious of solutions may have eluded me.

Your help greatly appreciated.

2 Answers 2

2

The problem is that you are using file_get_contents() which only gets the content of the file (hence the name)

if you need to render(evaluate and process the php file) then just use include().

But also if your cup_bc_layout.php file echo's out something then you should put the include inside an object buffer and then return the value to make sure the shortcode works properly.

So try this:

function cup_bc_shortcode($atts,$content=NULL){
    ob_start();
    include('/html/cup_bc_layout.php');
    $return_val = ob_get_contents();
    ob_end_clean();     
    return $return_val;
}
add_shortcode('budgetcalc', 'cup_bc_shortcode');
3
  • Hi Bainternet. That worked perfectly. I was unfamiliar with the object buffer technique so have spent a little time getting versed in its application. I see a world of possibilities in which I can use this now. Out of curiousity why have you passed arguments via the cup_bc_shotcode function? I didn't include these in my code and it still seemed to work fine. Commented Dec 12, 2011 at 14:17
  • Better yet, don't echo anything in that file, and just do a simple include, then echo out or pass data to an applicable function inside the shortcode callback, then you'll avoid any need for output buffering. My point being, i've never personally found myself in a situation where i had to use output buffering, and it's usually a quick fix and not an ideal solution(there's usually a better way). Perhaps you can tell us more about what's in the file you're including?
    – t31os
    Commented Dec 12, 2011 at 16:31
  • Cool, I'm open to exploring my options. Basically all the file contains is html and php. In fact it was the php file I used to hardcode this functionality into a previous website. This is my first time developing a WordPress plugin and boy has it been a learning experience! So as I say the file is a standard php file containing mostly html interspersed with php echo commands. To make my main plug in file clean I want to keep the content of the file I want to include separate from the main plug in file. Commented Dec 12, 2011 at 21:20
0

Why don't you place the code of that file inside the cup_bc_shotcode() function directly instead of calling a separate file. Or place the code of that file in a function and then call the function inside the cup_bc_shotcode() function.

1
  • In relation to your first point I am trying to avoid having to do that as the contents of the file contains over 300 lines of code. It's definitely the fall back option though. In relation to your second point that sounds like a good idea. I'll give that a shot. Thanks! Commented Dec 12, 2011 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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