Created
September 12, 2022 05:25
-
-
Save dlxsnippets/708d7b374cb35e841e323d41f3e08cea to your computer and use it in GitHub Desktop.
Get Plugin Asset URL or Full DIR Path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get the plugin directory for a path. | |
* | |
* @param string $path The path to the file. | |
* | |
* @return string The new path. | |
*/ | |
public static function get_plugin_dir( $path = '' ) { | |
$dir = rtrim( plugin_dir_path( __FILE__ ), '/' ); | |
if ( ! empty( $path ) && is_string( $path ) ) { | |
$dir .= '/' . ltrim( $path, '/' ); | |
} | |
return $dir; | |
} | |
/** | |
* Return a plugin URL path. | |
* | |
* @param string $path Path to the file. | |
* | |
* @return string URL to to the file. | |
*/ | |
public static function get_plugin_url( $path = '' ) { | |
$dir = rtrim( plugin_dir_url( __FILE__ ), '/' ); | |
if ( ! empty( $path ) && is_string( $path ) ) { | |
$dir .= '/' . ltrim( $path, '/' ); | |
} | |
return $dir; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assume that
__FILE__
is on the root file. Includedirname
if in a subdirectory.