How to get Plugin Slugs?

by Jeff Mattson   Last Updated May 26, 2019 17:08 PM

I am trying to get the 'slug' used for each plugin installed. I have not found a good solution for this. I have created this function to get all the slugs and return them in an array. I am hoping there is a better way. Will this work for all plugins?

 function l7wau_get_slugs(){
        $plugin_array = get_plugins();
        $url_array = array_keys( $plugin_array );
        foreach ( $url_array as $plugin_url ){
            $temp_array = explode( '/', $plugin_url );
            $slugs[] = str_replace( '.php', '', end( $temp_array ) );
        }
        return $slugs;
 }

Thanks for your help in advance.



Answers 3


I am hoping there is a better way

You can write your code a bit better, like adding some protection should something fail in your function. One should never take code for granted that it will always work, even if looks like it. Always code with the mindset of that your code will fail.

I will just simply do a foreach loop and then pass the keys through basename will return everything after the last / if it exists and will also remove the .php extension.

As for get_plugins(), that is definitely a good option to go with as it utilizes the build-in cache system to cache it's results

I would probably rewrite your code as follows; (Requires PHP 5.4+)

function l7wau_get_slugs()
{
    $plugin_array = get_plugins();

    // First check if we have plugins, else return false
    if ( empty( $plugin_array ) )
        return false;

    // Define our variable as an empty array to avoid bugs if $plugin_array is empty
    $slugs = [];

    foreach ( $plugin_array as $plugin_slug=>$values ){
        $slugs[] = basename(
                $plugin_slug, // Get the key which holds the folder/file name
                '.php' // Strip away the .php part
            );
    }
    return $slugs;
}

Will this work for all plugins?

Yes, it will work for plugins in the plugin directory. get_plugins() uses get_plugin_data() which parses the plugin header data to retrieve the meta data. Just look at the sources included in the links.

Pieter Goosen
Pieter Goosen
October 11, 2015 16:58 PM

Note if you are looking to get the plugin slug for use with the plugin update server (eg. for checking and displaying if your plugin has an update on a plugin screen) you should read this answer to a similar question:

https://wordpress.stackexchange.com/a/216940/76440

So in that case this would work:

 function get_plugin_update_slugs(){
    $plugin_array = get_plugins();
    if (empty($plugin_array)) {return array();}
    foreach ( $plugin_array as $pluginfile => $plugindata) {
        $slugs[] = sanitize_title($plugindata['Name']);
    }
    return $slugs;
 }
majick
majick
February 07, 2016 12:57 PM

Here's what I used to solve this:

function apc_get_plugin_slugs() {
$all_plugins =  get_plugins() ;
if ( empty( $all_plugins ) ) { return []; } 

foreach ($all_plugins as $pluginFile => $pluginData ) {
    // Strip filename and slashes so we can get the slug
    if ( false !== strpos( $pluginFile, '/' ) )  {
        $slugs[] = substr( $pluginFile, 0, strpos( $pluginFile, '/'));
    }
    // If its just a php file then lets use the pluginData Name and sanitize it
    else {
        $slugs[] = sanitize_title( $pluginData[ 'Name' ] );
    }
}
return $slugs;

}

Get the plugin folder name so we can query the .org api, if its jut a single php file we get the pluginData name (hello.php becomes hello-dolly). The first part of the function is for plugins that don't use the same structure for their name as they do on .org like Akismet, /akismet/akismet.php versus akismet-anti-spam.

Jay
Jay
May 26, 2019 16:58 PM

Related Questions


How can I find plugins' slug?

Updated May 18, 2017 13:08 PM

How to define the slug for my custom plugin?

Updated September 16, 2019 16:08 PM


how to change a theme slug

Updated March 24, 2017 07:08 AM