2

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.

3 Answers 3

4

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.

2
  • Are the slugs also all lower case letters? So we should wrap the: strtolower( basename( $plugin_slug, '.php' ) ); Oct 12, 2015 at 19:18
  • You can do that, I'm not very sure if plugin mainfile names can have uppercase letters, never actually tested it. But in short, if it can, then yes, use the extra code. Would be great to fully test this scenario though. Will try to test it tommorow as I'm off to bed now Oct 12, 2015 at 19:28
0

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;
 }
0

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 it's just 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.