I have wordpress custom template page test.php in which I want to pass variable throuh url like,
http://localhost/wordpress/test?date=2017&postid=11&title=some-title
in this format,
http://localhost/wordpress/test/2017/11/some-title
and grab the parameter from the url.
I have followed this link, but couldn't produce the result as I have desired.
Is there another way to achieve the desired result as I have mentioned??? I am pretty newbie to wordpress theme development.
Well, as suggested by #vl4d1m1r4 , I have used the web server(in my case apache) rewrite rule to rewrite the path. Some-what I have achieved the desired result.
In function.php
I have written the following code to update the rewrite rule in wordpress(.htaccess)
add_filter( 'query_vars', 'add_custom_query_vars' );
function add_custom_query_vars( $vars )
{
array_push($vars, "test_year", "test_postid", "test_title");
return $vars;
}
function custom_rewrite_rule()
{
add_rewrite_rule('^test/([^/]*)/([^/]*)/([^/]*)?','index.php?pagename=test&test_year=$matches[1]&test_postid=$matches[2]&test_title=$matches[3]','top');
}
add_action('init', 'custom_rewrite_rule');
After adding the above code, I have re-saved the permalink settings(setting>>permalink)
To Get the parameter in custom-template-page i.e test.php. I have used the following query.
echo get_query_var('test_title');
echo get_query_var('test_postid');
echo get_query_var('test_year');
Related
I need to add some of my own php files to an existing Wordpress site (from Bitnami) so it can be accessed from a 3rd-party service. I don't need to access anything from Wordpress in my code, but I need my code to be accessible from a url for 3rd-party services.
Like this:
https://myWordPressSite.com. <- normal WP site
https://myWordPressSite.com/myCustomDirectory/generateSerial.php <- my code
https://myWordPressSite.com/myCustomDirectory/doSomething1.php <- my code
https://myWordPressSite.com/myCustomDirectory/doSomething2.php <- my code
How can I add my directory of code to the Wordpress file structure, and how to access it from a URL?
You can add a $_GET OR $_POST to wp in the plug side
if you want I would add a code to help with not getting hack
Your url https://myWordPressSite.com?name=yes
function getdata(){
if(($_GET['name'] == "yes") || ($_POST['name'] == "Run")){
}
}
This is one way
Good Luck
Karl K
Create a folder in your theme's root folder called myCustomDirectory and place your php file inside that.
then add to your functions.php the following:
This will handle the Rewrite Rules:
add_action('init', 'custom_add_rewrite_rule');
function custom_add_rewrite_rule() {
add_rewrite_rule('^myCustomDirectory/generateSerial.php', 'index.php?generateSerial=true', 'top');
}
This will handle the Wordpress Query Vars:
function add_query_vars_filter( $vars ){
$vars[] = 'generateSerial';
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
This will tell wordpress to pull the correct file:
function load_custom_template($template){
if(get_query_var('generateSerial') ){
$template = get_template_directory() ."/myCustomDirectory/generateSerial.php";
}
return $template;
}
add_filter('template_include', 'load_custom_template');
make sure to flush your rewrite rules when you are done by going to: Settings > Permalinks and clicking 'save changes'
It turns out it was much simpler than I thought, or any of the other answers presented here.
The answer is to just add a directory of my own code inside the Wordpress home folder.
In the case of a AWS/Lightsail/Bitnami/Wordpress package, the home directory is:
/opt/bitnami/wordpress
So I created a directory and put my code in it..
/opt/bitnami/wordpress/myDirectory
/opt/bitnami/wordpress/myDirectory/generateSerial.php
/opt/bitnami/wordpress/myDirectory/doSomething.php
And it was then accessible from a url like:
https://myWordPressSite
https://myWordPressSite/generateSerial.php //etc
One small catch is that some Wordpress packages and some other Bitnami packages are laid out a bit different, so check on your specific installation.
I build a Wordpress website with a page called 'Nieuwsberichten'.
The url looks like
https://www.example.com/nieuwsberichten
This page is dynamic and I need an extra parameter called 'news_page'
https://www.example.com/nieuwsberichten/?news_page=recent
The URL above is not friendly so I create a rewrite rule for this and I add the variable 'news_page'.
functions.php
function add_query_vars($vars){
$vars[] = 'news_page';
return $vars;
}
add_filter( 'query_vars', 'add_query_vars', 10, 1 );
function rewrite_paged(){
add_rewrite_rule('^nieuwsberichten/([0-9]+)/?', 'nieuwsberichten?news_page=$matches[1]', 'top');
}
add_action('init', 'rewrite_paged');
After this I flushed the permalinks
menu -> settings -> permalinks -> save
If I browse to
https://www.example.com/nieuwsberichten/recent/
It will redirect to
https://www.example.com/nieuwsberichten/
What do I miss in my code?
Hope somebody can help me with this
In the regex part, you specify an entry for numbers only, but you pass text like recent in your url. Maybe the error is here. Please check if it works with number input, edit your expression if that's the problem. Also, the url structure you specified does not seem to be quite correct.
Anyway, I'm not sure of my answer so I'd appreciate it if you could confirm after checking.
https://developer.wordpress.org/reference/functions/add_rewrite_tag/
This may be the function you should use for the parameters you add. This function will also not require the query_var function. So I think you won't need to use the other function.
I'm not in a position to check or test right now. If your problem is not solved, I will look into it in detail.
You can use it like this:
function rewrite_paged(){
add_rewrite_tag('%news_page%', '([^/]+)');
add_rewrite_rule('^nieuwsberichten/?([^/]*)$', 'nieuwsberichten?news_page=$matches[1]', 'top');
}
add_action('init', 'rewrite_paged');
Also if you are pointing to a page you will need to use page id or pagename. A blog or a product etc. If it is, there will still be parameters you need to use and you should redirect this url to index.php. I'm leaving an example for you to understand what I'm saying, and for your review, you can take a look at the rewrites on your site as follows:
Review examples:
global $wp_rewrite;
print_r($wp_rewrite);
Examples:
Match Pagename:
add_rewrite_rule('^nieuwsberichten/?([^/]*)$', 'index.php?pagename=nieuwsberichten&news_page=$matches[1]', 'top');
Match Page ID:
add_rewrite_rule('^nieuwsberichten/?([^/]*)$', 'index.php?page_id=10000&news_page=$matches[1]', 'top');
If you have an installation outside of wordpress your example is correct but if it will be inside wordpress all requests should be directed to index.php. Therefore, you may need to edit the code I have given like these examples.
I'm want to redirect a custom URL same like example in docs here
EX: http://domain.com/find/324 to http://domain.com/?text=324
This is the code , for some reason it keeps redirect me to homepage without the parameter "text" in the URL..
function custom_rewrite_basic() {
add_rewrite_rule('find/(.+)/?', 'index.php?text=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');
The "text" parameter is not a build in WoprPress function, i'm going to use it with custom code of mine.
I flushed that links cache, still redirects to homepage without the parameters.
What i'm missing?
You cannot add custom query vars directly. Custom query vars have to be added using the query_vars filter to be parsed within rules. You can add custom query vars as seen below.
function wpd_add_query_vars( $qvars ) {
$qvars[] = 'text'; //I used the one in your example
return $qvars;
}
add_filter( 'query_vars', 'wpd_add_query_vars' );
Then you get get the query vars like this:
get_query_var('text')
check https://wordpress.stackexchange.com/questions/271931/add-rewrite-rule-parameter-is-not-received-by-the-page?rq=1 for more detail.
I am trying to create custom rewrite rules in WordPress so that I can pass data to a page based on what is in the query string.
My page works fine:
/retailers-a-z/?merchant=some_merchant
and then also
/retailers-a-z/?merchant=some-merchant&offer=some-offer
I first tried to create a rewrite rule for this in .htaccess but since realised WordPress has it's own internal redirection database.. so after a lot of research I managed to come up with the following code... However, it is still not working.. Whenever I try to access
/retailers-a-z/some-retailer or /retailers-a-z/some-retailer/some-offer it just loads the home page.
functions.php:
function wp_raz_rules()
{
add_rewrite_rule(
'retailers-a-z/([^/]+)/?$',
'index.php?merchant=$matches[1]',
'top'
);
add_rewrite_rule(
'retailers-a-z/([^/]+)/([^/]+)/?$',
'index.php?merchant=$matches[1]&offer=$matches[2]',
'top'
);
add_rewrite_tag('%merchant%', '([^/]+)');
add_rewrite_tag('%offer%', '([^/]+)');
}
add_action('init', 'wp_raz_rules');
function wp_raz_vars($vars)
{
$vars[] = 'merchant';
$vars[] = 'offer';
return $vars;
}
add_filter('query_vars', 'wp_raz_vars');
I then believe I can access them with get_query_var('merchant') and get_query_var('offer') instead of $_GET[]
Any ideas? Thanks:)
Managed to fix it by explicitly defining the page name in the query string.
index.php?pagename=retailers-a-z&merchant=$matches[1]&offer=$matches[2]
Not 100% sure if it is the correct way of doing it.
I'm trying to add a rewrite rule to pass a var to my page template.
it only excepts numbers in the variables, when I type in characters other then number it directs to 404.
for example:
this works: domain.com/reco/9080
But this doesn't: domain.com/reco/abcd (redirects to 404)
This is my code:
function add_rewrite_rules($wp_rewrite) {
add_rewrite_rule('reco/([^/]*)/?', 'index.php/reco/?b=$1', 'top');
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
function query_vars($public_query_vars) {
$public_query_vars[] = "b";
return $public_query_vars;
}
add_filter('query_vars', 'query_vars');
Cheers!
I ended up using get_query_var('b') which works! apparently wordpress "hogs" the query vars because the website is actually: index.php?pagename=yourPermalink.
Also don't try to use variable "s" because wordpress uses this on every page for search purposes.