Alecaddd / Sunset-theme

Premium WordPress theme built during this series of tutorials: https://www.youtube.com/playlist?list=PLriKzYyLb28kpEnFFi9_vJWPf5-_7d3rX
GNU General Public License v2.0
330 stars 354 forks source link

URL injecting with Ajax #6

Open Si-HaMaDa opened 6 years ago

Si-HaMaDa commented 6 years ago

when the site is in a subdirectory the URL is not working probably in the index but works fine in archive page cause we preset the URL with "data-archive" and passing it... for example is the site URL is http://localhost/wordpress in the homepage when scrolling to first div of "page-limit" the URL becomes http://localhost/ or when load more becomes http://localhost/page/2 So here's my solution I hope it's good in index `<?php if( is_paged() ): ?>

            <div class="container text-center container-load-previous">
                <a class="btn-sunset-load sunset-load-more" data-prev="1" data-homepage="<?php echo sunset_grab_current_uri(); ?>" data-page="<?php echo sunset_check_paged(1); ?>" data-url="<?php echo admin_url('admin-ajax.php'); ?>">
                    <span class="sunset-icon sunset-loading"></span>
                    <span class="text">Load Previous</span>
                </a>
            </div><!-- .container -->

        <?php endif; ?>

        <div class="container sunset-posts-container">

            <?php 

                if( have_posts() ):

                    echo '<div class="page-limit" data-page="' . sunset_grab_current_uri() . '">';

                    while( have_posts() ): the_post();

                        /*
                        $class = 'reveal';
                        set_query_var( 'post-class', $class );
                        */
                        get_template_part( 'template-parts/content', get_post_format() );

                    endwhile;

                    echo '</div>';

                endif;

            ?>

        </div><!-- .container -->

        <div class="container text-center">
            <a class="btn-sunset-load sunset-load-more" data-homepage="<?php echo sunset_grab_current_uri(); ?>" data-page="<?php echo sunset_check_paged(1); ?>" data-url="<?php echo admin_url('admin-ajax.php'); ?>">
                <span class="sunset-icon sunset-loading"></span>
                <span class="text">Load More</span>
            </a>
        </div><!-- .container -->`

in sunset.js `$(document).on('click','.sunset-load-more:not(.loading)', function(){

    var that = $(this);
    var page = $(this).data('page');
    var newPage = page+1;
    var ajaxurl = that.data('url');
    var prev = that.data('prev');
    var archive = that.data('archive');
    var homepage = that.data('homepage');

    if( typeof prev === 'undefined' ){
        prev = 0;
    }

    if( typeof archive === 'undefined' ){
        archive = homepage;
    }

    that.addClass('loading').find('.text').slideUp(320);
    that.find('.sunset-icon').addClass('spin');

    $.ajax({

        url : ajaxurl,
        type : 'post',
        data : {

            page : page,
            prev : prev,
            archive: archive,
            action: 'sunset_load_more'

        },
        error : function( response ){
            console.log(response);
        },
        success : function( response ){

            if( response == 0 ){

                $('.sunset-posts-container').append( '<div class="text-center"><h3>You reached the end of the line!</h3><p>No more posts to load.</p></div>' );
                that.slideUp(320);

            } else {

                setTimeout(function(){

                    if( prev == 1 ){
                        $('.sunset-posts-container').prepend( response );
                        newPage = page-1;
                    } else {
                        $('.sunset-posts-container').append( response );
                    }

                    if( newPage == 1 ){

                        that.slideUp(320);

                    } else {

                        that.data('page', newPage);

                        that.removeClass('loading').find('.text').slideDown(320);
                        that.find('.sunset-icon').removeClass('spin');

                    }

                    revealPosts();

                }, 1000);

            }

        }

    });

});`

in ajax.php `function sunset_load_more() { $paged = $_POST['page'] + 1; $prev = $_POST['prev']; $archive = $_POST['archive'];

if ($prev == 1 && $_POST['page'] != 1) {
    $paged = $_POST['page'] - 1;
}

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'paged' => $paged,
);

if ($archive != '0') {
    $archVal = explode('/', $archive);
    $flipped = array_flip($archVal);

    switch (isset($flipped)) {

        case $flipped['category']:
            $type = 'category_name';
            $key = 'category';
            break;

        case $flipped['tag']:
            $type = 'tag';
            $key = $type;
            break;

        case $flipped['author']:
            $type = 'author';
            $key = $type;
            break;

    }

    $currKey = array_keys($archVal, $key);
    $nextKey = $currKey[0] + 1;
    $value = $archVal[ $nextKey ];

    $args[ $type ] = $value;

    //check page trail and remove "page" value
    if (isset($flipped['page'])) {
        $pageVal = explode('page', $archive);
        $page_trail = $pageVal[0];
    } else {
        $page_trail = $archive;
    }
} else {
    $page_trail = '/';
}

$query = new WP_Query($args);

if ($query->have_posts()):

    echo '<div class="page-limit" data-page="'.$page_trail.'page/'.$paged.'/">';

while ($query->have_posts()): $query->the_post();

get_template_part('template-parts/content', get_post_format());

endwhile;

echo '</div>'; else:

    echo 0;

endif;

wp_reset_postdata();

die();

} and as improvment in archive.php <?php if( is_paged() ): ?>

            <div class="container text-center container-load-previous">
                <a class="btn-sunset-load sunset-load-more" data-prev="1" data-archive="<?php echo sunset_grab_current_uri(); ?>" data-page="<?php echo sunset_check_paged(1); ?>" data-url="<?php echo admin_url('admin-ajax.php'); ?>">
                    <span class="sunset-icon sunset-loading"></span>
                    <span class="text">Load Previous</span>
                </a>
            </div><!-- .container -->

        <?php endif; ?>

        <div class="container sunset-posts-container">

            <?php

                if( have_posts() ):

                    echo '<div class="page-limit" data-page="' . sunset_grab_current_uri() . '">';

                    while( have_posts() ): the_post();

                        get_template_part( 'template-parts/content', get_post_format() );

                    endwhile;

                    echo '</div>';

                endif;

            ?>

        </div><!-- .container -->

        <div class="container text-center">
            <a class="btn-sunset-load sunset-load-more" data-page="<?php echo sunset_check_paged(1); ?>" data-archive="<?php echo sunset_grab_current_uri(); ?>" data-url="<?php echo admin_url('admin-ajax.php'); ?>">
                <span class="sunset-icon sunset-loading"></span>
                <span class="text">Load More</span>
            </a>
        </div><!-- .container -->`

what I did is passing the full URL like you did in archive.php I passed the full URL even in index... I hope you see it and tell if there's something wrong in my code

iswaq commented 5 years ago

I have the same issue, is there any solution?

Si-HaMaDa commented 5 years ago

I have the same issue, is there any solution?

I've fixed it... copy and paste the code in your files and try also you can read to understand what I did

iswaq commented 5 years ago

I just uploaded my theme on the hosting server, it is working fine there. It means, it is creating problem with localhost.

Si-HaMaDa commented 5 years ago

I just uploaded my theme on the hosting server, it is working fine there. It means, it is creating problem with localhost.

the problem isn't with local host or server the problem will appear with sub-directory even on you server try to make a wordpress installation on sub-directory on your server and see