Question

Is there a function that would return the equivalent of plugin_dir_path but it being agnostic of a plugin / theme? JS scripts need to be enqueued as resources, as such, you cannot include their on-server path which is, say /var/www/html/wordpress/thing/thing2/script.js, it needs to be the http://www.example.com/thing/thing2/script.js counterpart.

Was it helpful?

Solution 2

Although @butlerblog's answer works, I find it unecessarily complex. I've checked above and beyond, site_url will always give you the current site's link, it will resolve the schemas for you, whether or not it's http or https, etc and as such, there's no issues with it.

I've written a simpler, easier to understand function:

/**
 * Retrieves the full front-facing URL for a given path. In other words, it transforms an absolute path
 * into an URI.
 *
 * Note: when allowing direct access to your files, if there is any I/O (there shouldn't be, but, you know) operations,
 * you must check whether or not ABSPATH is defined.
 *
 * @see https://stackoverflow.com/a/44857254/12297763
 *
 * @param string $from An absolute path. You can just call this function with the parameter __FILE__ and it will give you a front-facing URI for that file.
 * @param boolean $strict Flag that the function uses to see if it needs to do additional checks.
 *
 * @return string|false Returns a string in the form of an URI if all checks were passed or False if checks failed.
 */
function getURIFromPath( $from, $strict = False )
{
    if( $strict ) {
        if( !\file_exists( $from ) ) {
            return False;
        }
    }

    $abspath = untrailingslashit( ABSPATH ) ;

    $directory = dirname( $from );

    return str_replace( "//", "\\", site_url() . str_replace( $abspath, '',  $directory ) );
}

The reasoning for naming it URI... is that there's no case where you're going to build a link to include a PHP file. This is to be used in the case where you're distributing your code as a package and cannot rely on your framework's / main plugin CONSTANTS. In other words, when you don't know the install path of your package, use this. It'll work similarly to CSS' ../ (always relative).

OTHER TIPS

This is kind of a hacky way to do this; but unfortunately, there is not a WP function that will do both (theme and/or plugin). It's only an either/or proposition.

On the surface, you'd think it wouldn't be difficult. You could just get the path and compare it with the site URL or something like that. But you run into problems when WP is installed somewhere other than the root (such as in a directory).

If you look at my setup in the function, the "else" condition is the simple. If WP was always in the root, that would be all you need to do. Everything else is done to handle the other possibility (that WP is in a directory - or lower).

In that case, it explodes the site URL to determine if there is more than just the root domain (an array bigger than 3). If so, it loops through the parts of the URL we got from the explode() process. We can skip the first three elements of the array as those should be the root of the domain (https://example.com). Then it builds the path (in case it's more than just one directory below).

Using that it strips out everything below the root URL so you get just a clean URL you can use. Then it appends the path to the file.

function my_get_file_url_path() {

    // Account for WP being installed in a directory.
    $path_info = '';
    $site_url  = site_url();
    $url_parts = explode( '/', $site_url );

    if ( array_count_values( $url_parts ) > 3 ) {
        $x = 0;
        foreach ( $url_parts as $this_part ) {
            if ( $x > 2 ) {
                $path_info = $this_part . '/';
            }
            $x++;
        }

        $site_actual_url = str_replace( $path_info, '', trailingslashit( $site_url ) );

        return $site_actual_url . trim( str_replace( $_SERVER['DOCUMENT_ROOT'], '', __DIR__ ), '/' );

    } else {
        return str_replace( $_SERVER['DOCUMENT_ROOT'], $site_url, __DIR__ );
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top