admin管理员组

文章数量:1425858

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.

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.

Share Improve this question asked Oct 11, 2015 at 14:16 Jeff MattsonJeff Mattson 3072 silver badges10 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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

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 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 like Akismet, /akismet/akismet.php versus akismet-anti-spam.

本文标签: How to get Plugin Slugs