the situation :
my settings > permalinks are sets on post name, but sometimes anyway a post url is using the query post id :
https://my_website.com/?p=4348
instead of https://my_website.com/my_post
I identified in which situation it happens : when the post status is draft instead of publish. I let the connected users see their page in draft, but in this case the url does not uses the post_name anymore, but a query with the post_id.
the problem :
I want to be safe from this when I'm in production, here is the problem it causes :
On posts i have links like <a href="?action=my_action"></a>
, they are supposed to produce this kind of url :
https://my_website/my_post/?action=my_action
but instead, if the page url was using the query and not the post_name, i get this url :
https://my_website/?action=my_action
because the new query replace the previous one. So i loose the information of the page on which I want to execute the action.
solutions ?
I cannot dynamically create the link to add the information in the query for example (I'm not the one creating the front, and the links are created with a front builder, divi)
I've tried to get the current url, check if it uses the wrong pattern, and in that case redirect to the right pattern, but I could not. First I created the right url with home_url('path/'.$post->post_name)
, then i wanted to compare it to the current url, but I could not reliably find it ? I've tried with get_permalink();
, global $wp; $wp->request;
, and get_query_vars()
, but it failed.
I also tried to check from which page the url comes, but i use the request referer for that and I think I cannot rely on it ? Anyway this option is not optimal since sometime i cannot differentiate the action by the page i come from, in case there is action on both pages.