0

This is my first post! Apologies in advance if I miss any conventions (and please let me know).

Just a general question - when outputting a shortcode that requires some dynamic data to be pulled in via PHP, is it best to include the PHP in and before the HTML in the shortcode (as in below example), or somehow pre-process the data and just output the final HTML? If the latter, how would this be done?

This question also extends to javascript (in between <script>) tags that has dynamic data pulled from PHP. Currently I include this in shortcodes but I'd prefer to be able to load them elsewhere (not in the main HTML body).

Thanks!

add_shortcode('my_tag', 'my_output');

function my_output(){
require '/output.php';
}

//output.php
<?php
  $number_of_places = 4; // Dynamic data
?>
<form method="POST">
     <select name="number-of-places" type="number">
         <?php
         $i = 1;
         while ($i <= $number_of_places){
             echo '<option value="' . $i . '">' . $i . '</option>';
             $i++;
         }
         ?>
     </select>
     <input type="submit" name="submit" value="Book Now">
 </form>

3
  • is your PHP code incomplete? I'm not seeing any shortcode related code. Remember this isn't a discussion forum/thread so you need to be able to mark an answer not just as the best response, but as the canonical correct answer for all people with this question, if you're just looking for general advice you might be better asking this in a forum
    – Tom J Nowell
    Sep 27, 2022 at 17:23
  • I haven't included the add_shortcode wordpress function or the function that delivers the code, just the code that is in the function that is called by the short code Sep 28, 2022 at 19:48
  • 1
    EDITED - to include shortcode function and file Sep 28, 2022 at 20:14

1 Answer 1

2

Shortcodes should always return data rather than echoing it. That way it appears in the right place on the page.

A couple of ways you can do this:

Option 1 - add to variable

function my_output() {
    $output = '<div>' . $myvariable . '</div>';
    return $output;
}

Option 2 - use the object buffer

function my_output() {
    // Start the object buffer, which saves output instead of outputting it.
    ob_start();
    ?>
    <div>
    <?php echo $myvariable; ?>
    </div>
    <?php
    // Return everything in the object buffer.
    return ob_get_clean();
}
2
  • As Tom pointed out I didn't include the complete code so I've just now edited my post. And thank you for your reply. So to clarify, you would instead of requiring a file you suggest carrying out the php logic separately and assigning the results to variables, then returning another variable that concatenates the HTML with the calculated values as other variables? Sep 28, 2022 at 20:20
  • She's right, require would still output directly to the browser which is not how a shortcode works. You should expect anything that involves nested shortcodes to be broken with your shortcode always appearing at the top regardless of the ordering, as well as anything that parses content such as RSS feeds or REST APIs. If you still want to require a file I'd suggest Option 2 is best as you can do it between the output buffers. I'd also suggest switching from require to get_template_part if this is inside a theme and not a plugin. That would allow child themes to override this
    – Tom J Nowell
    Sep 29, 2022 at 11:01

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.