[Resolved] More precise way to manipulate/target output using AJAX.

Home Forums Support [Resolved] More precise way to manipulate/target output using AJAX.

Home Forums Support More precise way to manipulate/target output using AJAX.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1946254
    Frank

    Hi,

    I was wondering if there is a more ‘targeted’ way of triggering GeneratePress filter settings to process/output AJAX requests/data instead of using a very global term, like for example:

    function set_gp_columns( $count ) {
       if ( wp_doing_ajax() ) {
          return 25;
       }
       return $count;
    }
    add_filter( 'generate_blog_get_column_count','set_gp_columns' );

    This would affect all output using AJAX in every location it’s being used.

    As an extension of the previous question, is there a way I can use the “option_generate_blog_settings” filter to target AJAX output, or maybe a recommended way for me to include the source of the request, so I can tell the output to use the “option_generate_blog_settings” filter” as if it were being displayed when normally loading the Tag Archives for example. I figured if I could somehow include the origin request or filter the data manually using the correct filtering options you provide, I could get the output styled properly according to the page it’s being displayed on.

    Thanks in advance.

    #1947608
    Tom
    Lead Developer
    Lead Developer

    Hi Frank,

    What exactly is the use-case here? I can’t think of a reason you’d want to change a value based on whether AJAX is happening or not.

    I’ve never used wp_doing_ajax(), but I’d be surprised if it was able to overwrite site settings based on an AJAX request on a page. If something has been rendered already it likely won’t re-render just because an AJAX request has happened.

    #1947673
    Frank

    Hey Tom.

    I’m trying to use taxonomies to filter using AJAX.

    Say I’m on a custom post type archive. When you first load the page I can have column width settings and post layout settings by applying ‘generate_blog_get_column_count’ and ‘option_generate_blog_settings’ using ‘is_post_type_archive(‘custom-post-type’) in the filter section.

    When using an AJAX filter I’m taking a custom taxonomy and feeding that to either a new wp_query or get_posts() function. But the query gets done outside of the loop of that archive page since it’s being requested through AJAX. So it’s not applying the column en post styling for that archive page causing the results to have a different layout when returning the data. I was trying to find the correct conditionals in your filters to still apply the correct layout when returning the filtered content.

    Maybe I’m not phrasing it correctly though. Can I for example when using a new wp_query or get_posts function tell WordPress to use a specific GeneratePress generate_blog_get_column_count and option_generate_blog_settings filter to apply to the results without being on a specific page where the filtering is recognized because of the conditinonal that was already in place?

    #1948835
    Tom
    Lead Developer
    Lead Developer

    I don’t believe that’s possible – at least not with AJAX. Using AJAX makes a specific request for your specified items (new posts based on a different query). It doesn’t reload the entire page, so the values you’re wanting to change aren’t able to change – they’ve already loaded/been applied.

    In order to change those, you need WordPress to reload so those filters are read again.

    To change a layout with JS only you would need to change the HTML (add/remove classes) and add the necessary CSS to work with those class changes.

    Hope this helps!

    #1949970
    Frank

    Thanks Tom,

    So just to be sure, on the PHP side when doing a new WP_Query there’s no possible way for me to manually apply the GeneratePress filters/actions during the

    while ( have_posts() ) : the_post();
    get_template_part( 'content' );
    endwhile;

    part?

    For instance when using the Customize section to change the Layout -> Container -> Seperating Space it does affect the data that’s being returned from that same loop. The same with the unusual wp_doing_ajax() in the ‘generate_blog_get_column_count’ filter is affecting the output. ( I was just grasping at straws at this point ) It just feels I’m so close haha.

    Thanks again.

    #1950713
    Frank

    Found a way to achieve the results I was hoping to get. I’m not sure how much it impacts performance though.

    Just to recap what I wanted to achieve:

    On the archive/tag archive pages for posts/custom posts types with (custom) taxonomies I used the filters ‘generate_blog_get_column_count’ and ‘option_generate_blog_settings’ to create a specific layout. Images above the Title and an excerpt etc. This can be easily done using conditionals for these filters. But once you implement an AJAX request the formatting is lost when returning the results.

    To get the image above the text part I first remove them completely from these post types:

    add_filter('generate_featured_image_output', function($image) {
    	
    	$posttype = get_post_type();
    
    	if ('custom-1' == $posttype) {
    		$image = '';
    	}
    	
    	if ( 'custom-2' == $posttype) {
    		$image = '';
    	}
    	
    	if ( 'post' == $posttype) {
    		$image = '';
    	}
       
    	return $image;
    });

    So now on either the normal pageload and the AJAX call the image is removed from the output. After that I placed it above the content in both request types and prevented them from being rendered on single posts :

    add_filter('generate_before_content', function() {
    	if ( wp_doing_ajax() && 'custom-1' == get_post_type() || 'custom-1' == get_post_type() && ! is_single() ) {
    		echo '<div class="post-image">';
    		echo the_post_thumbnail();
    		echo '</div>';
    	}
    
    	if ( wp_doing_ajax() && 'post' == get_post_type() || 'post' == get_post_type() && ! is_single() ) {
    		echo '<div class="post-image">';
    		echo the_post_thumbnail();
    		echo '</div>';
    	}	
    }, 25);

    This way the styling stays the same in both conditions.

    Hope this helps someone facing a similar problem.

    Thanks again.

    #1950876
    Tom
    Lead Developer
    Lead Developer

    Glad you got it sorted – thanks for sharing your code.

    Just to answer your first question – once AJAX fires, it’s too late to alter anything that’s already loaded (like the options you were wanting to filter).

    The above works because the hooks you’re adding to are added/removed with AJAX.

    Thanks again!

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.