如何获得主页网址?

如何获得主页网址?

问题描述:

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\wordpresseturn.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

我正在Wordpress上制作一个重定向页面。 PHP code>将返回到网站主页。 但我无法取回主页网址。 p>

我的php文件路径 xampp \ htdocs \ wordpress eturn.php code>。 p>

这是我的代码: p>

  $ url =“$ _SERVER [HTTP_HOST]”; 
 header('Refresh:3; url ='。$  url); 
 echo get_option('home'); 
 echo $ url; 
  code>  pre> 
 
 

$ url code>是 localhost8080 / wordpess / return.php code>。 我想从 url:localhost8080 / wordpess / return.php code>转到 url:local:8080 / wordpress code>。

如何找回网址 local:8080 / wordpress code>? p>

Thx p> div>

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

From what I understand, you're trying to redirect your page from localhost:8080/wordpess/return.php to localhost:8080/wordpess/ using -

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

What you need to do is change your $url variable to the location where you want to redirect, which is -

$url = "http://localhost:8080/wordpess/";
header('Refresh: 3; url =' . $url);

Hope that's what you were looking for.

EDIT -

If you don't want to hard code the URL, you can try the following -

$url = "/wordpess";
header("Refresh: 3; url = http://" . $_SERVER['HTTP_HOST'] . $url);

From my understanding of your question, you want to go back one level from the current page. This is it?

If so, you can accomplish that by doing some string manipulation as follows:

<?php
    // Given that your current url is in the '$url' var
    $url = 'localhost8080/wordpess/return.php';

    // Find the position of the last forward slash
    $pos = strrpos($url, '/');

    // Get a substring of $url starting at position 0 to $pos 
    // (if you want to include the slash, add 1 to the position)
    $new_url = substr($url, 0, $pos + 1);

    // Then you can have the redirection code using the $new_url variable
    ...

Please let me know if I misunderstood.
Hope it helps. Cheers.