Skip to content

Instantly share code, notes, and snippets.

@moskalukigor
Last active September 14, 2017 08:36

Revisions

  1. Ihor revised this gist Sep 14, 2017. 2 changed files with 18 additions and 25 deletions.
    27 changes: 2 additions & 25 deletions Wordpress Ajax → functions.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    /*IS NOT WOOCOMMERCE*/
    if ( ! function_exists( 'is_ajax' ) ) {
    /**
    @@ -8,23 +9,17 @@ function is_ajax() {
    return defined( 'DOING_AJAX' );
    }
    }



    //FUNCTIONS AJAX
    add_action( 'wp_ajax_loadPosts', 'loadPosts');
    add_action( 'wp_ajax_nopriv_loadPosts', 'loadPosts');
    function loadPosts($paged = 1){
    extract($_POST);

    //show post

    if (is_ajax()) {
    exit();
    }
    }


    //FUNCTIONS VARIABLES JS
    function js_variables(){
    $variables = array (
    @@ -36,22 +31,4 @@ function js_variables(){
    ';</script>'
    );
    }
    add_action('wp_head','js_variables');



    //JS
    var ajaxcontent = $('.products');

    $.ajax({
    type: 'POST',
    url: window.wp_data.ajax_url,
    data: {
    action: "loadPosts",
    },
    success: function (html) {
    ajaxcontent.html(html);
    }
    });

    return false;
    add_action('wp_head','js_variables');
    16 changes: 16 additions & 0 deletions loadPosts.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    $(document).on("click", "#button", function(){

    var ajaxcontent = $('.products');
    $.ajax({
    type: 'POST',
    url: window.wp_data.ajax_url,
    data: {
    action: "loadPosts",
    },
    success: function (html) {
    ajaxcontent.html(html);
    }
    });
    return false;

    });
  2. Ihor revised this gist Sep 14, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Wordpress Ajax
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ if ( ! function_exists( 'is_ajax' ) ) {
    //FUNCTIONS AJAX
    add_action( 'wp_ajax_loadPosts', 'loadPosts');
    add_action( 'wp_ajax_nopriv_loadPosts', 'loadPosts');
    function loadloadPosts($paged = 1){
    function loadPosts($paged = 1){
    extract($_POST);

    //show post
  3. Ihor revised this gist Sep 14, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Wordpress Ajax
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,8 @@ add_action( 'wp_ajax_nopriv_loadPosts', 'loadPosts');
    function loadloadPosts($paged = 1){
    extract($_POST);

    echo 'test';
    //show post

    if (is_ajax()) {
    exit();
    }
  4. Ihor revised this gist Sep 14, 2017. 1 changed file with 7 additions and 8 deletions.
    15 changes: 7 additions & 8 deletions Wordpress Ajax
    Original file line number Diff line number Diff line change
    @@ -12,9 +12,9 @@ if ( ! function_exists( 'is_ajax' ) ) {


    //FUNCTIONS AJAX
    add_action( 'wp_ajax_loadSticker_ajax', 'loadSticker_ajax');
    add_action( 'wp_ajax_nopriv_loadSticker_ajax', 'loadSticker_ajax');
    function loadSticker_ajax($paged = 1){
    add_action( 'wp_ajax_loadPosts', 'loadPosts');
    add_action( 'wp_ajax_nopriv_loadPosts', 'loadPosts');
    function loadloadPosts($paged = 1){
    extract($_POST);

    echo 'test';
    @@ -40,18 +40,17 @@ add_action('wp_head','js_variables');


    //JS
    var form = $('.filtering > form'),
    ajaxcontent = $('.products');
    var ajaxcontent = $('.products');

    $.ajax({
    type: 'POST',
    url: window.wp_data.ajax_url,
    data: {
    action: "loadMorePosts",
    action: "loadPosts",
    },
    success: function (html) {
    ajaxcontent.html(html);
    }
    });
    });

    return false;
    return false;
  5. moskalukigor revised this gist Apr 7, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Wordpress Ajax
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,9 @@ var form = $('.filtering > form'),
    $.ajax({
    type: 'POST',
    url: window.wp_data.ajax_url,
    data: form.serialize() + "&category="+category,
    data: {
    action: "loadMorePosts",
    },
    success: function (html) {
    ajaxcontent.html(html);
    }
  6. moskalukigor revised this gist Dec 29, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Wordpress Ajax
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,6 @@ $.ajax({
    data: form.serialize() + "&category="+category,
    success: function (html) {
    ajaxcontent.html(html);
    }
    }
    });

  7. moskalukigor revised this gist Dec 6, 2016. 1 changed file with 1 addition and 40 deletions.
    41 changes: 1 addition & 40 deletions Wordpress Ajax
    Original file line number Diff line number Diff line change
    @@ -17,46 +17,7 @@ add_action( 'wp_ajax_nopriv_loadSticker_ajax', 'loadSticker_ajax');
    function loadSticker_ajax($paged = 1){
    extract($_POST);

    $tax_query = "";
    $orderby = "date";
    $order = "DESC";

    if($category != "*" && $category != null)
    {
    $tax_query = array(
    'relation' => 'AND',
    array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => array($category)
    ),
    );
    }
    $args = array(
    'posts_per_page' => 12,
    'paged' => $paged,
    'tax_query' => $tax_query,
    'post_type' => 'product',
    'post_status' => 'publish',
    'orderby' => $orderby,
    'order' => $order
    );
    $products = new WP_Query( $args );
    $num_pages = $products->max_num_pages;
    if($products->have_posts()) {
    while ( $products->have_posts() ) {
    $products->the_post();

    wc_get_template_part( 'content', 'product' );

    }
    if ($num_pages != $paged) {
    echo "<div class='for_button'><div class='button empty loadMore' data-href=". ($paged + 1) ." data-category=".$category.">Load More</div></div><div class='for_loader'>". get_loader() ."</div>";
    }
    }
    else {
    echo '<div><p class="custom_coming_soon">Coming <span>Soon!</span></p></div>';
    }
    echo 'test';
    if (is_ajax()) {
    exit();
    }
  8. moskalukigor revised this gist Nov 10, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Wordpress Ajax
    Original file line number Diff line number Diff line change
    @@ -89,8 +89,6 @@ $.ajax({
    success: function (html) {
    ajaxcontent.html(html);
    }
    //animation end here
    $('.add_to_cart_button:not(.product_type_variable)').attr('data-remodal-target', 'product_popup');
    }
    });

  9. moskalukigor revised this gist Nov 10, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Wordpress Ajax
    Original file line number Diff line number Diff line change
    @@ -79,6 +79,9 @@ add_action('wp_head','js_variables');


    //JS
    var form = $('.filtering > form'),
    ajaxcontent = $('.products');

    $.ajax({
    type: 'POST',
    url: window.wp_data.ajax_url,
  10. moskalukigor revised this gist Nov 10, 2016. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions Wordpress Ajax
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,16 @@
    /*IS NOT WOOCOMMERCE*/
    if ( ! function_exists( 'is_ajax' ) ) {
    /**
    * is_ajax - Returns true when the page is loaded via ajax.
    * @return bool
    */
    function is_ajax() {
    return defined( 'DOING_AJAX' );
    }
    }



    //FUNCTIONS AJAX
    add_action( 'wp_ajax_loadSticker_ajax', 'loadSticker_ajax');
    add_action( 'wp_ajax_nopriv_loadSticker_ajax', 'loadSticker_ajax');
  11. moskalukigor renamed this gist Nov 10, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. moskalukigor created this gist Nov 10, 2016.
    81 changes: 81 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    //FUNCTIONS AJAX
    add_action( 'wp_ajax_loadSticker_ajax', 'loadSticker_ajax');
    add_action( 'wp_ajax_nopriv_loadSticker_ajax', 'loadSticker_ajax');
    function loadSticker_ajax($paged = 1){
    extract($_POST);

    $tax_query = "";
    $orderby = "date";
    $order = "DESC";

    if($category != "*" && $category != null)
    {
    $tax_query = array(
    'relation' => 'AND',
    array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => array($category)
    ),
    );
    }
    $args = array(
    'posts_per_page' => 12,
    'paged' => $paged,
    'tax_query' => $tax_query,
    'post_type' => 'product',
    'post_status' => 'publish',
    'orderby' => $orderby,
    'order' => $order
    );
    $products = new WP_Query( $args );
    $num_pages = $products->max_num_pages;
    if($products->have_posts()) {
    while ( $products->have_posts() ) {
    $products->the_post();

    wc_get_template_part( 'content', 'product' );

    }
    if ($num_pages != $paged) {
    echo "<div class='for_button'><div class='button empty loadMore' data-href=". ($paged + 1) ." data-category=".$category.">Load More</div></div><div class='for_loader'>". get_loader() ."</div>";
    }
    }
    else {
    echo '<div><p class="custom_coming_soon">Coming <span>Soon!</span></p></div>';
    }
    if (is_ajax()) {
    exit();
    }
    }


    //FUNCTIONS VARIABLES JS
    function js_variables(){
    $variables = array (
    'ajax_url' => admin_url('admin-ajax.php'),
    );
    echo(
    '<script type="text/javascript">window.wp_data = '.
    json_encode($variables).
    ';</script>'
    );
    }
    add_action('wp_head','js_variables');



    //JS
    $.ajax({
    type: 'POST',
    url: window.wp_data.ajax_url,
    data: form.serialize() + "&category="+category,
    success: function (html) {
    ajaxcontent.html(html);
    }
    //animation end here
    $('.add_to_cart_button:not(.product_type_variable)').attr('data-remodal-target', 'product_popup');
    }
    });

    return false;