I have a url like this
xxxxx.com/?s=&cp_state=Porto&refine_search=yes
and I try to make a url like this
xxxxx.com/Porto
Already tried to use this code:
function search_url_rewrite_rule() {
if ( is_search() && !empty($_GET['s'])) {
wp_redirect(home_url("/search/") . urlencode(get_query_var('s')));
exit();
}
}
add_action('template_redirect', 'search_url_rewrite_rule');
But this code gives me a url like this
xxxxx.com/search/
Could you help me please?
I can't swear this will work but you're looking for something like this:
add_rewrite_rule('([^/]*)/?','index.php?s=&cpstate=$matches[1]&refine_search=yes','top');
I doubt the wisdom of rewriting based on the first segment in the URL, in case you want to switch over to pretty links for the rest of the site later on you will get a lot of odd results. For example example.com/blog would search for blog, example.com/search for search for search and so on... if you want something else you can just edit the regexp so that it fits what you want.
Read more here: http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
Related
On the WordPress site using Avada, My situation is very much similar to
https://stackoverflow.com/a/40250428/12323081
They have given the code to be added into functions.php, As per my understanding the code should go in the child theme of Avada, but I don't understand where this part of the code will go?
(Which template file should this code be added to?)
Once this is done, you can query for these variables in your template:
if ( get_query_var('firstName') ) {
echo get_query_var('firstName');
}
if ( get_query_var('lastName') ) {
echo get_query_var('lastName');
}
Once added, how can these parameters be called on Front End?
Any help will be highly appreciated.
After doing quite some R&D, there seems to be a very simple solution to get parameters from the URL in WP/Avada.
In order to achieve the result in my question, please use the Title element and then make use of the dynamic field 'Request Parameter' -> Param Type - Get -> Query Var - firstName
The image is attached for reference.
tinyurl.com/yy3vtwxq
I don't know the exact meaning of the question but someone asked me this question in interview. I just want to know that there's something like that, we use any route algorithm in Wordpress?
This could be a trick question because routing would mean mapping an HTTP request to trigger specific function or method that would handle the request which is not something that WordPress does (there is a section about WordPress at the bottom). In simple word, you read the HTTP request information to decide what function is going to be triggered.
Bit more details in simple words
if you are building a PHP project from scratch and want to display specific content or trigger a method/function there are usually two option (without routing)
Using POST , GET or REQUEST variables and complex conditional statements to achieve what you want, so a result URL could be something like this
http://example.com/index.php?view=pubications&per_page=5
Setting a PHP file for each type of content
http://example.com/publications.php?per_page=5
However, if you created a Router (Routing algorithm as you named it or routing system) then pushed all requests to index.php and have the latter include let's say something like this:
// Include the Router class
require('classes/router.php');
// Include functions responsible for display our content
require('view/display.php');
I'll not go into how to build a router, just giving examples assuming that you already have one just to give you an idea how routing works.
So assuming you have a router and function to display a contact form for example, you'd also include something like this:
Router::add('/contact-us', get_contact_form(),'get');
Router::add('/contact-us', handle_contact_form(),'post');
Then initialize the Router
Router::initialize('/');
Again assuming you have a complete Router, the above function would tell the index.php file to handle HTTP requests on this URL differently:
http://example.com/contact-us
If it's the default request type GET, trigger this function get_contact_form(), but if the request type is POST trigger this one handle_contact_form() which will act and display content differently depending on your needs.
That's great because it would be instead of something like
http://example.com/index.php?page=contact-us
index.php content would handle the request differently since there is no router.
// Include functions responsible for display our content
require('view/display.php');
if( isset($_GET['page']) && $_GET['page'] == 'contact-us'){
echo get_contact_form();
}
if( isset($_GET['page']) && $_GET['page'] == 'contact-us' && isset($_POST['contact_submit']) ){
echo handle_contact_form();
}
Imagine how long and ugly this would look like if you have a lot of pages and a complex site.
So back to WordPress
If you have a new installation you'd notice that the URLs looks something like this:
http://example.com/?p=62
http://example.com/?cat=1
http://example.com/?author=3
So it would just take URL parameters then build a WP_Query based on that, if is p then look for posts in database by ID, if cat then look for categories by ID and so on... (that's the simple explanation, there is a lot going on of course in the back-end, but just to give an idea).
You might notice after changing permalink structure that the above examples would now look something like this:
http://example.com/post-slug
http://example.com/author/name
http://example.com/category/uncategorized
This might look like routing, but it isn't, let's go in a bit more details about how this works.
When requesting a (pretty-link) URL on WordPress, first thing that happens is that the .htaccess looks for a folder/file with same name on the server, if it exists it will served, if not, it would send that request to the index.php file which does one thing:
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
loading the wp-blog-header.php file, which will make a small check to make sure the code only run once then the following:
// Load the WordPress library.
require_once( dirname(__FILE__) . '/wp-load.php' );
// Set up the WordPress query.
wp();
// Load the theme template.
require_once( ABSPATH . WPINC . '/template-loader.php' );
Let's not go deeper into these files, what's concerns us the most is what 'wp-load.php' and 'template-loader.php' does
wp-load.php
This one among other things, looks for wp-config, make sure everything is set correctly, then connect to the database, of course after a lot of initialization, setting up constants loading a lot files that handles different parts of WordPress structure. Part of this process is that WordPress tries to match the request URL with a large set of rule called rewrite rules which are set of regular expressions, when a match is found WordPress will translate that URL into a database query using [WP_Query][1] class which is located at wp-includes/class-wp-query.php and this class will save the query results among other things (query type...etc)
template-loader.php
This one handles the display part, it uses some WordPress function that make use of WP_Query (eg:is_home()) to find out what type of content is to be displayed, then loads the the correct template based on that, and finally the template will use WP_Query to show the result.
I am having a big problem where I cannot use the Linkedin Share button.
One of the reasons is the non friendly URLs I am using.
Basically I have built a plugin for wordpress which shows me jobs.
It is working perfect show the jobs and everything but the link looks like this
www.recruitmentagency.com/job/?id=250
and I want it to look like
www.recruitmentagency.com/job/250
or
www.recruitmentagency.com/job/job-id/250
I tried to add rewrite rules to htaccess with no luck
RewriteRule ^job/([0-9]+)/$ job/?id=$1
RewriteRule ^job-id/([0-9]+)/$ /?id=$1
and none of them worked.
Any solution will be greatly appreciated.
I tried to use the inbuilt system and it is partially working.
However I don't like the way it is doing it so.
Instead of getting the job like
$job_id=$_GET['id'];
I am getting it like
$job_id=wp_query->query_vars['page'];
when the page is load like
www.recruitmentagency.com/job/250
I can't even understand why my supposed job-id is appearing as a page, but I am using it since I need this to work.
I was also facing the same issue. after long time I have reached the below solutions:
add_filter('rewrite_rules_array','job_rewrite_rules_array');
function job_rewrite_rules_array($rules){
$job_page = get_post(123);
$job_rules = array();
if( is_object($job_page) ){
$job_slug = $job_page->post_name;
$job_rules[$job_slug.'/([^/]*)$'] = 'index.php?pagename='.$job_slug.'&jobid=$matches[1]';
}
return $job_rules + $rules;
}
//Bind Query Var
add_filter('query_vars','job_query_vars');
function job_query_vars($vars){
array_push($vars, 'jobid');
return $vars;
}
Execute job_rewrite_flush just one time
function job_rewrite_flush(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init','job_rewrite_flush');
Now access the param as: $jobid= get_query_var( 'jobid', 1 )
I think the above script work as per your requirement.
I am setting a WP that it should redirect all the request made to sub-directory to file inside that directory.
For example if there a visit on example.com/link/me then it should send the user to /link/index.php?me
To achieve this, I am trying to add rewrite rule in theme function file.
Here's what my code looks like:
function moin_add_rewrite_rules() {
add_rewrite_rule(
'^([^/]*)/(link)/(?:[a-z][a-z0-9_]*)?$',
'/link/index.php?$matches[1]',
'top'
);
}
add_action( 'init', 'moin_add_rewrite_rules' );
It is not working, visiting example.com/link/meshows a 404 (as link is outside WP).
Is there any problem with regex code? or anywhere else? What could be other possible solution?
Looking at the example in the Codex, the matched expression doesn't have the leading /.
Stripping the leading bit out of your regex, then, I think this should work (sorry, I don't have a test environment handy):
add_rewrite_rule('^link/([a-z][a-z0-9_]*)/?$','/link/index.php?$matches[1]','top');
Alternatively, you could do something similar in your .htaccess file.
As a side note, I think as it's currently coded, you should be using $matches[3] not $matches[1], as the value you're interested in is the third parameter in parentheses (the Codex notes "capture group data starts at 1, not 0").
OK it might sound stange but I am facing a good challenge with wordpress.
when echoing the_permalink(); and checking the portfolio pages, I am getting a link something like this:
http://www.domain.com/?portfolio=test
(Where test is the portfolio name).
Is there any option to return the permalink trimmed to ?portfolio=test keeping out the domain url?
While asking, I think I got the answer already (trim()) but I would like to hear your ideas too.
every answer will be much appreciated!
You can obtain the permalink of a post by doing something like so:
<?php
function my_permalink() {
echo substr(get_permalink(), strlen(get_option('home')));
}
?>
The get_option method replaces the deprecated get_settings method, and allows you to retrieve named option values from the database:
http://codex.wordpress.org/Function_Reference/get_option
The 'home' value passed in to the get_option method will return the site URL.