I'm using a localhost site as the basis for creating other sites from. I'm using php to generate internal links by finding the current domain, and displaying it with a shortcode. I intend to upload my site to a live host and therefore the domain will change, and my internal links won't be broken.
//add shortcode that displays current site name
function GR_site_name(){
$currentDomain = $_SERVER['HTTP_HOST'];
return $currentDomain.'/wordpress';
}
add_shortcode('GR_site_name', 'GR_site_name');
?>
I've tested this by adding the code to live sites and it works perfectly.
I've read the post at how safe is $_SERVER["HTTP_HOST"]? however it details it as 'generally unsafe' due to the script that it runs.
Is $_SERVER['SERVER_NAME'] safer than $_SERVER['HTTP_HOST']? Or are both regarded as bad practice?
Make a config file with define('host','https://example.com') and use host wherever you want and when you upload script to server just change url in config file and you're good to go
Use a combination of the standard WP_HOME & WP_SITEURL constants, along with the site_url() function.
I'd recommend using one local copy of wp-config.php and a different one for the live site, and then add the following to them:
define('WP_SITEURL', 'http://www.example.com/wordpress'); // Or 'http://localhost/wordpress' for the local copy
define('WP_HOME', 'http://www.example.com');
That way, you don't need a custom function or shortcode - you can simply call site_url() wherever you need it to get the URL of your WP site.
Alternatively, if you need to keep the site URL variable and to accept anything that points to it:
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/wordpress');
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] . '/wordpress');
If you need to keep the shortcode for use within the context of a post, you can change it to:
function GR_site_name(){
return site_url();
}
Note that setting WP_HOME and WP_SITEURL override any database setting for the site's URL, and if you visit the Settings->General page, you'll see those two fields are greyed out.
Related
I have some trouble with setting up a correct Cookie path. What do i want to achieve?
I have a project that runs on my localhost server, with a path http://localhost/project_folder/Controller/function
I can set the Cookie path using the mentioned relative path /project_folder/Controller/function and it works
But when the project runs on another server, where the structure is a bit different, i.e.: http://localhost/project part is replaced by http://www.example.com/Controller, then the trouble begins
I am using CodeIgniter framework.
To achieve the metioned i have tried using the base_url() function CodeIgniter provides and if i change the site path in the configuration it works on the second server but does not work on the local one and vice-versa ...
How should i properly handle the Cookie path in this situation?
EDITED:
It's a simple Cookie set (i'm using a Custom cookie function in JavaScript and i'm setting the Cookie via JavaScript):
createCookie('filter_products', params, '1', '<?php echo base_url('Controller/function'); ?>');
The problem is, that the base_url() function returns the path below like i said before:
http://localhost/project_folder/Controller/function
But on the server the localhost/project_folder/ part is replaced by the domain name, so how can i create the Cookie path dynamically?
The first thing that comes to my mind is, that you should avoid setting Cookies via JavaScript if it is possible and use the CodeIgniter built-in helper or the native PHP functions.
To your question:
For dynamic decision on which server you are, you can use the PHP super-global $_SERVER value which is accessible from anywhere in your code. The SERVER_NAME field contains the actual name of the host, so you can use something like:
<?php
if (isset($_SERVER, $_SERVER['SERVER_NAME'])) {
if ($_SERVER['SERVER_NAME'] === 'localhost') {
// set base path to localhost/project_folder
} else {
// set base path to www.example.com <- MORE SECURE
// or, set base path to $_SERVER['SERVER_NAME'] <- INSECURE (any server could be written to the config)
}
}
Just wondering why by reading the source code in default-constants.php:
function wp_plugin_directory_constants() {
if ( !defined('WP_CONTENT_URL') )
define( 'WP_CONTENT_URL', get_option('siteurl') . '/wp-content'); // full url - WP_CONTENT_DIR is defined further up
I have an alias of my site and my homeaddress is different to my site_urladdress causing CORS problems.
What is the best method to make it point to home?
The value that's used by default, the WordPress address (siteurl), covers both of the most common WordPress configurations.
WordPress URL and Site URL are the same
WordPress is running in a different folder
If you were to use the Site URL (home) instead, anybody wishing to run WordPress in a different folder would have to either manually set the content directory constant or move their wp-content directory.
As you aren't running a standard configuration of WordPress, you'll simply need to override the constant yourself.
get_option() isn't going to work inside wp-config.php so you'll need to manually enter the URL or use another method of detecting it. You're probably better entering it manually in my view however it's going to depend on context.
define( 'WP_CONTENT_URL', 'https://example.com/wp-content' );
Is there a way I can change the header instead of adding on?
For example
Creating a user
create_user.php (a form to action.php) -> action.php (switch case which will call another function) -> user_controller.php
So from user_controller.php, upon successful creation, i used the header method to bring the user to another location, view_user.php.
So now the url header is like
http://localhost/dct/action.php/web/view_user.php?&success=[SUCCESS]%20User%20is%20created!
but i want it to be
http://localhost/dct/web/view_user.php?&success=[SUCCESS]%20User%20is%20created!
without the action.php. How do i go about achieving that?
You will need to use URL rewriting - this depends on your setup. You can either do it at the web-server level, or application-level.
Two ways :
save your "base_url" website (http://localhost/dct/) in parameter and call header('Location: ' . $baseUrl . '/veiw_user.php?...)
Simply add a / before your URL (example: header('Location: /view_user.php?...'), and it will redirect from the root of your website, but in your case, it will redirect to http://localhost/view_user.php?....
The first way is better for your case, but you have to update the parameter when you publish your website in production environment.
Okay, so I have a really odd problem... I have this line at the top of my page, because I want to get the latest wordpress blog post (which is fine, the post comes back);
<?php
require('./blog/wp-load.php');
?>
But then whenever I visit a page on my website that has https:// in the url it seems to stop my css file from working would anyone be able to help? thanks
in wordpress its always best to use the right api method:
http://codex.wordpress.org/Function_Reference/site_url
<?php
$url = site_url();
require($url. /blog/wp-load.php');
?>
The site_url template tag retrieves the site url for the current site (where the WordPress core files reside) with the appropriate protocol, 'https' if is_ssl() and 'http' otherwise. If scheme is 'http' or 'https', is_ssl() is overridden. Use this to get the "wordpress address" as defined in general settings. Use home_url() to get the "site address" as defined in general settings.
I have looked all over the place, but still cant figure out how to set a global variable in PHP (Lithium framework).
My goal is to make the server root always accessible everywhere, without having to write this code everytime, to make my app independent of the hostname it is running on :
<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
Please don't tell me to just use absolute or relative paths, as I have tried everything that way, and it is simply too messy (besides not working most of the time).
It looks like your code above is in a template. So you can use $this->request()->to('url') to get the current url. Or from a controller, it would be $this->request->to('url'). You can also pass 'absolute' => true to the options for Router::match. Unfortunately, the html link helper - i.e. $this->html->link(...) - doesn't pass the 'absolute' => true option through to the Router::match but you can call $this->url() which is passed through to Router::match. So it would be $this->url('/relative/url', array('absolute' => true)).
See the docs for Router::match
The Request object also provides access to things like http host and request uri. See the docs for Request::env()
But, to your original point about global vars, I think the Environment class should be used for this. You can put arbitrary data in it -- and even set it in your bootstrap to have different values for different environments (i.e. dev vs. staging vs. production).
Sorry, but use relative paths and use the link generator helper, which won't get what you're trying to do wrong.
See the docs.