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>