2

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.

9
  • 1
    Shortcodes can output as much stuff as you want, and would seem to be the way to go in your case - you're doing the add_shortcode() in your init action?
    – bonger
    Aug 10, 2015 at 9:50
  • @bonger: Thanks for the encouragement and tip. I added extra information to my post with what I've done since the post; I've actually made a lot of progress by a simple addition of an include directive in functions.php. Aug 10, 2015 at 15:00
  • 1
    You can wrap your included file in ob_start() and $ret = ob_get_clean() to gather all its echoed output into a string...
    – bonger
    Aug 10, 2015 at 16:08
  • @bonger: Thanks, I've seen that, but never investigated. Perhaps I should. I was considering adding a the_post action in functions.php to just modify the post looking for the tag. But it could be more problematic than I think, so I'll check your suggestion. But will it work if I have embedded php and html mixed? Aug 10, 2015 at 16:16
  • 1
    Yes, the embedded php will just be parsed as php by php as normal, so will just return html... I'd definitely go this (shortcode) way rather than doing your own parsing...
    – bonger
    Aug 10, 2015 at 16:31

1 Answer 1

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;

}
1
  • 1
    Thanks for the example for ob_start. I used that exact example, and it worked well. Since nobody else seemed to add an answer, I guess you get the acceptance credit. :-) Aug 11, 2015 at 5:02

Your Answer

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

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