I am appending "?sel" to my URL in order to keep track of which link was selected, and then show items based on that selection in the next page. I used the following in functions.php to register it as a Query var (rather than just use $_GET):
function add_query_vars_filter( $vars ){
$vars[] = "sel";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
Then I created the link with the var using add_query_arg() in the template where the links are.
$link = add_query_arg( 'sel', $slug, get_the_permalink($news_specials_listing_page_id) );
<a href="<?php echo $link ?>">
However, if I click that link, I still see the "?sel" in the URL. I've got my permalinks setting to post name in settings > permalinks, so I don't see the typical WordPress variables. Is it posible to hide "sel" along with the rest of the registered query variables, and still grab the value using get_query_var()?
I have also tried adding a custom rewrite tag and flushing my permalinks.
function custom_rewrite_tag() {
add_rewrite_tag('%sel%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
I have been experimenting with add_rewrite_rule() and have found that it adds RewriteRules to .htaccess. So if I add a rewrite rule that captures the sel variable in a regular expression and redirect it, would the variable still get saved? I'm also having a difficult time figuring out what the rule would be.
The whole point of using GET is to put variables into your URL. This forms a unique URL that can be bookmarked and indexed by search engines. If you do not want to do this use POST instead. POST allows you to encrypt variable names making it far more secure than the solution you are asking about.
Related
I'm developing a WP plugin and have a WordPress URL:
(e.g.: http://localhost/testsite1/coder/?id=66),
and have attempted to add a rewrite rule to
http://localhost/testsite1/coder/66/
using the following rule:
function add_mypage_rule(){
add_rewrite_rule(
'^coder/([0-9]+)',
'index.php?id=$matches',
'top'
);
}
add_action('init', 'add_mypage_rule');
I have registered a WP Query Var using:
add_filter('query_vars', 'registering_custom_query_var');
function registering_custom_query_var($query_vars){
$query_vars[] = 'id';
return $query_vars;
}
but when at URL http://localhost/testsite1/coder/66/, when I run code
echo get_query_var('id');
nothing is displayed
however when at URL http://localhost/testsite1/coder/?id=66 the echo statement will display 66.
What is wrong with my rewrite rule resulting in echo get_query_var('id'); not accessing the parameter and displaying 66?
When you use add_rewrite_rule function, the first argument is the regular expression. Right? When you wrap your regular expression/pattern in parentheses, you're grouping the expression, which means you could access the captured group in the parentheses like this: id=$matches[1].
Regular expression ^coder/([0-9]+)
Access it in the first captured group (since the id is in the first parenthesis), id=$matches[1]
add_action('init', 'add_mypage_rule');
function add_mypage_rule()
{
add_rewrite_rule(
'^coder/([0-9]+)',
'index.php?id=$matches[1]',
'top'
);
}
After that, refresh the permalinks and rewrite rules by navigating to:
Settings > Permalinks > Click on the 'Save changes' button at the bottom of the page!
And now it should, theoretically, work, unless there are more details that you have not mentioned in your question!
I'm searching for a solution to add .php extension to the page of my wordpress plugin.
I already found similar posts but none of them was about adding the .php extension to only on page generated by a plugin.
I already tried to work with the global $wp_rewrite; but it would apply the .php extension to all pages.
All I want is simply something like that:
www.mydomain.com/myfile => www.mydomain.com/myfile.php but only for this one particular page.
Update:
The file myfile.php doesn't exist. It's the permalink of a wp page. I basically want to add .php to the permalink of one page (only this one page). I know it's possible to change the permalink of an page, but wp would not let me add .php to the permalink. It automatically changes it to -php (it doesn't accept the dot).
It is not necessary to change the link format for all pages via wp_rewrite. You can add rewrite_rule for a specific page without changing the format for the entire Post Type.
add_rewrite_rule(
'^myfile.php',
'index.php?page_id=1234',
'top'
);
1234 = your page $post_id
Next, we change permalink generation for our page
add_filter( 'page_link', 'filter_function_myfile_link', 10, 3 );
function filter_function_myfile_link( $link, $post_id, $sample ){
if ( $post_id == 1234 ) {
$link = home_url('/myfile.php', 'https');
}
return $link;
}
I have a photo gallery page that is using a single page in Wordpress.
The gallery display is dynamic, and relies on a URL parameter. The parameter itself is an integer of the relating wordpress page ID. (as that's the page it's about).
Here is an example of a gallery URL:
http://www.bellavou.co.uk/gallery/?pid=238
For SEO purposes, I'm trying to get away from every possible Gallery URL combination to have the page title 'Gallery', as is set in the Wordpress page settings.
I've been trying this, which has been mentioned a few times:
function assignPageTitle(){
return "Title goes here";
}
add_filter('wp_title', 'assignPageTitle');
But as I'm also using Yoast SEO plugin, I think this is over-writing it (even though I uncheck 'Force title rewrite', and keep the page title field blank). Not sure why the above function doesn't seem to work.
Within the gallery page, I'm able to show the h1 title properly, passing PHP Variables into the code, but I want to do the same to the page title, but in a way that can be done directly on the page template.
Why is it that whenever I post a question, I seem to find the answer very soon afterwards?
I read a post saying that it helps to put a wp_title function before the wp_header call, which is what I tried, and that seems to work fine.
I can edit the page title just for an individual page using the PHP page template, and using this:
$procedure = isset($_GET['pid']) ? $_GET['pid'] : '';
add_filter( 'wp_title', 'wp_title_gallery', 100 );
function wp_title_gallery($title) {
global $procedure;
if(!$procedure) {
$title = the_title();
} else {
$title = the_title(get_the_title($procedure).' ');
}
return $title;
}
get_header();
$procedure gets the URL parameter, and then the function takes that ID, and gets the associated page from it, and then prints the title of the page, into the page title hook. Lastly, the WP header is called.
(Just incase anyone comes here looking for a solution)
This question is based on an unanswered question in Wordpress Development which has not gotten a solid answer.
I have a wordpress website which lists hotels. the url for a single hotel looks like:
/hotels/the-marriot-hotel
I also have a custom taxonomy for Locations, which allows me to browse the hotels in various locations, which works fine, the urls are like:
/Locations/Liverpool
For URL's like /hotels/* I would like to use a custom template, which I have done already and works fine.
The Problem
I also want to be able to drilldown the Locations taxonomy creating a breadcrumb type URL and also use a different template for the hotel page.
For Example, if a user is browsing /Locations/Liverpool and clicks the Marriot Hotel I would like it to click through to /Locations/Liverpool/the-marriot-hotel instead of /hotels/the-marriot-hotel and also use a slightly different template, which can also load a different sidebar and recommend other hotels in the area specific to the location slug in the URL
So basically I want two routes to a single post and a different template used based on the route used.
How would I go about implementing this?
What have I tried?
I've tried adding a new page and using a rewrite rule to point to it to be the locations hotel page.
I've tried adding a slug on the end of the /Locations/{location-slug} url and reading this in the page template and loading the hotel post instead of the list it doesn't seem to be working but also feels like a terrible hack anyway
An idea that I've had is to add a rewrite to the hotels/{slug} page and using code to detect the URL used and switch templates dynamically but I'm not sure this is the best approach
I have managed to get this working using the second method mentioned above (adding a rewrite to the locations landing page and checking for a query_var).
I will post the code below that I used but although this works and seems to be working very well, It does not feel like the best way of doing it. If someone know of a better way of doing this please post the answer.
I used this online post for reference.
Note: The listing page shows the list of hotels in the taxonomy in a side column down the side and shows the currently selected or a random one in the main content area. Which will explain how I am using the loop below.
function prefix_locations_rewrite_rule() {
add_rewrite_rule( 'Locations/([^/]+)/([^/]+)', 'index.php?locations=$matches[1]&hotel=$matches[2]', 'top' );
}
function prefix_register_query_var( $vars ) {
$vars[] = 'hotel';
return $vars;
}
function prefix_url_rewrite_templates() {
if ( get_query_var( 'hotel' ) && is_singular( 'hotel' ) ) {
add_filter( 'template_include', function() {
return get_template_directory() . '/taxonomy-locations.php';
});
}
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
add_filter( 'query_vars', 'prefix_register_query_var' );
add_action( 'init', 'prefix_locations_rewrite_rule' );
In my template file for the hotels landing page:
$hotelSlug = get_query_var( 'hotel', false);
if ( have_posts() ) {
while (have_posts()) : the_post();
if ($post->post_name == $hotelSlug) {
break;
}
endwhile;
}
This bit of code will iterate over the posts and if the hotel slug matches the query var it will break there so that the current post is the one we wanted.
We could just use a query here but as I already have a list of posts within the taxonomy I thought I'd just iterate over it. Below this I check to see if a specific hotel has been selected otherwise I show a random one from the list.
I am still to add additional logic and error handling to this code, I hope it helps someone with a similar issue
I'm trying to allow my page to receive query variable but to rewrite it to nice permalink. So I wanted this
example.com/wordpress-page/random
So I don't want random to be a subpage or something like that, since it's coming from outside service. In order to do this, I've added this code to my functions.php
function add_my_var($public_query_vars) {
$public_query_vars[] = 'my_var';
return $public_query_vars;
}
add_filter('query_vars', 'add_my_var');
function do_rewrite() {
add_rewrite_rule('wordpress-page/([^/]+)/?$', 'index.php?name=wordpress-page&my_var=$matches[1]','top');
}
add_action('init', 'do_rewrite');
When I go to example.com/wordpress-page I get my page, but when I go to example.com/wordpress-page/random I get 404 page.
I have flushed rewrite rules by saving permalinks in wp-admin panel.
Where did I go wrong?
I found I needed to call the flush_rewrite_rules() function. That was done in addition to the query_vars action hook and add_rewrite_rule() function you already demonstrated above.
In my case I chose to put it in my theme's functions.php using the switch_theme action hook and deactivated/reactivated my theme. But you can probably put it elsewhere as long as it get's called once at some point (so probably not 'init')
add_action('switch_theme', 'mytheme_setup_options');
function mytheme_setup_options () {
flush_rewrite_rules();
}