skip to Main Content

I am new in wordpress, Right now i am working with "my server", and my current domain/main url is like "myurl.com/myproject"
And later i will move project(wordpress) to another server and url will be like "myurl.com", so i want to know that right now how can
i manage this ? in other words which code should be use for include "css,js,images etc.." ?
I tried with following code not showing "http"

echo $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);

2

Answers


  1. You can get your domain URL via home_url('/') home_url and get your current directory via PHP function and constant basename(___FILE___) FILE_ basename or use WordPress function to create your desire dircetory path get_template_directory().'/your_dir' get_template_directory or URL get_template_directory_uri().'/your_dir' get_template_directory_uri

    Login or Signup to reply.
  2. Here you go:

    function get_url(){
        // Start memory cache
        static $parse_url;
        // Return cache
        if($parse_url) {
            return $parse_url;
        }
        // Check is SSL
        $is_ssl = (
            ( is_admin() && defined('FORCE_SSL_ADMIN') && FORCE_SSL_ADMIN ===true )
            || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
            || (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
            || (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on')
            || (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)
            || (isset($_SERVER['HTTP_X_FORWARDED_PORT']) && $_SERVER['HTTP_X_FORWARDED_PORT'] == 443)
            || (isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https')
        );
        // Get protocol HTTP or HTTPS
        $http = 'http'.( $is_ssl ? 's' : '');
        // Get domain
        $domain = preg_replace('%:/{3,}%i','://',rtrim($http,'/').'://'.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''));
        $domain = rtrim($domain,'/');
        // Combine all and get full URL
        $url = preg_replace('%:/{3,}%i','://',$domain.'/'.(isset($_SERVER['REQUEST_URI']) && !empty( $_SERVER['REQUEST_URI'] ) ? ltrim($_SERVER['REQUEST_URI'], '/'): ''));
        //Save to cache
        $parse_url = array(
            'method'    =>  $http,
            'home_fold' =>  str_replace($domain,'',home_url()),
            'url'       =>  $url,
            'domain'    =>  $domain,
        );
        // Return
        return $parse_url;
    }
    

    I build this function to work in one my plugin that cover all WordPress and PHP versions.

    Tested on few thousand installations.

    Feel free to use it!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search