Skip to content

Instantly share code, notes, and snippets.

@abegit
Forked from danielpataki/content-filter.php
Last active December 16, 2015 21:47
Show Gist options
  • Save abegit/2032e25857d72cb05d38 to your computer and use it in GitHub Desktop.
Save abegit/2032e25857d72cb05d38 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' )
));
}
$fixJq = jQuery.noConflict();
$fixJq( document ).on( 'click', '.love-button', function() {
var user_id = $fixJq(this).data('id');
var matchid = $fixJq(this).data('matchid');
$fixJq.ajax({
url : postlove.ajax_url,
type : 'post',
data : {
action : 'post_love_add_love',
user_id : user_id,
matchid : matchid
},
success : function( response ) {
$fixJq('#love-count').html( response );
alert( response );
}
});
return false;
})
<?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
*/
add_action( 'wp_enqueue_scripts', 'post_love_assets' );
function post_love_assets() {
if( bp_is_members_directory() ) {
wp_enqueue_style( 'love', plugins_url( '/love.css', __FILE__ ) );
}
}
add_action( 'wp_enqueue_scripts', 'ajax_test_enqueue_scripts' );
function ajax_test_enqueue_scripts() {
if( bp_is_members_directory() ) {
wp_enqueue_style( 'love', plugins_url( '/style.css', __FILE__ ) );
}
wp_enqueue_script( 'love', plugins_url( '/love-basic-ajax.js', __FILE__ ), '1.0', true );
wp_localize_script( 'love', 'postlove', array(
'ajax_url' => admin_url( 'admin-ajax.php' )
));
}
function bp_send_image_denied_message($user1, $user2) {
global $bp;
//check_admin_referer(message_check”); // adjust if needed
$sender_id = $user1; // moderator id ?
$recip_id = $user2; // denied image user id ?
if ( $thread_id = messages_new_message( array('sender_id' => $sender_id, 'subject' =>'Hello Partner', 'content' => 'How do you do', 'recipients' => $recip_id ) ) ) {
bp_core_add_message( __( 'Image Denied Message was sent.', 'buddypress' ) );
} else {
bp_core_add_message( __( 'There was an error sending that Private Message.', 'buddypress' ), 'error' );
}
}
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 = bp_get_profile_field_data( 'field=handshake&user_id='.$_REQUEST['user_id'] );
$love .= ','. $_REQUEST['matchid'];
xprofile_set_field_data( 'handshake', $_REQUEST['user_id'], $love );
bp_send_image_denied_message($_REQUEST['user_id'], $_REQUEST['matchid']);
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
echo $love;
die();
}
else {
wp_redirect( get_permalink( $_REQUEST['user_id'] ) );
exit();
}
}
function post_love_display( $content ) {
$love_text = '';
if (bp_loggedin_user_id()) {
$love = bp_get_profile_field_data( 'field=handshake&user_id='.bp_loggedin_user_id() );
$love = ( empty( $love ) ) ? 0 : $love;
echo '<p class="love-received">';
echo '<a class="love-button" href="' . admin_url( 'admin-ajax.php?action=post_love_add_love&user_id=' . bp_loggedin_user_id(). '&matchid=' . bp_get_member_user_id() ) . '" data-id="' . bp_loggedin_user_id() . '" data-matchid="' . bp_get_member_user_id() . '">'.bp_get_member_user_id().'</a>';
echo '<span id="love-count">' . $love . '</span>';
echo '</p>';
}
}
add_action('bp_directory_members_item', 'post_love_display');
?>
$fixJq = jQuery.noConflict();
$fixJq( document ).on( 'click', '.love-button', function() {
var post_id = $fixJq(this).data('id');
$fixJq.ajax({
url : postlove.ajax_url,
type : 'post',
data : {
action : 'post_love_add_love',
post_id : post_id
},
success : function( response ) {
$fixJq('#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++;
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