0

I am working on a voting system in Wordpress with jQuery and AJAX. To make this voting system work I need jQuery in my theme. So wrote the code down in my functions.php. But for some reason my script doesn't do anything. Watch my full vote script here.

So I started reading the codex, and found here to include jQuery for AJAX calls, you need the json2 dependency. Does anyone have experience with this, and can tell me how I need to include this, because jQuery is already registered in Wordpress, so do I need to register jQuery again and add the json2 dependency to this? I don't get it..

// Ajax Voting
wp_register_script( 'ajax-vote', get_template_directory_uri() . '/js/ajax-vote.js', array('jquery') );

$nonce = wp_create_nonce("vote_nonce");
$stn_vote_data = array(
    'ajaxurl' => admin_url( 'admin-ajax.php'),
    'nonce' => $nonce,
);
wp_localize_script( 'ajax-vote', 'stn_vote', $stn_vote_data );

function stn_script_enqueuer() {    
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'ajax-vote' );

}
add_action( 'wp_enqueue_scripts', 'stn_script_enqueuer' );

1 Answer 1

0

You don't have to manually handle dependencies. That is the whole point of the enqueue system, it handles dependencies for you.

There's nothing wrong with the code in your post, so if you want help on it, you'll need to ask about your problem more specifically.

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.