1

I'm extremely new to Wordpress, and I've offered to help out with a project that uses it.

In a new page I'm creating, I need to use javascript/jQuery to write out the html dynamically - it doesn't seem to be working? I believe I read there was something special that needed to be done, but I can't figure out. I tried changing the $ to jQuery, but that didn't work either.

<script type="text/javascript">
$(document).ready(function(){
     alert('hoi');
     jQuery.ajax({
        type: "GET",
        url: "link-here",
        dataType: "text",
        success: function(data) {alert(CSVToArray(data));}
        error: function(){ alert('error');}
     });
</script>

I have this at the top - the alert isn't even called. How can I get this working?

Edit It looks like jQuery is loaded for all but the admin page - correct?

//jQuery Insert From Google
if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", false, null);
   wp_enqueue_script('jquery');
}

Edit Two It looks like the code I wrote is being wrapped in <p> tags. This must mean that the script doesn't belong in the New Page. Where should the code go?

5
  • do you have access to the theme functions.php?
    – ifdion
    Sep 24, 2012 at 3:11
  • I do. I can make changes to it.
    – Cody
    Sep 24, 2012 at 3:16
  • Why the downvote?
    – Cody
    Sep 24, 2012 at 7:43
  • The downvotes are probably for the lack of research effort. If you search our site for include jquery you will find the answer.
    – fuxia
    Oct 1, 2012 at 17:34
  • 1
    My question was about including it in a page - not a page template. I couldn't find anything regarding this.
    – Cody
    Oct 1, 2012 at 20:19

2 Answers 2

2

You need to make sure that jQuery is loaded, so that you can use it.

In functions.php:

function namespace_enqueue_scripts() {
    if ( is_page( 'slug-of-new-page-you-are-creating' ) )
        wp_enqueue_script('jquery');
}
add_action( 'wp_enqueue_scripts', 'namespace_enqueue_scripts' );
5
  • Made an edit to my post
    – Cody
    Sep 24, 2012 at 3:24
  • I agree, it looks like jQuery is loaded in the front end, and not in the back end. Sep 24, 2012 at 3:29
  • So what is it I need to do?
    – Cody
    Sep 24, 2012 at 3:29
  • Do you have <php wp_head(); ?> somewhere in your header.php? Sep 24, 2012 at 3:31
  • yes, I do - is there something needed in the new page besides putting the code in a script tag?
    – Cody
    Sep 24, 2012 at 3:36
0

Ah, now I feel dumb. The problem was how I was putting the script into the page.

This solved my problem:

<script type="text/javascript"><!--//--><![CDATA[//><!--
 alert('hoi');
 //--><!]]></script>

Using Javascript

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.