Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get Root URL and Directory of a Plugin Dynamically (https://edwinvelez.net/get-root-url-and-directory-of-a-plugin-dynamically/)
<?php
/**
* Create an array of strings based on this file's absolute file system path as found in this plugin's root
* directory (not web root). Each index of the array is delimited by a forward slash "/".
*/
$this_file_tokenized_path = explode( '/', plugin_basename( __DIR__ ) );
$this_plugin_root_dir_name = $this_file_tokenized_path[ 0 ];
$substr_start_pos = strpos( plugin_dir_path( __DIR__ ), $this_plugin_root_dir_name );
$wp_plugins_root_dir_path = substr_replace( plugin_dir_path( __DIR__ ), '', $substr_start_pos );
$this_plugin_root_url = plugins_url( $this_plugin_root_dir_name );
$this_plugin_root_dir = sprintf( '%s/%s', untrailingslashit( $wp_plugins_root_dir_path ), $this_plugin_root_dir_name );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.