Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
do you have access to the theme functions.php? –  ifdion Sep 24 '12 at 3:11
    
I do. I can make changes to it. –  Doctor Oreo Sep 24 '12 at 3:16
    
Why the downvote? –  Doctor Oreo Sep 24 '12 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. –  toscho Oct 1 '12 at 17:34
    
My question was about including it in a page - not a page template. I couldn't find anything regarding this. –  Doctor Oreo Oct 1 '12 at 20:19
add comment

2 Answers 2

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

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' );
share|improve this answer
    
Made an edit to my post –  Doctor Oreo Sep 24 '12 at 3:24
    
I agree, it looks like jQuery is loaded in the front end, and not in the back end. –  Just Thomas Misund Sep 24 '12 at 3:29
    
So what is it I need to do? –  Doctor Oreo Sep 24 '12 at 3:29
    
Do you have <php wp_head(); ?> somewhere in your header.php? –  Just Thomas Misund Sep 24 '12 at 3:31
    
yes, I do - is there something needed in the new page besides putting the code in a script tag? –  Doctor Oreo Sep 24 '12 at 3:36
show 1 more comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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