I am trying to add a short javascript as part of registering a WP shortcode. You can see my simplified code below. When I execute this on my website everything loads fine except for the part between the <script> ... </script> tags. I looked at the source code after the page has loaded and neither the <script> tags nor the function in between is there.

I don't know what I'm doing wrong.

Is there a filter in WP that removes <script> tags?

Thank you!

// register custom shortcodes
function psb_register_shortcodes() {
    add_shortcode('psb-booking', 'psb_booking_shortcode');

function psb_booking_shortcode( $args, $content='' ) {
    global $wpdb; 
    $output = 
             function doSomething () {};
    $output .= '<p> testing </p>';
    $table_name = $wpdb->prefix . 'psb_booking';
    foreach ( $wpdb->get_col( $wpdb->prepare("DESC " . $table_name, 0 )) as $column_name ) {
        error_log( $column_name );
    $output .= file_get_contents(plugin_dir_path( __FILE__ ) . 'includes/psb-booking.html');

    return $output;
  • I know it doesn't technically answer your question ("Is there a filter in WP..." but enqueuing a script to the footer when the shortcode is called by using wp_enqueue_script, would answer your needs, yes? Commented Jan 30, 2021 at 1:05
  • @CelsoBessa, the Javascript I'm trying to add is a var structure which is created dynamically from the wpdb. I thought I could add the php code to create that var here in the callback function. Since it's dynamically created I can't use wp_enqueue_script, at least I don't know how. Commented Jan 31, 2021 at 18:24


Your Answer

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

Browse other questions tagged or ask your own question.