• Resolved amasuriel

    (@amasuriel)


    I am new to wordpress development and am getting very frustrated with this.

    First, what I am trying to accomplish is this: I have a page template (and a published page) that has some form data (some dropdowns and a text field). I want to take these field values and query wp_usermeta to get a table with cetain meta_key / meta_value pairs…I am not using the info to get posts.

    First I had a terrible time figuring out how to pass parameters to the results page at all, since having a submit button appends the Submit x and y values etc to the post.

    Eventually after some trial and error and a lot of searching I found this: /www.rlmseo.com/blog

    I did this, and now I’m closer but not there yet.

    See adding a filter like add_filter('query_vars', 'add_query_vars'); in my functions.php (in my theme) did not add the variables returned in the add_query_vars method to query_vars, at least by the time it got to my templated pages.

    Adding add_filter('request', 'add_query_vars');got it to show up sortof in the template…but instead of getting a query_var with a key= to my vars in add_query_vars with a value of the rewritten querystring parameter (if it existed) I get a key=0, 1 etc with a value equal to the vars returned in add_query_vars.

    I hope that makes sense. I can post additional code if that would be helpful. Any insights would be great…I have spent way to long trying to get a simple search working.

Viewing 15 replies - 1 through 15 (of 39 total)
  • Thread Starter amasuriel

    (@amasuriel)

    Either everyone is as lost as me or the gurus are all at lunch I guess ??

    I did have a read of your post earlier, but like you’ve suggested above, you could do with providing more code, if not only to create a little more context for what you’re asking.

    Put your template code(or associated code) into a pastebin and the post the link here.

    Not making any promises i can help, simply saying the thread could do with having a broader example of the code you’re working with.

    Thread Starter amasuriel

    (@amasuriel)

    Okay. So I have the following code in my functions.php of my theme:
    https://pastebin.com/6dVyTBiz

    I also have the following templated page with the permalink of

    https://blogurl/advertisers

    In the template code (shortened for clarity) I have:
    https://pastebin.com/46eNzQdC

    Thread Starter amasuriel

    (@amasuriel)

    Let me ask this another way if that’s too big a blob of stuff to parse;

    If I want to pass arbitrary parameters to a Page that has a template so the template PHP code can use them, given that I am using permalinks so using regular get parameters doesn’t work (I get 404 errors if I try to append parameters to a permalinked page such as https://blogurl/permalink/?param=value)…

    what is the easiest way to do that?

    Edit: Yes I have read blog posts and the APIs…I still need some help.

    Sorry not been at the PC, but you should you be able to pass around parameters to permalink’ed pages just fine..

    ie. example.com/pagename/?param=value

    Of course it may be that you need to query like so..

    example.com/pagename/&param=value

    .. thinking out loud here, but in terms of the real URL, there’s already an existing param…

    ie. example.com/?pagename=pagename (or whatever the real query var is)..

    My point being, it’s possible your passed parameter is seen as being a secondary parameter, therefore it’s being treated as ..

    example.com/?pagename=pagename?param=value

    It’s just a theory.. but it could be something that simple.. give me 5-10 minutes and i’ll have a quick skim through your code aswell…

    Can’t see any immediate problems with what you have so far…

    Are the custom rewrite rules appearing in the rewrite array ok, and are you able to query them ok (redirects aside)?

    ie. example.com/advertisers/whatever

    There is a specific filter available if you only want to add page rewrites, there’s not necessarily any reason you need to run a filter on the complete rewrite array..

    I’m not sure that you need to flush the rules either, it’s not usually a good idea to flush the rules on every page load, which i think is what you have by hooking onto init.. You could avoid that step by simply visiting your permalinks page and clicking save (after each change to the rewrite array).. although i’ve not had first-hand experience in flusing the rewrite array.. it’s just a suggestion.. (because to me it looks like you’re constantly flushing the array which seems overkill or needless)..

    I’ll go test your functions though and see how they work for me…

    Seem to be having the same issue with adding query vars to the array, they show up just fine if you print them out inside the function, outside the function it’s as if they never registered..

    Using request, the query var array key name gets assigned to 0, 1, and 2 respectively..

    Odd because, every guide for adding query vars suggest the method you were using (not the request approach, the non-working one i’m playing with), as does nearly every plugin i’ve found that adds to the array, yet i’m seeing the same issue as you’ve described…

    I’ll figure out how the query vars thing works though, because i’m sure i’ll want to use it at some point… so bear with me and i’ll tinker.. and work out what’s going wrong (if i can, i’m intrigued now).

    Ok, it’s starting to make sense now..

    I’ve got all three of your query vars working on a test page called advertisers. I’ll see about making the rewrite rules work correctly to, so far i’m using..

    wordpress.localhost/advertisers/?province=a&city=b&serviceField=c
    NOTE: You don’t use an ampersand when using permalinks as per my theory earlier.

    a,b,c are just test values, but they are in the query vars array.. (and i’m using a filter on query_vars not request) …

    I’ll give you the code as it works for me (once it works, and you can go from there) … just going to figure out the rewrite part and i’ll post code up with a little explanation..

    EDIT: The first rewrite actually works … my test URL below.
    wordpress.localhost/advertisers/a/?city=b&serviceField=c
    NOTE: Again my test value is simply a for province, and the query vars are shown in the query vars array.

    Ok, here’s the two functions i’ve been using, taken directly from the code you posted, i simply re-saved permalinks after each change to the new rules.

    function search_services_params( $query_v ) {
    	$query_v[] = "province";
    	$query_v[] = "city";
    	$query_v[] = "serviceField";
    	return $query_v;
    }
    function custom_rewrite_rules( $existing_rules ) {
    	$new_rules = array(
    		'advertisers/([^/]+)/([^/]+)/([^/]+)/?$' =>
    			'index.php?pagename=advertisers&province=$matches[1]&city=$matches[2]&serviceField=$matches[3]',
    
    		'advertisers/([^/]+)/([^/]+)/?$' =>
    			'index.php?pagename=advertisers&province=$matches[1]&city=$matches[2]',
    
    		'advertisers/([^/]+)/?$' =>
    			'index.php?pagename=advertisers&province=$matches[1]'
    	);
    	$existing_rules = $new_rules + $existing_rules;
    	return $existing_rules;
    }
    add_filter('rewrite_rules_array', 'custom_rewrite_rules');
    add_filter('query_vars',  'search_services_params');

    With the above code i can query like so..

    wordpress.localhost/advertisers/a/b/c/

    The query vars array will look something like this at that point (trimmed just to show the necessary bits).

    Array
        (
            [pagename] => advertisers
            [province] => a
            [city] => b
            [serviceField] => c
            [error] =>
            [m] => 0
            [p] => 0

    When you add a filter to query_vars all you’re doing is telling WordPress which query vars it will accept from the requested URL..

    So for example, if i requested..

    example.com/?wordpress=cool&spam=bad&t31os=figuringitout&you=listeningcarefully&humour=nottogreat

    And then had the following filter on query vars..

    function search_services_params( $query_v ) {
    	$query_v[] = "wordpress";
    	$query_v[] = "t31os";
    	return $query_v;
    }

    WordPress would then take those two vars (wordpress and t31os) and pass them into the query, the rest just get ignored.

    So the filter doesn’t register them into the array permenantly so to speak, it determines what vars it will pull from the requested URL and pass into the query, if you don’t request them in the URL, you won’t see them in the query vars array ..

    Does that help clear anything up? ??

    Furthermore, in troubleshooting this problem with you i’ve gained a whole new understanding of how those query vars and the filter on query vars actually works, so i’ve come away from this with my eyes opened a little more.. so thanks for prompting me to help.. ??

    Thread Starter amasuriel

    (@amasuriel)

    Holy (something)! I had given up on getting replies and when I check this morning there is a novel ??

    Let me review all this and give it a try. Thank you so much for all your help!

    Thread Starter amasuriel

    (@amasuriel)

    Grr, I think I found the issue, now I just have to figure out how to fix it.

    I used what you wrote and it still didn’t work for me, so I just went about debugging line by line the whole call.

    The query_vars do actually get added properly…until do_action(‘template_redirect’); gets called in template-loader.php. Then all the whole request gets rewritten and my added query_vars get lost.

    Thread Starter amasuriel

    (@amasuriel)

    So the do_action(‘template_redirect’) eventually calls canonical.php, which attempt to figure out the permalink and redirects to it (losing all my parameters in the process; yay).

    I don’t really feel comfortable mucking about in the core wordpress page loading code; I feel like its likely I would inadvertently break something else if I did.

    Anyone have any idea how I can get around this? I assume it must be possible, as surely others have permalinked pages they pass values to.

    The query_vars do actually get added properly…until do_action(‘template_redirect’); gets called in template-loader.php. Then all the whole request gets rewritten and my added query_vars get lost.

    So do you think this is simply a consequence of putting the code into a page template?

    If i can get a better idea of why you’re being re-directed then i might be able to offer a suggestion or two.

    template-loader.php will still fire and call the necessary template on my install, else it’ll have no idea what file to load (in my case the theme’s page.php).

    I’m trying to wrap my head round why what you’re doing is any different to how i tested the code, other then the fact you’re using a page template and i wasn’t, but i can test for that to if you think that’s the problem.

    Thread Starter amasuriel

    (@amasuriel)

    I’m 100% sure the template is the problem after my investigation this morning.

    The flow is:
    Called https://blogurl/advertisers/province/city/service
    I follow the code through index.php, which basically just includes wp-blog-header.php.

    wp-blog-header.php looks like:` $wp_did_header = true;

    require_once( dirname(__FILE__) . ‘/wp-load.php’ );
    wp();
    require_once( ABSPATH . WPINC . ‘/template-loader.php’ );`

    The wp() function is what parses the rewrite rules. At the end of this call my query_vars are set correctly.

    Then as you can see it calls template-loader.php. This first this template-loader.php does is call do_action(‘template_redirect’)

    In the do_action(‘template_redirect’) is part of plugin.php. It does this:

    do {
    		foreach ( (array) current($wp_filter[$tag]) as $the_ )
    			if ( !is_null($the_['function']) )
    				call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
    
    	} while ( next($wp_filter[$tag]) !== false );

    And one of the filters that it calls redirect_canonical in canonicals.php, which as I mentioned finds the permalink URL for whatever its passed and redirects to it…which calls index.php again, only this time the URL is just https://blogurl/advertisers.

    I’m sure you will be able to reproduce if you try to use a Page with a template.

    Ok, so when you request..
    blogurl/advertisers/province/city/service

    You’re being redirected to..
    blogurl/advertisers

    And you believe this problem relates to using a page template?..

    Just so i’m clear on what it is you’re saying ..

Viewing 15 replies - 1 through 15 (of 39 total)
  • The topic ‘Problems adding to query_vars’ is closed to new replies.