简体   繁体   中英

How can I properly combine this simple javascript into this tiny php snippet?

I have a snippet of code in a php (WordPress) page:

function vp_contact_popup_add_to_header()
{
// Register the style like this for a plugin:  
wp_register_style( 'videopopup', plugins_url( '/code/css/videopopup.css', __FILE__ ), 
 array(), '20120208', 'all' );
// Register the script like this for a plugin:  
wp_register_script( 'videopopup', plugins_url( '/code/js/videopopup.js', __FILE__ ) );

wp_enqueue_style("videopopup");
wp_enqueue_script("videopopup");
}

What I'm trying to do is to add something in there ... like an extra clause to the javascript portion of that snippet, like this:

<script data-cfasync="false" src="/code/js/videopopup.js"></script> 

So I want to somehow incorprate the data-cfasync="false" into the php snippet, but I'm unsure how to do this. Any guidance would be truly appreciated!

I don't see any way to do this using wp_register_script() or wp_enqueue_script() . The only way I see to do this (except modifying the header file of the theme) is using the wp_print_scripts hook directly. You can do it like this:

add_action("wp_print_scripts", "addPopupScript");

function addPopupScript() {
    echo '<script data-cfasync="false" src="' . plugins_url( '/code/js/videopopup.js', __FILE__ ) . '"></script>' . "\n";
}

But heed the warning in the documentation that this should not be used directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM