Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ajax in WordPress
add_action( 'wp_ajax_button_click', 'user_clicked' );
function user_clicked() {
update_user_meta( get_current_user_id(), 'clicked_link', 'yes' );
wp_redirect( $_SERVER['HTTP_REFERER'] );
exit();
}
add_action( 'wp_ajax_button_click', 'user_clicked' );
function user_clicked() {
update_user_meta( get_current_user_id(), 'clicked_link', 'yes' );
echo 'ok';
die();
}
jQuery(document).on( 'click', '#button', function() {
var button = jQuery(this);
var url = button.attr('href');
jQuery.ajax({
type : 'post',
url : url,
data : {
action : 'user_clicked'
},
success : function( response ) {
if( response === 'ok' ) {
button.fadeOut( function() {
button.replaceWith( 'already clicked' );
})
}
}
});
return false;
})
if ( is_user_logged_in() ) {
if ( has_user_clicked_link() ) {
echo "already clicked";
}
else {
echo "<a id='button' href='" . admin_url('admin-ajax.php?action=button_click') . "'>Click here now</a>";
}
}
if ( is_user_logged_in() ) {
if ( has_user_clicked_link() ) {
echo "already clicked";
}
else {
echo "<a id='button' href='?button_click=true'>Click here now</a>";
}
}
function ajax_search_enqueues() {
if ( is_search() ) {
wp_enqueue_script( 'ajax-search', get_stylesheet_directory_uri() . '/js/ajax-search.js', array( 'jquery' ), '1.0.0', true );
wp_localize_script( 'ajax-search', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
wp_enqueue_style( 'ajax-search', get_stylesheet_directory_uri() . '/css/ajax-search.css' );
}
}
add_action( 'wp_enqueue_scripts', 'ajax_search_enqueues' );
add_action( 'wp_ajax_button_click', 'user_clicked' );
function user_clicked() {
update_user_meta( get_current_user_id(), 'clicked_link', 'yes' );
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
echo 'ok';
die();
}
else {
wp_redirect( $_SERVER['HTTP_REFERER'] );
exit();
}
}
function has_user_clicked_link() {
if( 'yes' == get_user_meta( get_current_user_id(), 'clicked_link', true ) ) {
return true;
}
return false;
}
add_action( 'wp_ajax_load_search_results', 'load_search_results' );
add_action( 'wp_ajax_nopriv_load_search_results', 'load_search_results' );
function load_search_results() {
$query = $_POST['query'];
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
's' => $query
);
$search = new WP_Query( $args );
ob_start();
if ( $search->have_posts() ) :
?>
<header class="page-header">
<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentyfourteen' ), get_search_query() ); ?></h1>
</header>
<?php
while ( $search->have_posts() ) : $search->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
twentyfourteen_paging_nav();
else :
get_template_part( 'content', 'none' );
endif;
$content = ob_get_clean();
echo $content;
die();
}
jQuery(document).on( 'submit', '#search-container form', function() {
var $form = jQuery(this);
var $input = $form.find('input[name="s"]');
var query = $input.val();
var $content = jQuery('#content')
jQuery.ajax({
type : 'post',
url : myAjax.ajaxurl,
data : {
action : 'load_search_results',
query : query
},
beforeSend: function() {
$input.prop('disabled', true);
$content.addClass('loading');
},
success : function( response ) {
$input.prop('disabled', false);
$content.removeClass('loading');
$content.html( response );
}
});
return false;
})
add_action( 'init', 'user_clicked' );
function user_clicked() {
if( is_user_logged_in() && !empty( $_GET['button_click'] ) && $_GET['button_click'] == 'true' ) {
update_user_meta( get_current_user_id(), 'clicked_link', 'yes' );
}
}
#content.loading > * {
opacity:0.2;
}
#content.loading:before {
content: "Loading New Posts";
padding: 22px;
background: #000;
color: #fff;
width: 100%;
box-sizing: border-box;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.