Monday, January 30, 2017

WordPress important functions

home_url()                      //>     http://example.com
get_stylesheet_directory_uri()  //>     http://example.com/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
get_stylesheet_directory()      //>     /home/user/public_html/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__)        //>     http://example.com/wp-content/plugins/MY-PLUGIN/  [while used inside plugin.. same as:  plugins_url('',__FILE__) ]
plugin_dir_path(__FILE__)       //>     /home/user/public_html/wp-content/plugins/MY-PLUGIN/   [while used inside plugin]    



//===============MY EXAMPLES - USAGE============//
(i.e. wordpress is installed in subdirectory:  http://example.com/wpdir/)

define('domainURL',                 (((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' ).$_SERVER['HTTP_HOST']);
    // ----->  http://example.com
define('homeURL',                   home_url());
    // ----->  http://example.com/wpdir/
define('homeFOLD',                  str_replace(domainURL,'',   homeURL));
    // ----->                    /wpdir/
define('requestURI',                $_SERVER["REQUEST_URI"]);
    // ----->                    /wpdir/any-page?with=parameters
define('requestURIfromHome',        str_replace(homeFOLD, '',requestURI) );
    // ----->                          /any-page?with=parameters
define('requestURIWithoutParametr',parse_url(requestURIfromHome, PHP_URL_PATH));
    // ----->                    /wpdir/any-page
define('currentURL',                domainURL.requestURI);
    // -----> http://example.com/wpdir/any-page?with=parameters
define('THEME_URL',                 str_replace(domainURL, '', get_template_directory_uri()) );plugin_dir_url(__FILE__)) ); 
    // -----> http://example.com/wpdir/wp-content/themes/THE-THEME-NAME/
define('PLUGIN_URL',                str_replace(domainURL, '', plugin_dir_url(__FILE__)) ); 
    // -----> http://example.com/wpdir/wp-content/plugins/THE-PLUGIN-NAME/

No comments:

Post a Comment