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.

Revisions

  1. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions ux.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    $.ajax({
    url: ajaxpagination.ajaxurl,
    type: 'post',
    data: {
    action: 'ajax_pagination',
    query_vars: ajaxpagination.query_vars,
    page: page
    },
    beforeSend: function() {
    $('#main').find( 'article' ).remove();
    $('#main nav').remove();
    $(document).scrollTop();
    $('#main').append( '<div class="page-content" id="loader">Loading New Posts...</div>' );
    },
    success: function( html ) {
    $('#main #loader').remove();
    $('#main').append( html );
    }
    })
  2. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 37 additions and 0 deletions.
    37 changes: 37 additions & 0 deletions my_ajax_pagination.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    add_action( 'wp_ajax_nopriv_ajax_pagination', 'my_ajax_pagination' );
    add_action( 'wp_ajax_ajax_pagination', 'my_ajax_pagination' );

    function my_ajax_pagination() {
    $query_vars = json_decode( stripslashes( $_POST['query_vars'] ), true );

    $query_vars['paged'] = $_POST['page'];


    $posts = new WP_Query( $query_vars );
    $GLOBALS['wp_query'] = $posts;

    add_filter( 'editor_max_image_size', 'my_image_size_override' );

    if( ! $posts->have_posts() ) {
    get_template_part( 'content', 'none' );
    }
    else {
    while ( $posts->have_posts() ) {
    $posts->the_post();
    get_template_part( 'content', get_post_format() );
    }
    }
    remove_filter( 'editor_max_image_size', 'my_image_size_override' );

    the_posts_pagination( array(
    'prev_text' => __( 'Previous page', 'twentyfifteen' ),
    'next_text' => __( 'Next page', 'twentyfifteen' ),
    'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
    ) );

    die();
    }

    function my_image_size_override() {
    return array( 825, 510 );
    }
  3. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions final-localization.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    global $wp_query;
    wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'query_vars' => json_encode( $wp_query->query )
  4. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions final-localization.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'query_vars' => json_encode( $wp_query->query )
    ));
  5. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ajax-pagination.js
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@
    type: 'post',
    data: {
    action: 'ajax_pagination',
    wp_query: ajaxpagination.wp_query,
    query_vars: ajaxpagination.query_vars,
    page: page
    },
    success: function( html ) {
  6. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions ajax-pagination.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    (function($) {

    function find_page_number( element ) {
    element.find('span').remove();
    return parseInt( element.html() );
    }

    $(document).on( 'click', '.nav-links a', function( event ) {
    event.preventDefault();

    page = find_page_number( $(this).clone() );

    $.ajax({
    url: ajaxpagination.ajaxurl,
    type: 'post',
    data: {
    action: 'ajax_pagination',
    wp_query: ajaxpagination.wp_query,
    page: page
    },
    success: function( html ) {
    $('#main').find( 'article' ).remove();
    $('#main nav').remove();
    $('#main').append( html );
    }
    })
    })
    })(jQuery);
  7. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions ajax-action.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    add_action( 'wp_ajax_nopriv_ajax_pagination', 'my_ajax_pagination' );
    add_action( 'wp_ajax_ajax_pagination', 'my_ajax_pagination' );

    function my_ajax_pagination() {
    echo get_bloginfo( 'title' );
    die();
    }
  8. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions ajax-call.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    $(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 );
    }
    })
    })
  9. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions localize-script.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    <script type='text/javascript'>
    /* <![CDATA[ */
    var ajaxpagination = {"ajaxurl":"http:\/\/wordpress.local\/wp-admin\/admin-ajax.php"};
    /* ]]> */
    </script>
  10. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions localize-script.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
    'ajaxurl' => admin_url( 'admin-ajax.php' )
    ));
  11. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions simple-alert.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    (function($) {
    $(document).on( 'click', '.nav-links a', function( event ) {
    event.preventDefault();
    alert( 'Clicked Link' );
    })
    })(jQuery);
  12. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions enqueue.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    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 );
    }
  13. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions child-functions.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );
    function enqueue_parent_styles() {
    add_action( 'wp_enqueue_scripts', 'my_enqueue_assets' );
    function my_enqueue_assets() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
    }
  14. Daniel Pataki revised this gist Jan 14, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions child-functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );
    function enqueue_parent_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
    }
  15. Daniel Pataki created this gist Jan 14, 2015.
    11 changes: 11 additions & 0 deletions child-style.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    /*
    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
    */