Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Skip to Checkout Page from Beds24 Plugin Widget
// This code can be added to the theme's functions.php
// We first only add the script whereever the shortcode is present via an action
// See: https://wordpress.stackexchange.com/questions/228595/insert-custom-html-after-shortcode#answer-228597
<?php
add_action( 'after_setup_theme', function() use ( &$shortcode_tags ){
// Shortcode to override. Edit to your needs
$shortcode = 'bookwidget';
// Nothing to do if it's not registered as shortcode
if( ! shortcode_exists( $shortcode ) )
return;
// Get the shortcode's callback
$callback = $shortcode_tags[$shortcode];
// Override the shortcode
add_shortcode( $shortcode, function( $atts = array(), $content = '' ) use ( $callback ) {
// Fallback, in case the roomid is not set
$script = '';
// The room id needs to be present in the shortcode for this to work
if (isset($atts[roomid])) {
// Build the hidden input field
$input = "<input type='hidden' name='br1-" . $atts[roomid] . "' value='Book'>";
// Build the script
$script = "<script type='text/javascript'>
jQuery(document).ready(function() {
// Create the input variable
const input = jQuery(" . json_encode($input) . ");
// Even though we add the script right after shortcode finishes...
// We need to set a timeout to wait for the widget to load, before appending the input
setTimeout(function() {
// Append the widget to the form
input.appendTo(jQuery('.book-form-horizontal'));
}, 2000);
});
</script>";
}
// Append custom script to the bookwidget output
return
is_callable( $callback )
? call_user_func( $callback, $atts, $content, $callback ) . $script
: '';
});
}, PHP_INT_MAX );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment