Skip to content

Instantly share code, notes, and snippets.

@vishalbasnet23
Created February 18, 2015 10:53
Show Gist options
  • Save vishalbasnet23/bdd286c3e46c3a81dd4b to your computer and use it in GitHub Desktop.
Save vishalbasnet23/bdd286c3e46c3a81dd4b to your computer and use it in GitHub Desktop.
Ajax Call On Successful Twitter Share
<?php
add_action('wp_ajax_twitter_share', 'twitter_share', 0);
add_action('wp_ajax_nopriv_twitter_share', 'twitter_share');
function twitter_share() {
$your_data = $_POST['your_data'];
echo $your_data;
die;
}
<a href="https://twitter.com/intent/tweet?text=<?php the_title();?>&amp;url=<?php the_permalink();?>" data-lang="en" class="twitter-btn btn twitter-share" your_data="Your Custom Data">Tweet</a>
<script>
/******Twitter share start****/
window.twttr = (function (d, s, id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; js = d.createElement(s); js.id = id;
js.src = "//platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
return window.twttr || (t = { _e: [], ready: function (f) { t._e.push(f) } });
}(document, "script", "twitter-wjs"));
twttr.ready(function (twttr) {
twttr.events.bind('tweet', function (event) {
var yourData = event.target.attributes.your_data.value;
jQuery.ajax({
type: 'POST',
url: "<?php echo admin_url('admin-ajax.php'); ?>",
data: {
action: "twitter_share",
your_data: yourData
},
success: function(result) {
console.log(result)
}
});
});
});
/******Twitter share end******/
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment