Capslock10 Capslock10 - 3 months ago 10
PHP Question

How to get the homepage url?

I am making a redirect page on Wordpress. The

PHP
will return back to the website homepage. But I can't get back the home page url.

My php file path
xampp\htdocs\wordpress\return.php
.

And here is my code:

$url = "$_SERVER[HTTP_HOST]";
header('Refresh: 3;url=' . $url);
echo get_option('home');
echo $url;


The
$url
is
localhost8080/wordpess/return.php
.
I want to go
url : local:8080/wordpress
from
url : localhost8080/wordpess/return.php
.

How can I get back the url
local:8080/wordpress
?

Thx

Answer

Wordpress has a built-in function for that: wp_redirect(see doc)

require_once( dirname(__FILE__) . '/wp-load.php' ); // first you need to load WordPress libraries if you are in an external file.

wp_redirect( home_url() );

exit; // don't forget to exit after a redirection
Comments