Wordpress : Url Rewrite - wordpress

I am trying to rewrite a url in wordpress so that I can serve up dynamic content based on variables that are passed. I have a plug in that needs variable data passed into it. Currently I have:
http://xyzsite.com/page/?var1=something
this works fine and passes in a $_GET var. So my next step is to clean up the variable so that it looks like
http://xyzsite.com/page/something
I have done a few google searches and come accross some site that looked promising but I cannot get any of them to work. From what I have read, i need to use
add_rewrite_tag and add_rewrite_rule
After reading through the articles I have added this to my functions.php page:
add_rewrite_tag('%var1%','([^&]+)');
add_rewrite_rule('^page/([^&]+)/?','index.php?p=1141&var1=$matches[1]','top');
when i navigate to the page http://xyzsite.com/page/something i get a 404 error. When i navigate the to http://xyzsite.com/page/?var1=something it is still working fine. So it looks as if my rewrite is not registering or working correctly.
Can someone help me to achieve the above rewrite. FYI my permalink settings is set to post name if that matters at all. Thank you.

I̶'̶m̶ ̶n̶o̶t̶ ̶a̶ ̶r̶e̶g̶e̶x̶ ̶p̶r̶o̶,̶ ̶b̶u̶t̶ ̶I̶ ̶s̶u̶s̶p̶e̶c̶t̶ ̶a̶n̶ ̶i̶s̶s̶u̶e̶ ̶i̶n̶ ̶y̶o̶u̶r̶ ̶r̶e̶w̶r̶i̶t̶e̶ ̶r̶u̶l̶e̶:̶
a̶d̶d̶_̶r̶e̶w̶r̶i̶t̶e̶_̶r̶u̶l̶e̶(̶'̶^̶p̶a̶g̶e̶/̶(̶[̶^̶&̶]̶+̶)̶/̶?̶'̶,̶'̶i̶n̶d̶e̶x̶.̶p̶h̶p̶?̶p̶=̶1̶1̶4̶1̶&̶v̶a̶r̶1̶=̶$̶m̶a̶t̶c̶h̶e̶s̶[̶1̶]̶'̶,̶'̶t̶o̶p̶'̶)̶;̶
̶
̶N̶o̶t̶e̶ ̶t̶h̶e̶ ̶/̶?̶ ̶y̶o̶u̶'̶v̶e̶ ̶a̶d̶d̶e̶d̶ ̶a̶t̶ ̶t̶h̶e̶ ̶e̶n̶d̶.̶ ̶T̶h̶u̶s̶ ̶y̶o̶u̶ ̶s̶h̶o̶u̶l̶d̶ ̶a̶c̶c̶e̶s̶s̶ ̶y̶o̶u̶r̶ ̶p̶a̶g̶e̶ ̶w̶i̶t̶h̶ ̶h̶t̶t̶p̶:̶/̶/̶x̶y̶z̶s̶i̶t̶e̶.̶c̶o̶m̶/̶p̶a̶g̶e̶/̶s̶o̶m̶e̶t̶h̶i̶n̶g̶/̶?̶ ̶a̶n̶d̶ ̶n̶o̶t̶ ̶x̶y̶z̶s̶i̶t̶e̶.̶c̶o̶m̶/̶p̶a̶g̶e̶/̶s̶o̶m̶e̶t̶h̶i̶n̶g̶.̶ ̶H̶a̶v̶e̶ ̶y̶o̶u̶ ̶t̶r̶i̶e̶d̶ ̶i̶f̶ ̶t̶h̶a̶t̶ ̶w̶o̶r̶k̶s̶?̶
The stroked out text above is wrong, as Gustavo Straube pointed out in the comments. Please disregard that proposed solution.
My only last advice is to try adding a flush_rules(); after your last add_rewrite_rule, as stated in http://codex.wordpress.org/Rewrite_API/flush_rules.
Note that you should be accessing your query vars with get_query_var('var_name') instead of trying to access $_GET directly.

Related

Woocommerce Underscore is added to some product URLs

Got an unusual problem that started recently and wonder if anyone has any ideas why it's happening and how to stop it.
Some of my products are getting an underscore added to the url when selected which, naturally, results in a 404 error. It's consistent - either an underscore gets added or it doesn't. It doesn't seem to be related to the GUID or the post_name in the wp_posts table. It also doesn't matter if you call it using the URL or the post ID so the following:-
Bad index.php?page_id=1075707 the-john-rutter-christmas-piano-album-2 https://website-name.com/product/the-john-rutter-christmas-piano-album-2/
Good index.php?page_id=1087442 the-john-rutter-piano-album https://website-name.com/product/the-john-rutter-piano-album/
Bad index.php?page_id=1159681 parable-for-harpsichord https://website-name.com/product/parable-for-harpsichord/
Good index.php?page_id=1159684 jesu-meine-freude-2 https://website-name.com/product/jesu-meine-freude-2/
Bad here meaning that the URL appears with an underscore such as
https://website-name.com/product/parable-for-harpsichord_/
I've also tried this with every single plugin, except WooCommerce, de-activated and with my functions.php disabled. No difference.
I've tried using the URLs as given above, C&P or typed in manually and by directly referencing the post ID (as shown).
This has only recently started and I'm at a bit of a loss here. I don't know how widespread this problem is either since I've over 120,000 products and can't check them all by hand. Fortunately, this isn't live yet.
Any suggestions or ideas here would help me.
There appears to be a hidden character at the end of the post_name field. Deleting and recreating the field got rid of the problem.

Querying Two Wordpress Categories via URL

I have posts that are assigned to two categories let's say cars and paints. I'm trying to only get posts that are in both, the following code http://example.com/category/cars+paints is supposed to work according to this article but it doesn't and I get a page saying The requested document was not found on this server. When I try http://exapmle.com/category/cars,paints it works but it gives me too many posts.
It seems that & is supposed to work as well, I tried http://exapmle.com/category/cars&paints and http://exapmle.com/category/?cat=cars&cat=paints but this doesn't either work, I don't know if I have the syntax wrong.
When I try http://exapmle.com/?cat=1&cat=2 using the category IDs it directs to a page like this http://exapmle.com/category/paints with the last parameter corresponding to the last number entered in the URL.
Does anyone know why it's not working and what I can do to get it to work, thanks in advance.
Changing the + to a space %20 seems to be working for me
Example:
http://example.com/category/cars%20paints

WP Rest API: Can't seem to get embedded data from the post object

I am working with the WORDPRESS REST API and Wordpress version 4.8 for an internal network page at a local office. We have permalinks disabled (security reasons) and thus I am accessing the posts object like so:
https://url/blogs/usernamehere/?rest_route=/wp/v2/posts/12345
I am able to do a GET request and can get the posts data into my view template with no issues. However, I can't seem to figure out how to consume additional content in the post object . I have followed the documentation and tried to do:
https://url/blogs/usernamehere/?rest_route=/wp/v2/posts/12345?_embed=true
But, I get a STATUS 404 .
How would I correctly apply the embed function in the URL so I can get the additional data associated with the post?
Pass the _embed global parameter without a value, per the documentation.
you should do this:
https://url/blogs/usernamehere/?_embed=true&rest_route=/wp/v2/posts/12345
Basically you add _embed=true at before other parameters and after & add other parameters.

CMS links on frontend not converting ie href=[sitetree_link_id=xx]

An issue has been noticed on one of our old sites running 2.4 where when the user creates a link in the CMS content, selecting an existing page to link to, the link is not being converted to the actual URL on the front end and all links are coming through in the format of <a href="[sitetree_link_id=12]">
What would be causing this and how do I fix it?
The tag looks like it's being set incorrectly. It should be [sitetree_link id=12], not [sitetree_link_id=12].
We later added support to the parser for [sitetree_link,id=12] so that links didn't need to contain spaces, but I can't recall if that's in 2.4 or only 3.0+.
Can you confirm that your WYSIWYG insertion is putting in that errant _? If so, you might want to checkout the handleaction_insert function in tiny_mce_imporvements.js to confirm that it has a line like so:
case 'internal':
href = '[sitetree_link id=' + this.elements.internal.value + ']';
If the inserted links don't actually have the errant _ but they aren't being parsed, then try checking your sapphire/_config.php file for this:
ShortcodeParser::get('default')->register('sitetree_link', array('SiteTree', 'link_shortcode_handler'));
If your site makes changes to the ShortcodeParser at all you might have inadvertently turned off sitetree_link support.
If all of that looks in order, perhaps the ShortcodeParser isn't being called for some reason. In HTMLText::forTemplate(), put a debug statement (I like die("I got here!");) to confirm that HTMLText::forTemplate() is actually getting called. If it's not, you might need to manually call it in some pre-processing of your Content variable. Instead of this:
$content = $this->Content;
Do this:
$content = $this->obj('Content')->forTemplate();
I hope that one of those answers help. Either way, it would be great if you could post back, so we could isolate what caused this. It might help us make the API easier to use in SilverStripe 3.1.

How to OR solr term facets via the search URL in Drupal 7 site?

I have a Drupal 7 website that is running apachesolr search and is using faceting through the facetapi module.
When I use the facets to narrow my searches, everything works perfectly and I can see the filters being added to the search URL, so I can copy them as links (ready-made narrowed searches) elsewhere on the site.
Here is an example of how the apachesolr URL looks after I select several facets/filters:
search_url/search_keyword?f[0]=im_field_tag_term1%3A1&f[1]=im_field_tag_term2%3A100
Where the 'search_keyword' portion is the text I'm searching for and the '%3A' is just the url encoded ':' (colon).
Knowing this format, I can create any number of ready-made searches by creating the correct format for the URL. Perfect!
However, these filters are always ANDed, the same way they are when using the facet interface. Does anyone know if there is a syntax I can use, specifically in the search URL, to OR my filters/facets? Meaning, to make it such that the result is all entries that contains EITHER of the two filters?
Thanks in advance for any help or pointers in the right direction!
New edit:
I do know how to OR terms within the same vocabulary through the URL, I'm just wondering how to do it for terms in different vocabularies. ;-)
You can write a filter query that looks like:
fq=field1:value1 OR field2:value2
Alternatively you can use localparams to specify the query operator:
fq={!q.op=OR}field1:value1 field2:value2
As far as I know, there's not any easier way to do this. There is, in fact, an rather old bug asking for a way to OR the fq parameters...
I finally found a way to do this in Drupal
Enable the fq parameter setting.
Go to admin/config/search/apachesolr/[your_search_page]/core_search/edit or just navigate to the settings of the search page you're trying to modify
Check the 'Allow user input using the URL' setting
URL Syntax
Add the following at the end of the URL: ?fq=tid:(16 OR 38), where 16 and 38 are the term ids

Resources