WordPress is known for its default URLs that represent various content entities such as posts, pages, and pre-designed templates like archive pages. However, WordPress goes a step further by providing developers with a powerful built-in API that enables the creation and management of custom routes.
A route serves as a mapping mechanism that enables the server to handle different requests and respond accordingly, allowing for dynamic and organized navigation within the application.
Let’s see an example:
http://www.example.com/company/1
http://www.example.com/company/234
The example route “http://www.example.com/company/1” is designed to handle requests related to individual companies based on their unique ID. By accessing this route, the server can retrieve and process data or perform operations tailored to the company with the ID “1”.
add_action('init', function(){
add_rewrite_rule( 'company/([a-z]+)[/]?$', 'index.php?company=$matches[1]', 'top' );
});
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'company';
return $query_vars;
} );
In summary, this code snippet sets up a custom route that captures URLs with the pattern “company/[company_name]”, passes the company name as a query variable, and directs the request to the index.php file for further processing.
The add_rewrite_rule() function in WordPress is used to add a custom rewrite rule to the site’s URL structure. It allows you to define specific URL patterns and map them to specific queries or content within your WordPress application.
add_rewrite_rule( string $regex, string|array $query, string $after = 'bottom' )
The add_rewrite_rule() function takes three main parameters:
For more detailed information and documentation on the add_rewrite_rule() function in WordPress, you can visit the official WordPress Developer Reference page at this link.
The code add_filter( ‘query_vars’, function( $query_vars ) { … }) adds a filter to modify the list of query variables recognized by WordPress.
To connect a route to a new template in WordPress, one effective approach is to use the ‘template_include’ action hook. By hooking into this action, you can dynamically determine the template that should be included based on specific conditions. Here’s an example of how you can achieve this:
add_action( 'template_include', function( $template ) {
// Check if the 'company' query variable is not set or empty
if ( get_query_var( 'company' ) == false || get_query_var( 'company' ) == '' ) {
return $template; // Return the default template
}
// If the 'company' query variable is set, return the custom template 'company.php'
return get_template_directory() . '/company.php';
} );
In this example, the code checks if the ‘company’ query variable exists and has a value. If it is not set or empty, the function returns the default template, $template, without making any changes. However, if the ‘company’ query variable is set, the function returns the path to a custom template called ‘company.php’ located in the current theme’s directory get_template_directory().
If you have suggestions for improving the code, please send an email. It should be noted that the code's functionality is provided without any guarantee or responsibility.
WordPress nonces provide essential security measures against CSRF attacks.
To optimize the speed and efficiency of WordPress websites, developers often turn to WordPress Transients.
This website uses cookies to ensure you get the best experience on our website.