4

I am looking to enqueue this script on the header of a wordpress page

<script src="https://js.chargebee.com/v2/chargebee.js" data-cb-site="mydomainame" ></script>

I know how to use

function wp_mk_scripts() {

    if( is_page(11) ){
    wp_enqueue_script( 'chargebee', 'https://js.chargebee.com/v2/chargebee.js', array(), '2', false );
}

}
add_action( 'wp_enqueue_scripts', 'wp_mk_scripts' );

but how do I add the parameter data-cb-site="mydomainame" ?

2
6

Please try code given below:

function add_data_attribute($tag, $handle) {
   if ( 'chargebee' !== $handle )
    return $tag;

   return str_replace( ' src', ' data-cb-site="mydomainame" src', $tag );
}
add_filter('script_loader_tag', 'add_data_attribute', 10, 2);
1
  • great this works fine !
    – JoaMika
    Aug 22 '18 at 12:20
1
function wp_mk_scripts() {

if ( is_page('331') ) {
        wp_enqueue_script( 'chargebee', 'https://js.chargebee.com/v2/chargebee.js', array(), 2.0 , false);
    }

}
add_action( 'wp_enqueue_scripts', 'wp_mk_scripts' );

add_filter('script_loader_tag', 'add_attributes_to_script', 10, 3); 
function add_attributes_to_script( $tag, $handle, $src ) {
    if ( 'chargebee.js' === $handle ) {
        $tag = '<script type="text/javascript" src="' . esc_url( $src ) . '" data-cb-site="mydomainame" ></script>';
    } 
    return $tag;
}

Based on the answer of the sample thread posted by kero - However this doesn't work - i do not get the attribute loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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