-
-
Save jquimera/f7cac3c2f57792c1d4f9112fefb0a34f to your computer and use it in GitHub Desktop.
Ajax in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'wp_ajax_button_click', 'user_clicked' ); | |
function user_clicked() { | |
update_user_meta( get_current_user_id(), 'clicked_link', 'yes' ); | |
echo 'ok'; | |
die(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function has_user_clicked_link() { | |
if( 'yes' == get_user_meta( get_current_user_id(), 'clicked_link', true ) ) { | |
return true; | |
} | |
return false; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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