javascript - How to disable link after first click and allow user to vote only if logged in -
i working on plugin in wordpress vote post or vote down using ajax. working fine problem not able disable onclick eventhandler
after first click whenever voting post , can add vote multiple times. want ignore should able vote once. if click on vote voteup anchor tag should disable , votedown anchor tag should enable. @ same time if click on votedown anchor tag votedown should disable , voteup should enable. want enable voting feature if user logged in wordrpess.
i have function show popup if user not logged in. i.e login_open();
if user not logged in , try vote function should execute login_open();
otherwise user should able vote or downvote once ..
here code //
php
//defining base paths define('voteupurl', wp_plugin_url."/".dirname( plugin_basename( __file__ ) ) ); define('voteuppath', wp_plugin_dir."/".dirname( plugin_basename( __file__ ) ) ); //enqueue script admin ajax , cutom js file function voteme_enqueuescripts() { wp_enqueue_script('voteme', voteupurl.'/js/voteup.js', array('jquery')); wp_localize_script( 'voteme', 'votemeajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); wp_localize_script( 'votedown', 'votedownajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); } add_action('wp_enqueue_scripts', voteme_enqueuescripts); //adding vote links posts. function voteme_getvotelink(){ $votemelink = ""; $post_id = get_the_id(); $votemecount = get_post_meta($post_id, '_votemecount', true) != '' ? get_post_meta($post_id, '_votemecount', true) : '0'; $link = $votemecount.' <a href="javascript:void(0);" onclick="votemeaddvote('.$post_id.');">'.'vote up'.'</a>'; $link .=' <a href="javascript:void(0);" onclick="votemedownvote('.$post_id.');">'.'vote down'.'</a>'; $votemelink = '<div id="voteme-'.$post_id.'">'; $votemelink .= '<span>'.$link.'</span>'; $votemelink .= '</div>'; return $votemelink; } //function count function get_current_vote_count(){ $voteup_count = ""; $post_id = get_the_id(); $votemecount = get_post_meta($post_id, '_votemecount', true) != '' ? get_post_meta($post_id, '_votemecount', true) : '0'; $votelink = '<span class="vote_count">'. $votemecount .'</span>'; return $votelink; die($votelink); } //add vote function function voteme_addvote() { $results = ''; global $wpdb; $post_id = $_post['postid']; $votemecount = get_post_meta($post_id, '_votemecount', true) != '' ? get_post_meta($post_id, '_votemecount', true) : '0'; $votemecountnew = $votemecount + 1; update_post_meta($post_id, '_votemecount', $votemecountnew); $results.=$votemecountnew; // return string die($results); } // creating ajax call of add vote wordpress add_action( 'wp_ajax_nopriv_voteme_addvote', 'voteme_addvote' ); add_action( 'wp_ajax_voteme_addvote', 'voteme_addvote' ); //add vote function function voteme_downvote() { $results = ''; global $wpdb; $post_id = $_post['postid']; $votemecount = get_post_meta($post_id, '_votemecount', true) != '' ? get_post_meta($post_id, '_votemecount', true) : '0'; $votemecountnew = $votemecount - 1; update_post_meta($post_id, '_votemecount', $votemecountnew); $results.= $votemecountnew; // return string die($results); } // creating ajax call of down vote wordpress add_action( 'wp_ajax_nopriv_voteme_downvote', 'voteme_downvote' ); add_action( 'wp_ajax_voteme_downvote', 'voteme_downvote' );
//javascript , ajax calls
function votemeaddvote(postid) { jquery.ajax({ type: 'post', url: votemeajax.ajaxurl, data: { action: 'voteme_addvote', postid: postid }, success:function(data, textstatus, xmlhttprequest){ var vote_count_id = jquery('.vote_count'); jquery(vote_count_id).html(''); jquery('.vote_count').append(data); var thisr = jquery('.voter button:first-child') thisr.disable = true; // add additional logic here }, error: function(mlhttprequest, textstatus, errorthrown){ alert(errorthrown); } }); } function votemedownvote(postid) { jquery.ajax({ type: 'post', url: votemeajax.ajaxurl, data: { action: 'voteme_downvote', postid: postid }, success:function(data, textstatus, xmlhttprequest){ var vote_count_id = jquery('.vote_count'); jquery(vote_count_id).html(''); jquery('.vote_count').append(data); }, error: function(mlhttprequest, textstatus, errorthrown){ alert(errorthrown); } }); }
//html adding votes
<div class="voter"> <a onclick="votemeaddvote(<?php echo $post_id; ?>);">voteup</a> <a onclick="votemedownvote(<?php echo $post_id; ?>)">vote down</a> </div>
simplely using .one()
instead of .on()
try this,
$(".voter a:last-child").one( "click", function() { console.log("click"); var data = $(this).data(); console.log(data); votemedownvote(data.id); });
Comments
Post a Comment