Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain wordpress root url (ABSPATH changes on each page)

Tags:

php

wordpress

I have wordpress installed at:

http://example.com/wordpress/

When on homepage, the ABSPATH constant outputs exactly that, but when you navigate off to some other page, like:

http://example.com/wordpress/contact

the ABSPATH also turns to:

http://example.com/wordpress/contact

The question is, how can I obtain the actual root (marked in bold) no matter on which page I am - without hard-coding it?

I'm a bit confused with why ABSPATH changes value, aren't constants unchangeable once they are defined?

Thanks!

like image 779
CodeVirtuoso Avatar asked May 30 '11 11:05

CodeVirtuoso


2 Answers

you can use Site_url();... :)

like image 82
Sujit Agarwal Avatar answered Nov 06 '22 21:11

Sujit Agarwal


I had the same issue on an admin page. Not only do you have to avoid the extra folder that gets inserted, WP may be installed in a folder itself.

Here is a way, albeit somewhat convoluted and written for clarity, that makes the adjustments for these various items. It does avoid DIRECTORY_SEPARATOR issues as well:

if (!defined(PLUGINUPDATEMGR_DOMAIN)) 
    define("PLUGINUPDATEMGR_DOMAIN", strtolower( $_SERVER['HTTP_HOST'] ) );

$wprootbase = strtolower( site_url() );

$wprootstart = strpos( $wprootbase, PLUGINUPDATEMGR_DOMAIN ) + 
    strlen( PLUGINUPDATEMGR_DOMAIN  ); // + 1 to strip the leading slash/backslash

$wprootend = strlen( $wprootbase );

$wproot = substr( $wprootbase, $wprootstart, $wprootend );

echo "Local WP path = '" . $wproot . '"';

Shaken, not stirred, output:

Local WP path = '/wp/wordpress-3.4.2" 

Of course, YMMV =;?)

like image 24
BitTech Avatar answered Nov 06 '22 21:11

BitTech