1

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!

Jason Weber
  • 5,653
  • 6
  • 40
  • 73
  • 1
    The [answer](http://stackoverflow.com/a/18945175/1287812) I provided to the `defer` issue should cover this as well, no? . . . : `if ( FALSE !== strpos( $url, 'videopopup.js' ) )` – brasofilo Sep 22 '13 at 17:17
  • Hi Brasofilo. You code for this issue resulted in the following html output, which seems right to me: – Jason Weber Sep 22 '13 at 20:24

1 Answers1

1

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.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
  • Thank you Gerald. This resulted in the following html output, which is seemingly perfect: ... without the semicolon that stackoverflow throws in there. I think this will put me on the right track -- still testing. Thanks for your time! – Jason Weber Sep 22 '13 at 20:30
  • This is tested and a proven solution for anybody who has a similar issue. – Jason Weber Sep 23 '13 at 00:54
  • This is tested and a proven solution that wants an html output as stated above, however it duplicates a second call to the javascript file; still tinkering with it via the print_scripts hook link Gerald provided to see if I can only get it to call the javascript once. – Jason Weber Sep 23 '13 at 01:29
  • Thanks again Gerald ... this solution is tested now. I hadn't properly eliminated the previous function, and thus was getting a duplicate. All is perfect now. BUT ... I'm still trying to figure out where you got the "addPopupScript" from ... or did you just pick a name for the function yourself? – Jason Weber Sep 23 '13 at 03:44
  • I just made it up. You have to choose a name that is unique through the WordPress installation (including all plugins and themes that may be installed), so I usually prefix my functions with an abbreviation of the name of my theme/plugin. – Gerald Schneider Sep 23 '13 at 06:25
  • Just a note: If you want to add this to all javascript calls you will be better off with the solution suggested by brasofilo in the comments. – Gerald Schneider Sep 23 '13 at 06:27