How to add several parameters with slash in wordpress post url? - wordpress

I want to add several parameters with slash not by question mark in Wordpress Post URL. Thanks in advance.

You can achieve by WordPress Standard. See below quick step and follow WordPress docx for specific functions/filters, if you get stuck.
You need to use add_rewrite_rule as per WordPress Standard to pass query string as a slash based URL
add_rewrite_rule(
'^pageslug/([^/]+)([/]?)(.*)',
//!IMPORTANT! THIS MUST BE IN SINGLE QUOTES!:
'index.php?pagename=pageslug&page_id=$matches[1]',
'top'
);
});
This filter is used to target the query string variable.
add_filter('query_vars', function( $vars ){
$vars[] = 'pagename';
$vars[] = 'page_id';
return $vars;
});
Update permalink settings
This will be used to get the value in a specific page
get_query_var( 'page_id' )

Related

Rewrite all wordpress site routes to include a new path that's a dynamic query parameter

I'm trying to rewrite all of my site's routes to include a new path that is also a query parameter that can change. For example, I want to change all of my site's routes from domain.com/post/ to domain.com/new-path/post/ and the "new-path" can change from "new-path" to "another-path" and I want to use that path as a query parameter.
I'm able to to capture the "new-path" as a parameter with:
function site_query_vars( $query_vars ) {
$query_vars[] .= 'param';
return $query_vars;
}
add_filter( 'query_vars', 'site_query_vars' );
function site_rewrites() {
add_rewrite_rule(
'^([^/]+)/?$',
'index.php?param=$matches[1]',
'top'
);
}
add_action( 'init', 'site_rewrites' );
I can access the new path as a query parameter ie... param = new-path, param = another-path .
The biggest issue that I'm having is that I can't find a way to simply add the new dynamic path to all routes (categories, posts, pages, attachments etc...) and have the site function the same.

Need URL without query string in WordPress

I think people have already given solutions for the below questions so I have tried but that is not working, so please do not mark my questions in already asked.
Instead of this url example.com/product-detail?product_id=12345 I need example.com/product-detail/12345
And below I have written script in functions.php
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{
$vars[] = 'product_id'; // var1 is the name of variable you want to add
return $vars;
}
On the next page for getting product_id I have written code:
$product_id = get_query_var( 'product_id' ) ?? "123456";
Everything working properly in localMachine but showing 404 error on Live site.
Please help me for getting this url example.com/product-detail/12345 without 404 error.

In wordpress where is the code that differentiates between categories and standard pages?

This is a question for Wordpress experts:
Where, in the Wordpress machine, is the code that decides whether to route a query to the standard page or to a category? Does it try to match specific strings in the URL looking for categories and then, if it doesn't get a match, default to the code that looks for the page in the wp_posts table?
This is something I have been wondering about for a while but I have found it difficult trying to trace the path of the query through the system.
Thanks for any insights you can give!
There is a function for this you can use to hook in a page or a category.
function is_category( $category = '' ) {
}
function is_page( $page = '' ) {
}
$category / $page can be ID, name, slug or array

WordPress -Mainintaing URL parameters

I have created a custom table namely company Now I want to load respective company's details based on URL
I am thinking of creating URL such as example.com/?company=1 and example.com/?company=1&depart=2
Now after login employee will be redirected to its respected company URL.
Now based on company URL ($_GET['company'] )I will be setting some global vars which will contain company details such as COMPANY_NAME so that I can access on any page.
With this approach I have to maintain the URL parameters on every page load/redirect. And also maintain global vars.
Please tell me if this is a good approach or if there is any other better way. I don't want to use multisite.
I have started with using query vars
add_filter( 'query_vars', 'addnew_query_vars' );
function addnew_query_vars($vars)
{
$vars[] = 'company';
$vars[] = 'depart';
return $vars;
}
add_action( 'template_redirect', 'setVars' );
function setVars()
{
if($_GET['company']){
set_query_var( 'store',$_GET['company'] );
}else{
set_query_var( 'company',2 );
}
if($_GET['depart']){
set_query_var( 'depart',$_GET['depart'] );
}else{
set_query_var( 'depart',2 );
}
}
If parameters are set in the URL then it will get that param values else it will take the default one (i.e 2 )
Edit
I need to create URLs for each company so that I can send to clients,therefore I have to use URL parameters anyway
Well, that's an old approach, now a days, Wordpress and php even all technologies are very flexible, they provide thousands of different ways to do the same thing.
For your approach, best way is to do the things using Wordpress query param, which is a modified way to make an awesome url.
If you don't prefer to share the company ids on the url then try using php session, which is also a good practise and also you have secure data. but with session you will not be able to share the url, so best way is to use the query param.

Wordpress - Build one Page to use URL to pull data from REST API

I'd appreciate any advice, resources or assistance with this issue.
I want to be able to have part of my Wordpress site where I can parse the URL and then use those values to populate the page with content from another API.
For example:
server.zz/weather/Sydney%20Australia
server.zz/weather/Houston%20Texas
Where I could write a Plugin which would intercept these requests, be able to extract the end of the URLs, and then call another API to get the data to then merge into a Template to be presented to the visitor.
I know that there are Custom Post Types, but I wasn't sure if they were the best solution for this usage case.
As I said, any advice or suggestions would be appreciated.
I found the solution to this problem by using add_rewrite_rule(), add_rewrite_endpoint(), and flush_rewrite_rule().
For the example I provided earlier, I created the following code in a Plugin.
// Define the URL Rewrite Rules
function crw_rewrite_urls(){
add_rewrite_rule(
'^weather/(.+)$' ,
'index.php?weather_location=$matches[1]' ,
'top'
);
add_rewrite_endpoint('weather_location', EP_ROOT);
flush_rewrite_rules();
}
add_action('init', 'crw_rewrite_urls');
// Initialise the Query Variable
function crw_query_var( $vars ) {
$vars[] = 'weather_location';
return $vars;
}
// Check for the Variable and Display Content as needed
function crw_handler() {
global $wp_query;
if ( isset( $wp_query->query_vars['weather_location'] ) ) {
// Call the API, fill the Template here
}
return;
}
add_action('template_redirect', 'crw_handler');

Resources