简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM