We are out of ideas. We also tried a lot.
In Wordpress we need to change the "site_url" for a few pages (because also the scripts, images, etc. need to work without much changes). The remaining pages keep the normal site_url.
Reason: We want to use a Shared SSL that looks like:
https://www.the-shared-ssl.com/www.our-website.com
So we need to change the site_url for these few pages (not all pages).
We tried the Wordpress HTTPS plugin, it doesnt work with our hoster.
We also tried to put an "if" statement into the wp-config.php, it also doesn't work.
It would be good if there's a solution without htaccess
Is there a code guru that can help us?
site_url() calls the function get_site_url() which looks up the option in your wp_options table called "site_url." Changing the config file won't have any impact on it.
What you can do is apply a filter to site_url on the pages where you need to use something else. The last part of the get_site_url() function reads:
return apply_filters('site_url', $url, $path, $scheme, $blog_id);
So if you want to modify the value that is returned add something like this to your theme's functions.php file:
add_action('template_redirect', 'modify_site_url');
function modify_site_url(){
/** check if you're on one of the pages that requires the change */
if(is_page(array('page-one', 'page-two'))){
add_filter('site_url', 'new_site_url', 10, 4);
}
}
function new_site_url($url, $path, $scheme, $blog_id){
/** modify the url here */
$url = whatever you want it to be;
return $url;
}
In the new_site_url function it's not using the additional parameters ($path, $scheme, $blog_id) but you could use those to further refine how the url is modified.
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.
Hello Everyone and thank you for your help!
I have an external (outside wordpress in root directory) php file that I would like to use the custom_rewrite_rule function to make the url pretty as well as pass on information for variables that will look up an API record from that URL. You might ask why I haven't done it the regular way and made it a page template inside wordpress, and that is because I am using a custom JQuery slideshow that seems to have trouble running inside a wordpress environment.
So what I have in my child themes functions.php is:
function custom_rewrite_rule() {
add_rewrite_rule('^yacht-details/([^/]*)/([^/]*)/?','yacht-details.php?info=$matches[1]&boatid=$matches[2]','top');}
add_action('init', 'custom_rewrite_rule', 10, 0);
and in the php file am using the standard $_GET to acquire the information.
$info = $_GET["info"];
$boatid = $_GET["boatid"];
My actual problem is what I am actually getting is literally $matches[1] and $matches[2] instead of the variables.
What am I doing wrong? Does this need to be done with .htaccess instead of functions.php? The problem I run in to going that route is wordpress does not seem to like more than one subfolder.
Thank you again.
I am developing my own theme for private use and i have a lot of scripts working with urls with extra parameters.
I'm finding my self adding a lot of empty pages to my site just to be able to connect some php file to specific url so i will be able to send parameters to that specific address.
Is there a better way i can have urls that can be associated with specific php files without adding pages or other things to the site. just like work out of the box when installing my theme?
You should try like this code:
//page url: http://www.example/myurl
add_action( 'init', 'my_rewrite' );
function my_rewrite() {
global $wp_rewrite;
//wp-content/plugins/myplugin/myurl.php
add_rewrite_rule('myurl/$', WP_PLUGIN_URL . '/myplugin/myurl.php', 'top');
$wp_rewrite->flush_rules(true);
}
More info:
https://developer.wordpress.org/reference/classes/wp_rewrite/
https://codex.wordpress.org/Class_Reference/WP_Rewrite
I've been looking for hours trying to find a straightforward answer to this but I just can't seem to find anything out there which tackles this issue. There are numerous examples for rewriting standard URLs, but nothing in-terms of rewriting pages from within a custom plugin.
I've created a custom plugin with a special directory in it called test. The test directory has several pages which are blah.php and shmeh.php. Is it possible to create rewrite rules to these pages i.e. http://example.com/blah, http://example.com/shmeh
Thank you very much for all the help.
So essentially what you are looking for, is to have Wordpress load a specific page template (blah.php) when the user visits http://example.com/blah/. Also, you need it to to be completely generated in your plug-in without actually creating any pages or posts. You are on the right track. What you want to do here is create a rewrite rule that utilizes a specific query var to load the page. Here it is step by step:
Step 1: Rewrite rules
http://codex.wordpress.org/Function_Reference/add_action
http://codex.wordpress.org/Plugin_API/Action_Reference/init
http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
/* Rewrite Rules */
add_action('init', 'yourpluginname_rewrite_rules');
function yourpluginname_rewrite_rules() {
add_rewrite_rule( 'blah/?$', 'index.php?blah=true', 'top' );
}
So in the above code, your just telling Wordpress that example.com/blah/ should be treated as if the user were visiting example.com/index.php?blah=true. Now "blah" is a query var that you will set up in the next function below. Query vars are just what Wordpress calls $_GET variables. However they must be registered with Wordpress so it can properly store the value (which is "true" in this case). We will do that below in the next function. It's also worth noting the third setting in the function "top", which gives this rewrite rule priority over other rewrite rules. Alternatively, setting it to "bottom" would check that no other rewrite rules match first before using this one.
Step 2: Query Vars
http://codex.wordpress.org/Function_Reference/add_filter
http://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
/* Query Vars */
add_filter( 'query_vars', 'yourpluginname_register_query_var' );
function yourpluginname_register_query_var( $vars ) {
$vars[] = 'blah';
return $vars;
}
In the code above, your just registering the query var "blah" with Wordpress. That way, when someone visits example.com/blah/, which your rewrite rule tells Wordpress to treat it like they are visiting example.com/index.php?blah=true, Wordpress actually bothers to save it's value (which is set to true). This is so you can use it in the next function, which loads the template you want when users visit your url.
Step 3: Template Include
http://codex.wordpress.org/Function_Reference/add_filter
http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include
http://codex.wordpress.org/Class_Reference/WP_Query#Interacting_with_WP_Query
http://codex.wordpress.org/Function_Reference/plugin_dir_path
/* Template Include */
add_filter('template_include', 'yourpluginname_blah_template_include', 1, 1);
function yourpluginname_blah_template_include($template)
{
global $wp_query; //Load $wp_query object
$page_value = $wp_query->query_vars['blah']; //Check for query var "blah"
if ($page_value && $page_value == "true") { //Verify "blah" exists and value is "true".
return plugin_dir_path(__FILE__).'test/blah.php'; //Load your template or file
}
return $template; //Load normal template when $page_value != "true" as a fallback
}
In the code above, you are simply telling Wordpress to use your blah.php template any time the query var "blah" is present and set to true. In this case it is present because a user is visiting a url with a rewrite rule that utilizes the "blah" query var. The function checks if the "blah" query var is present, then checks if "blah" is set to true, and if so loads the template. Also note that the template path is test/blah.php. You are using plugin_dir_path(__FILE__) to relatively include the template based on the location of your plugin. That way it's relative as a plug-in should be.
Step 4: The Actual Template (blah.php)
http://codex.wordpress.org/Function_Reference/get_header
http://codex.wordpress.org/Function_Reference/get_footer
http://codex.wordpress.org/Function_Reference/get_sidebar
Your template file will need to load the header, footer, and sidebar itself. When using template_include, Wordpress will not automatically load the header and footer as if you were using a page template. Be sure your template file calls get_header(), get_footer(), and get_sidebar() so that you get your complete layout.
Notes
Hope this helps, I was searching for something similar the other day and had to figure it out the hard way. It's also worth noting that I have prefixed all of the functions as "yourpluginname_". You can change this to whatever you want your prefix to be, but be sure to update the function names to match as well.