Skip to content

Instantly share code, notes, and snippets.

@pgroot91
Forked from danielpataki/ajax-action.php
Created April 17, 2021 14:02
Show Gist options
  • Save pgroot91/3a65fa57b9d65a635b2936557d3b80f1 to your computer and use it in GitHub Desktop.
Save pgroot91/3a65fa57b9d65a635b2936557d3b80f1 to your computer and use it in GitHub Desktop.
Twenty Fifteen AJAX
$(document).on( 'click', '.nav-links a', function( event ) {
event.preventDefault();
$.ajax({
url: ajaxpagination.ajaxurl,
type: 'post',
data: {
action: 'ajax_pagination'
},
success: function( result ) {
alert( result );
}
})
})
add_action( 'wp_enqueue_scripts', 'my_enqueue_assets' );
function my_enqueue_assets() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}
/*
Theme Name: Twenty Fifteen AJAX
Theme URI: http://yourwebsite.com/twentyfifteen-ajax/
Description: The Twenty Fifteen theme with additional AJAX pagination
Author: Daniel Pataki
Author URI: http://danielpataki.com
Template: twentyfifteen
Version: 1.0.0
Tags: black, green, white, light, dark, two-columns, three-columns, left-sidebar, right-sidebar, fixed-layout, responsive-layout, custom-background, custom-header, custom-menu, editor-style, featured-images, flexible-header, full-width-template, microformats, post-formats, rtl-language-support, sticky-post, theme-options, translation-ready, accessibility-ready, responsive-layout, infinite-scroll, post-slider, design, food, journal, magazine, news, photography, portfolio, clean, contemporary, dark, elegant, modern, professional, sophisticated
Text Domain: twentyfifteen-ajax
*/
function my_enqueue_assets() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
wp_enqueue_script( 'ajax-pagination', get_stylesheet_directory_uri() . '/js/ajax-pagination.js', array( 'jquery' ), '1.0', true );
}
<script type='text/javascript'>
/* <![CDATA[ */
var ajaxpagination = {"ajaxurl":"http:\/\/wordpress.local\/wp-admin\/admin-ajax.php"};
/* ]]> */
</script>
wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));
(function($) {
$(document).on( 'click', '.nav-links a', function( event ) {
event.preventDefault();
alert( 'Clicked Link' );
})
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment