Home The plugin shortcodes not working in ajax call
Reply: 0

The plugin shortcodes not working in ajax call

user3295
1#
user3295 Published in May 24, 2018, 11:21 pm

I am using simple share buttons. I have two posts on the page and other posts are getting via button click by ajax. On the ajax loaded content, simple share buttons are not displayed. I am getting them via shortcode. What is wrong?

    // Function.php
    function true_load_posts(){
        $args = unserialize(stripslashes($_POST['query']));
        $args['paged'] = $_POST['page'] + 1; // one more post
        $args['post_status'] = 'publish';
        $q = new WP_Query($args); //Query
        if( $q->have_posts() ):
            while($q->have_posts()): $q->the_post();
                ?>
              <article class="article" id="post-<?php the_ID(); ?>" <?php post_class(); ?>> // Article
                  <?php the_category( $post_id ); ?>
                <div class="news-article">
                  <h2><a href="<?php echo get_permalink();?>" class="headline-link"><?php the_title(); ?></a></h2> // headline post
                  <p class="author">By <span><?php echo get_the_author(); ?></span></p>
                  <div class="news-article__image"> // image article
                    <img src="<?php echo get_the_post_thumbnail_url(); ?>" alt="image">
                  </div>
                  <div class="inner-block">
                    <p class="excerpt"><?php $exs = get_the_excerpt(); print $exs;?></p>
                    <a href="<?php echo get_permalink();?>" class="more">read more</a> // link
                    <div class="bottom-block flex align-center"> // some block
                      <p class="post-date"><?php the_time('F j, Y g:i a'); ?>// post date</p>
                      <p class="share-links"><?php echo do_shortcode('[do_widget id=ssba_widget-2]'); ?></p>// my sortcode
                    </div>
                  </div>
                </div>
              </article>
            <?php
            endwhile;
        endif;
        wp_reset_postdata();
        die();
    }

    add_action('wp_ajax_loadmore', 'true_load_posts'); // hooks
    add_action('wp_ajax_nopriv_loadmore', 'true_load_posts'); // hooks

    // Script

    $('#true_loadmore').click(function(){ //Button click 
            $(this).text('Loading...'); // button text
            var data = {
                'action': 'loadmore',
                'query': true_posts,
                'page' : current_page
            };
               $.ajax({ // ajax call
                url:ajaxurl,
                data:data,
                type:'POST',
                success:function(data){
                    if( data ) {
                        $('#true_loadmore').text('Load more').before(data);
                        current_page++;
                        if (current_page == max_pages) $("#true_loadmore").remove();
                    } else {
                        $('#true_loadmore').remove();
                    }
                }
            })
        });

Maybe i should use some code for using shortcodes in ajax? I tried to use wp_localize_script but it doesnt work.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.314215 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO