Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author TC.K

    (@wp_dummy)

    Why don’t use a custom template to display the posts? With custom template you can defined the custom query load first when the page load.

    Thread Starter NeoBean

    (@neobean)

    I hadn’t thought of custom templates- what plugin hook could I use to do that? An example would be awesome, but I know you have limited time :]

    The other thing though is that setting up a custom template for each could be unnecessary duplicate work- if a URL parameter were used, we could re-use the same page/code and create things on the fly. So instead of, say 20 pages, I could just have 20 URLs instead.

    Thanks so much!

    Plugin Author TC.K

    (@wp_dummy)

    Actually custom template is the wp default feature, not in my plugin.
    You can check on this documentation of how to create a custom template with custom query.

    Thread Starter NeoBean

    (@neobean)

    Ah, I see what you’re saying; maybe I can make what I’m trying to do a bit more clear-

    Say I have a search form that has 3 taxonomies- Category, Genre, and Artist. The search form has checkboxes for the categories, genres, and artists, which is great. What I want to do, is instead of making the user click on the “Rock and Roll” checkbox under the “Genre” taxonomy, is I give them a URL that comes with “Rock and Roll” already checked and the R&R results filtered. Like so- https://example.com/?filter=rockandroll

    So far I already have code to grab which URL parameter is used, so I just want to pass that in and POST to update the page after it loads (or ideally before). Can you tell me what event/function on the page sends the POST to update the results for checkboxes? I’ve looked in uwpqsfscript.js and saw the jQuery.ajax call but am not sure exactly how this is used to trigger the update. Thanks!

    Plugin Author TC.K

    (@wp_dummy)

    I am not quite sure what you meant, but if you want to submit the query on page load you can use:
    $('#uwpqsffrom_{form_id} .usearchbtn').click();

    Thread Starter NeoBean

    (@neobean)

    I got this to work using a slightly different call. I copied the process_data function into a header script that then calls

    process_data($(‘#tchkb-0-‘+filterBy).click());

    where filterBy is the parameter passed in from the URL; you’ll also notice I had to edit the front-class.php file to append -‘.$value.’ to each ID for my checkboxes so they could be programmatically selected.

    Works great!

    Hi NeoBean! I am looking to do exactly the same thing, is it possible to share your code?

    Thread Starter NeoBean

    (@neobean)

    Hey slickorange, sorry for the delay! This is what I did, hope it still helps.

    1) Paste this into a new .js file-

    <!-- Load page with some boxes pre-checked -->
    var urlParams;
    (window.onpopstate = function () {
        var match,
            pl     = /\+/g,  // Regex for replacing addition symbol with a space
            search = /([^&=]+)=?([^&]*)/g,
            decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
            query  = window.location.search.substring(1);
    
        urlParams = {};
        while (match = search.exec(query))
           urlParams[decode(match[1])] = decode(match[2]);
    })();
    
    var filterBy = urlParams["filter"];
    
    jQuery(document).ready(function ($) {
    
    	window.process_data = function ($obj) {
    
    			var ajxdiv = $obj.closest("form").find("#uajaxdiv").val();
    			var res = {loader:$('<div />',{'class':'umloading'}),container : $(''+ajxdiv+'')};
    
    			var getdata = $obj.closest("form").serialize();
    			var pagenum = '1';
    
    			jQuery.ajax({
    				 type: 'POST',
    				 url: ajax.url,
    				 data: ({action : 'uwpqsf_ajax',getdata:getdata, pagenum:pagenum }),
    				 beforeSend:function() {$(''+ajxdiv+'').empty();res.container.append(res.loader);},
    				 success: function(html) {
    					res.container.find(res.loader).remove();
    				  $(''+ajxdiv+'').html(html);
    
    				 }
    			});
    
    	}	
    
    	process_data($('#tchkb-0-'+filterBy).click());
    	process_data($('#tchkb-1-'+filterBy).click());
    	process_data($('#tchkb-2-'+filterBy).click());
    	process_data($('#tchkb-3-'+filterBy).click());
    
    });

    Add or remove process_data calls according to how many you need- one for each section.

    2) Link that file in your header. WP offers many ways of doing this- plugin, functions.php, or direct if you’re positive that’s what you want to do.

    3) Edit uwpqsf-front-class.php to include the id value, so around line 37 in mine-

    $html .= '<label><input type="checkbox" id="tchkb-'.$c.'-'.$value.'" name="taxo['.$c.'][term][]" value="'.$value.'" >'.$term->name.'</label>';

    4) Then create links with ?filter=VALUE at the end to filter by the value you want to filter by. Also note the value should be formatted as a slug, not with spaces or stuff.

    I think that should be everything. It’s been a while since I implemented this so things are a bit fuzzy. Good luck!

    Thanks NeoBean, I managed (your initial post did steer me in the right direction) but I am sure your code will help somebody else.. My requirements were a bit more basic. The code I used is this:

    jQuery(document).ready(function($) {
             $('[id^=tdp-] option[value="<?php echo $select_value?>"]').attr("selected", "selected");
    
             function loadAjax(){
                var obj = $("[id^=uwpqsffrom_]");//remember to replace the form id
    
                process_data(obj);
                 return false;
                }
                window.onload = loadAjax;
    
        });

    The nice thing is that even if I add another taxonomy dropdown filter the code will need no adjustment. You can see it in use here: https://www.movingintoaction.co.za/category/directory

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Using URL parameters’ is closed to new replies.