Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active September 3, 2018 12:38
Show Gist options
  • Save danielpataki/70a2bbebe9cefa1aba4e to your computer and use it in GitHub Desktop.
Save danielpataki/70a2bbebe9cefa1aba4e to your computer and use it in GitHub Desktop.
AJAX
add_filter( 'the_content', 'post_love_display', 99 );
function post_love_display( $content ) {
$love_text = '';
if ( is_single() ) {
$love = get_post_meta( get_the_ID(), 'post_love', true );
$love = ( empty( $love ) ) ? 0 : $love;
$love_text = '<p class="love-received"><a class="love-button" href="#" data-id="' . get_the_ID() . '">give love</a><span id="love-count">' . $love . '</span></p>';
}
return $content . $love_text;
}
add_action( 'wp_enqueue_scripts', 'ajax_test_enqueue_scripts' );
function ajax_test_enqueue_scripts() {
wp_enqueue_script( 'test', plugins_url( '/test.js', __FILE__ ), array('jquery'), '1.0', true );
}
add_filter( 'the_content', 'post_love_display', 99 );
function post_love_display( $content ) {
$love_text = '';
if ( is_single() ) {
$love = get_post_meta( get_the_ID(), 'post_love', true );
$love = ( empty( $love ) ) ? 0 : $love;
$love_text = '<p class="love-received"><a class="love-button" href="' . admin_url( 'admin-ajax.php?action=post_love_add_love&post_id=' . get_the_ID() ) . '" data-id="' . get_the_ID() . '">give love</a><span id="love-count">' . $love . '</span></p>';
}
return $content . $love_text;
}
jQuery( document ).on( 'click', '.love-button', function() {
var post_id = jQuery(this).data('id');
jQuery.ajax({
url : postlove.ajax_url,
type : 'post',
data : {
action : 'post_love_add_love',
post_id : post_id
},
success : function( response ) {
jQuery('#love-count').html( response );
}
});
return false;
})
add_action( 'wp_ajax_nopriv_post_love_add_love', 'post_love_add_love' );
add_action( 'wp_ajax_post_love_add_love', 'post_love_add_love' );
function post_love_add_love() {
}
function post_love_add_love() {
$love = get_post_meta( $_REQUEST['post_id'], 'post_love', true );
$love++;
update_post_meta( $_REQUEST['post_id'], 'post_love', $love );
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
echo $love;
die();
}
else {
wp_redirect( get_permalink( $_REQUEST['post_id'] ) );
exit();
}
}
jQuery(document).ready( function($) {
$.ajax({
url: "http://yourwebsite.com",
success: function( data ) {
alert( 'Your home page has ' + $(data).find('div').length + ' div elements.');
}
})
})
add_action( 'wp_enqueue_scripts', 'ajax_test_enqueue_scripts' );
function ajax_test_enqueue_scripts() {
if( is_single() ) {
wp_enqueue_style( 'love', plugins_url( '/love.css', __FILE__ ) );
}
wp_enqueue_script( 'love', plugins_url( '/love.js', __FILE__ ), array('jquery'), '1.0', true );
wp_localize_script( 'love', 'postlove', array(
'ajax_url' => admin_url( 'admin-ajax.php' )
));
}
jQuery( document ).on( 'click', '.love-button', function() {
var post_id = jQuery(this).data('id');
jQuery.ajax({
url : postlove.ajax_url,
type : 'post',
data : {
action : 'post_love_add_love',
post_id : post_id
},
success : function( response ) {
alert(response)
}
});
})
<?php
/**
* Plugin Name: Ajax Test
* Plugin URI: http://danielpataki.com
* Description: This is a plugin that allows us to test Ajax functionality in WordPress
* Version: 1.0.0
* Author: Daniel Pataki
* Author URI: http://danielpataki.com
* License: GPL2
*/
jQuery( document ).on( 'click', '.love-button', function() {
var post_id = jQuery(this).data('id');
jQuery.ajax({
url : postlove.ajax_url,
type : 'post',
data : {
action : 'post_love_add_love',
post_id : post_id
},
success : function( response ) {
jQuery('#love-count').html( response );
}
});
})
add_action( 'wp_ajax_nopriv_post_love_add_love', 'post_love_add_love' );
add_action( 'wp_ajax_post_love_add_love', 'post_love_add_love' );
function post_love_add_love() {
$love = get_post_meta( $_POST['post_id'], 'post_love', true );
$love++;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
update_post_meta( $_POST['post_id'], 'post_love', $love );
echo $love;
}
die();
}
.entry-content .love-button {
background: #f14864;
color: #fff;
padding:11px 22px;
display:inline-block;
border:0px;
text-decoration: none;
box-shadow: 0px 6px #d2234c;
position:relative;
}
.entry-content .love-button:hover{
top:3px;
box-shadow: 0px 3px #d2234c;
}
.entry-content .love-button:active{
top:6px;
box-shadow: none;
}
#love-count {
background: #eee;
box-shadow: 0px 6px #ddd;
color: #666;
padding:11px 22px;
display:inline-block;
}
add_action( 'wp_enqueue_scripts', 'post_love_assets' );
function post_love_assets() {
if( is_single() ) {
wp_enqueue_style( 'love', plugins_url( '/love.css', __FILE__ ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment