Wordpress will only fully load on localhost

后端 未结 5 727
栀梦
栀梦 2021-02-06 15:29

I have recently set up wordpress on my wamp server and when loading up the webpage through localhost it will always fully load up the entire page but when i try to access the we

5条回答
  •  盖世英雄少女心
    2021-02-06 16:00

    The main issue is because WordPress uses the server addresses from the database. WordPress uses the root urls from the database options named home and siteurl, so if you try access WordPress outside that computer it may get the incorrect path for CSS and JavaScript.

    If you want to get the correct path without doing a redirect, you can define a dynamic root url in wp-config.php

    add this script below the define('ABSPATH', dirname(__FILE__) . '/');

    /**
     * get home url from absolute path
     * @return string url to main site
     * hello@lafif.me
     */
    function get_dynamic_home_url(){
        $base_dir  = ABSPATH; // Absolute path
        $doc_root  = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME']);
        $base_url  = preg_replace("!^${doc_root}!", '', $base_dir);
        $protocol  = empty($_SERVER['HTTPS']) ? 'http' : 'https';
        $port      = $_SERVER['SERVER_PORT'];
        $disp_port = ($protocol == 'http' && $port == 80 || $protocol == 'https' && $port == 443) ? '' : ":$port";
        $domain    = $_SERVER['HTTP_HOST'];
        $home_url  = "${protocol}://${domain}${disp_port}${base_url}";
    
        return $home_url;
    }
    $url = get_dynamic_home_url();
    define('WP_SITEURL', $url);
    define('WP_HOME', $url);
    

    I think that would work with http://localhost/, http://localhost/wp/, http://127.0.0.1/, or http://yoursite.dev/

提交回复
热议问题