Force reload after changing listing category in Add/Edit listing page

Hello

I use filters (add_filter) to reorder the listing form in the Add or Edit Listing templates. When a user wants to submit a listing for the first time, he will be asked to select the category first. That seems to reload via AJAX. Problem is that my filters don’t apply until the page is reloaded, and I have to ask users to refresh the page so the form is reordered (and some fields relabeled) like it should.

Is there a way to force a reload after selecting category? Or to make sure the add_filter executes after choosing a category?

Thank you

Hello,
I guess you can use a script like this

jQuery(function($){
  $(document).on('change', 'select[name="categories"], select[name="categories[]"]', function(){
    window.location.reload();
  });
});

or this

jQuery(function($){
  // Listen for the 'change' event on the category select fields.
  $(document).on('change', 'select[name="categories"], select[name="categories[]"]', function(){
    
    // Check if the selected value is not empty (i.e., a category has actually been chosen).
    if ($(this).val()) {
      
      // Introduce a small 100ms delay to allow other scripts to finish.
      setTimeout(function(){
        window.location.reload();
      }, 100);
    }
  });
});

Hi

thanks, I tried both and they both keep reloading the page endlessly.

Seems that the onchange event fires when the changed element is out of focus. Basically all the time

I tried a simpler way that works partially. Added the html tag oninput=“window.location.reload()” to the Categories select field. Oninput executes only after you select a new input. The problem is that the page reloads, but the category is not changed. If I reload again, the category is changed. I don’t know why, maybe it needs a delay.

So what happens is-> I select new category-> page reloads (thanks to html tag) but shows old category-> I manually refresh->page reloads with correct new category

Seems like the html tag executes so quickly that it doesn’t allow the category field to refresh

Any help?

Hey @Escape,

I know it’s a different request, but the solution in this topic might help point you in the right direction:

I hope this helps!

Cheers,
Chris :victory_hand:

2 Likes

definitely needs
Hi Chris, I use that kind of snippet too.

Thanks

I checked. Maybe what @ihor posted could help, changing it for the listing-submit form, but I am not a developer so I don’t really understand everything that’s happening there and it might have unintended consequences. I’d rather wait to see if they have any answer

Please post the snippet you use for re-ordering fields (if it’s too complex you can post a sample with changing order for a single field), there may be an issue in the snippet because the form hooks are available in all contexts including AJAX. Another possible reason is that the form is cached (e.g. via a plugin or hosting-side caching) and when the form is re-loaded via REST API (AJAX) the cache is disabled so the custom code snippet is applied.

Sure, here is the partial snippet:

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form ) {

		if(isset($form['fields']['categories'])){
			$form['fields']['categories']['_order'] = 1;
		}
			
		if(isset($form['fields']['title'])){
			$form['fields']['title']['_order'] = 3;
		}

        return $form;
	},
	1000
);

I will check cache settings now

Hi,

We have tested the snippet, and it works on our end. If you are using a caching plugin, ensure that caching is disabled for logged-in users. The hook functions by always applying when the form is not cached.

Sorry

I found the problem. I use the WP Code snippets plugin. It has an option to execute snippets with conditional logic (for example, when page url contains submit-listing). In this case, those snippets only apply the filters when I refresh the page.

For those using this plugin, do not use frontend conditional logic to execute filtering snippets

Thanks everyone anyways

2 Likes