Wordpress add_rewrite_rule is not working - wordpress

I have used this code in my themes function.php file, to pass custom argument to a page in wordpress but it is not working. I am getting 404 page not found. What am I doing wrong?
function photo_details_vars()
{
add_rewrite_rule('^photo-details/([^/]*)/?','index.php?p=20&id=$matches[1]','top');
}
add_action('init', 'photo_details_vars');
I am new to wordpress. I found this idea from here - http://codex.wordpress.org/Rewrite_API/add_rewrite_rule please be nice and direct me to a solve.
Additionally how can I catch this url variables value from the template file of page no. 20
How should I use add_rewrite_tag() for this rewrite rule?

Have you registered the query variable using add_rewrite_tag()? otherwise WordPress will not recognize the variable.
Additionally how can I catch this url variables value from the template file of page no. 20??
if I understand it correctly, are you asking about this: $_GET['p']?

Related

Wordpress add action 'init' not working frontend

I have installed Wordpress without making any changes and using the standard 2019 theme. I want to create virtual pages when ever specific url patterns are called. To do this I need to run a URL rewrite as early as possible, so I have added this line to the top of the themes functions.php file:
function url_rewrite() {
die('here');
}
add_action('init', 'url_rewrite');
I get the message 'here' as expected on pages, but if its a URL which does not exist the site is not picking this up at all, it just goes to a 404 page. I'm hoping someone knows why.
I have also tried:
add_action('init', 'url_rewrite', 1, 0);
its working for me perfect, can you please check as you have added code with theme selected and once update your permalink in backend

Creating personalized URLs (PURLs) in Wordpress

I need to create a site that will allow for a unique user-identifying number to go in be appended to the URL.
So for instance
test.com/12345/about/
will load the page with slug 'about' (not redirect to it).
I have tried the following:
function custom_rewrite_basic() {
add_rewrite_rule('^([0-9]+)/', '/$matches[1]', 'bottom');
}
add_action('init', 'custom_rewrite_basic');
But it does not work, any prefix to the URL redirects the page with or without the block of code above.
So in my tests
test.com/23423423/about will re-direct to test.com/about which is not what I want.
Is what I am trying to achieve possible? If so, could anyone offer advice as to how I would best go about implementing it?

wordpress auto generate pages from sub url of page

Hoping someone can help. I don't want to create actual pages in the backend of wordpress but i want to know how i can make it so if anyone goes to a url: mydomain.com/page/sub-url.
Then i can grab that "sub-url" and output a page with content i generate via php.
If i grab that "sub-url" and i don't want to output i can do a 404 error.
I want to try to stay away from editing the htaccess file is possible but if i need to i can.
Everytime i try search for this, i get result for creating actual pages in the backend automatically which i don't want.
I think what you're looking for is a custom rewrite endpoint, which you call 'page'. Then on yourdomain.com/page/sub-url, 'page' is the parameter and 'sub-url' is the value. You can get that value with the function get_query_var().
After applying, be sure to go to your Wordpress admin->Settings->Permalinks, and click save to flush the current rewrite rules.
Update:
Use this code to show a 404 page
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 );
exit();
(source)

Custom Plugin for wordpress with hierarchy of SEF pages

Here's my issue. My company needs a vendor database added to our wordpress website. None of the existing plugins will even come close to what we need, and we already have a mysql database with all of our information, so we need to create a plugin or something to do what we need.
These urls need to be direct-accessible and have SEF urls. So, for example:
mysite.com/vendors/
mysite.com/vendors/pipe-manufacturers/
mysite.com/vendor/bobs-pipes/
And, the custom content needs to appear inside the wordpress template.
There are really 2 options:
1) Find a way to write our application outside of wordpress, but find a way to bootstrap wordpress to show the header, footer, and sidebar.
2) Run the app from inside wordpress.
So I went for option #2. I created a new template file named "vendor.php", and began working. I added this code to my functions.php of my theme:
add_filter( 'template_include', 'xyz_template_check' );
function xyz_template_check() {
global $template;
$rqst = $_SERVER['REQUEST_URI'];
$ra = split("/", $rqst);
if ($ra[1] == "vendors") {
$template_file = get_stylesheet_directory() . '/vendors.php';
return $template_file;
}
return $template;
}
So what the above code does, if it sees the word "vendors" as the first part of the url after the site name, it sends you to vendor.php. This works PERFECTLY....
except...
Wordpress believes that the page is not found. It returns a 404 header, and NOT FOUND into the page title and breadcrumb.
Adding a PAGE called "Vendor Database" with the permalink "/vendors/" fixes the main page. But there will be literally hundreds of vendors and different categories. I cant be creating a custom page for each one. This needs to be dynamic.
So, how do I make wordpress give a 200, and supply an acceptable page title, breadcrumb, etc.
Don't even get me started on the danged wp_title filter. This did NOT work as documented. Although, it just occurred to me that this might be an issue with Wordpress SEO (the wp_title filter issue).
Anyone got an idea on this?
Ok got this. The solution was to use the rewrite api, as mentioned above, to look for the pattern /vendors/, letting it know that it was a valid URL. Coupled with my existing template override, this is what I needed.

Wordpress custorm URL variables causes strange redirect

I'm trying to have custom variables in my URL for Wordpress site. I have read up as much as I could find on the subject and so far have the following in my functions page:
function add_query_vars($aVars) {
$aVars[] = "mrdrct";
return $aVars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');
And the following on my header page:
if(isset($wp_query->query_vars['mrdrct'])) {
$mVar = $wp_query->query_vars['mrdrct'];
echo "variable is $mVar <br />";
}
Just to test out if things are being passed correctly and they are. However, when I use a link with the url variable in it - say www.mydomain.com/?mrdrct=myVarable - I am not directed to my homepage of my Wordpress site which is set to a static page with a template on it - I am instead directed to a page with my latest posts on it. I cannot figure out why this is happening - any ideas? Hopefully I've explained this well enough.
Thanks.
When WP sees a query string (? after the URL) it will attempt to display matching posts using it's rewrite rules. If no posts match it will show a 404 error - I would guess you do not have a 404.php file, so WP is showing the default which is index.php (see the Wordpress Template Hierarchy for more details on that).
I'm not 100% sure what you want to achieve, but I'd suggest that you need to look at changing the query when $wp_query->query_vars['mrdrct'] is set. See the WP Codex for query_posts() for a good place to start, if you are not already familier with it.

Resources