• Is there any standard way to determine the wordpress directory from php?

    I am writing a plugin that needs to use a URL version of that directory. To require_once wp-config I use the ABSPATH constant:

    require_once(ABSPATH . ‘wp-config.php’);

    but ABSPATH yields a non-URL path.

    I found some php code in another plugin that looks like this:

    $site_root = parse_url(get_settings('siteurl'));
    $site_root = trailingslashit($site_root['path']);

    But those functions require that wp-config already has been run. Any other suggestions?

Viewing 1 replies (of 1 total)
  • Thread Starter timr

    (@trohrer)

    Ok, it appears that ABSPATH is undefined before calling wp-config as I was trying to do in the above message.

    So I can crudely use relative paths as below, and then get wp-config to run, after which the php code to get the site_root URL will work.

    require_once( '../../../wp-config.php');
    $site_root = parse_url(get_settings('siteurl'));
    $site_root = trailingslashit($site_root['path']);

    This still seems clunky to me. Is there a better way?

    (I should note for the sake of not confusing others that my plugin resides in a directory. For a plugin not contained in its own directory there would need to be only two .. relative path-out markers–back out once to wp-content, then back out again to the wordpress root directory.)

Viewing 1 replies (of 1 total)

The topic ‘Determining wordpress installation directory’ is closed to new replies.