0

I am still looking for a solution for this and can't seem to find it. I have the following code to start, but am not sure how to write the function to fire off a .js file. I need this to work on one specific page ID.

function ipad_alert() {
 if( preg_match('/ipad/i',$_SERVER['HTTP_USER_AGENT'])) {?>
      // link to .js file? 
 <?php }
 }
 add_action('init','ipad_alert');

1 Answer 1

2

use wp_enqueue_script() to add a js file. I've hooked wp_enqueue_scripts so I can check is_page() for a specific page.

function ipad_alert() {
    if( preg_match('/ipad/i',$_SERVER['HTTP_USER_AGENT']) && is_page(7) ) {
        wp_enqueue_script('my_script', get_bloginfo('template_directory') . '/js/my_script.js');
    }
}
add_action('wp_enqueue_scripts','ipad_alert');
3
  • I've tried using that, but I can't seem to get it to work. Here's a knucklehead question. I currently have the HTML that the js is supposed to hook to in the wordpress editor. Could that keep it from working? I didn't know create a specific template page for this because I don't see the need.
    – chris_s
    Jul 19, 2011 at 20:10
  • @chris_s - I'm not sure I understand what you mean? I put the above code in my functions.php file and it's working, so I'm not sure what the issue may be without more info.
    – Milo
    Jul 22, 2011 at 19:22
  • I currently have this HTML in the editor in a page. <a href="#dialog" name="modal">Simple Modal Window</a> <div id="boxes"> <!-- #customize your modal window here --> <div id="dialog" class="window"> <b>Testing of Modal Window</b> <!-- close button is defined as close class --> <a href="#" class="close">Close it</a> </div> <!-- Do not remove div#mask, because you'll need it to fill the whole screen --> <div id="mask"></div> </div> Can I get this to work if it's in the editor?
    – chris_s
    Jul 26, 2011 at 19:51

Your Answer

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

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