Skip to content

Instantly share code, notes, and snippets.

@dlxsnippets
Created September 12, 2022 05:25
Show Gist options
  • Save dlxsnippets/708d7b374cb35e841e323d41f3e08cea to your computer and use it in GitHub Desktop.
Save dlxsnippets/708d7b374cb35e841e323d41f3e08cea to your computer and use it in GitHub Desktop.
Get Plugin Asset URL or Full DIR Path
<?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;
}
@dlxsnippets
Copy link
Author

Assume that __FILE__ is on the root file. Include dirname if in a subdirectory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment