Adding PHP/HTML code inside page from custom template

I’m helping my son with his financial site with some custom programming. The first project for him is a custom spreadsheet using jQuery, flot, php, etc.

I created a child theme, created a template, functions.php, style sheet, etc, which all works well – as long as the spreadsheet is at the end of the page. But now I’m trying to improve it by adding the ability for him to add text both before and after the spreadsheet.

My first idea was that I might be able to simply use a shortcode, but I don’t know if that’s the correct way. It seems that shortcodes are not for outputting large amounts of mixed php/html. I can include the file and call a function in the spreadsheet file, and that works, but the shortcode so far hasn’t done anything except print out the actual shortcode itself. But if it’s an incorrect method, I don’t want to waste time trying to make it work.

Any ideas for how I can do this? I suppose I could just manually parse the_post() and process a shortcode myself, but I’d hate to do that if there’s a method I can use that’s built in.

More Info
I tried experimenting with shortcodes. In my functions.php (which I know is being used because I load some javascript and css files):

function register_shortcodes() {
  add_shortcode('spreadsheet_placeholder', 'display_spreadsheet');
}
add_action( 'init', 'register_shortcodes');

I put a tag [spreadsheet_placeholder] in the middle of a page, and the page rendered with that tag in the middle. Up until today, it just displayed the tag, but just now I got the bright idea to include the external php file containing the function (display_spreadsheet).

I got the spreadsheet to display after including the php file, just not the way I wanted. But it did work. It just displayed above the content rather than in the middle. I had read that I need to return the data, not just ouput it directly, so I expected something like that.

I had tried using a nowdoc variable to save the entire function to a variable, but I think this is overly optimistic (maybe very much so), as I have mixed php and html with php tags, so I guess I would need to go into the php code and save it as several variables and concatenate them together to return the entire code. But that’s more of a php problem than WordPress, I guess.

At this point, I’m not sure which is easier – doing that, or parsing for a shortcode-like tag myself in the content of the page, and doing the logic in the code.

The solution

Thanks to bonger and Brad Dalton for the help with using ob_start(), something I’ve never used before.

I took my original php/html code out of it’s file, and put it into a separate file, inside a function. I included that file in functions.php, and pointed the shortcode registration to the function. Then, using Brad’s example, I returned the entire file at the end and everything worked perfectly.

Thanks to both of you. I’m happy with the results, and so is my son.

1 Answer
1

This is how to code the shortcode

function foobar_func( $atts ){
    return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );

And to execute a function using output buffering

add_shortcode( 'shortcode_tag', 'function_name' );
function function_name($atts) {
    ob_start();

    // Add your code

    $var = ob_get_contents();
    ob_end_clean();
    return $var;

}

Leave a Comment