11

I'm wondering how can I find plugins' slug (slug = internal name used by WordPress to do plugin updates and to determine which plugins are currently active)? It is usually plugin's folder name but if a plugin doesn't have a folder, it is its file name (like hello.php). Are there any other exceptions?

  1. Do lowercase and uppercase characters matter?
  2. Can a plugin have different slug than its folder name? What if there's a plugin called hello.php and another /hello.php/hello.php?
  • Very good question, pity that we cannot award bounties to Q's, but I guess the A is the award ;) – brasofilo Oct 25 '13 at 0:00
7

The string used in WordPress to identify the plugin is:

plugin_basename($file);

… where $file is a file with the plugin headers.

So if you are in your plugin, get the slug with:

$slug = plugin_basename( __FILE__ );
  • 1
    plugin_basename($file); Is not the slug since 3.8.1. This is the folder/plugin_main_file.php path. As the "slug" of the plugin Akismet is not "akismet/akismet.php" but it is "akismet". – Jeff Mattson Sep 29 '15 at 13:46
  • 3
    this is no longer the correct answer in either case. – majick Feb 7 '16 at 7:15
  • @majick true, for Wordpress 4.7.4 it is not the answer – Marecky Apr 24 '17 at 23:54
  • 2
    Another way to generate the plugin slug is by using dirname(plugin_basename(__FILE__)). – ilanco Jun 28 '17 at 12:53
2

If you install WP-CLI then you can get the list of plugins with their slug and version from the command line:

> wp plugin list

I know that's probably not what you want, if you need to find the slug in code, but it has helped me while working with the TGM-Plugin-Activation plugin.

I find it hard to work with WordPress without the WP-CLI, in general it's a very useful tool for many common tasks related to WordPress.

1

The difference between the plugin (main) file and the plugin's slug is a place where the WordPress Codex could do much better. I understand your confusion as I have felt it too recently (mixed with frustration).

This is what I have learned by doing some "detective work" on the WordPress core code.

The plugin file

This is the unique way WordPress identifies and records a plugin. It is made up of the plugin's directory AND the main plugin file (the one with the file header containing the various plugin details like version, author, etc.).

It would look something like this: your-plugin-directory/main-file.php

If you look at the active plugins data (returned by get_option( 'active_plugins' ) ), you will see that WordPress only needs this plugin file to properly identify plugins.

You could choose to think of it as your plugin's main file relative path (relative to the wp-content/plugins/ directory that is). You could "compose" the absolute path of the main plugin file with something like this: trailingslashit( WP_PLUGIN_DIR ) . $plugin_main_file

The core itself generates the plugin file like this:

$plugin_main_file = plugin_basename( trim( $plugin_main_file_absolute_path ) );

The plugin slug

One would expect the plugin "slug" to be some sort of standardized ID for the plugin like the post slug is for posts - so you could use this "slug" to provide it to WordPress core functions and get things going.

Not really. After searching the core for references to plugin slugs (or themes for what matters) and finding almost nothing, I think I have a grasp on it.

The only real slugs are those for things accessible via a unique URL: posts, pages, taxonomies, etc. That is the whole point of taking the name of something (like a post title) and generating a URL friendly version of that: to use it in a URL.

But where do we use theme/plugins "slugs" in URLs?

We don't do that on individual WordPress installations - neither in the WP admin or the frontend.

However, there is a place very much entangled with the WordPress code, the WordPress.org site. People are having a hard time distinguishing between the two, including that it got somehow common among developers to consider the WordPress.org theme or plugin slugs should work the same as a post or page slug.

They serve the same purpose but on separate websites. On WordPress.org they are used to uniquely identify a theme from others, and a plugin from the rest (in URLs like https://wordpress.org/plugins/akismet/).

But when it comes to individual WordPress installations, the same uniqueness can't be guaranteed because there is no authority to enforce it (like on WordPress.org). It could work if all the plugins and themes came from WordPress.org, but thankfully that is not the case.

What does the WordPress code do with theme/plugin slugs?

The WordPress core code doesn't rely on theme/plugin slugs to do things like installing, activating, updating, deleting themes or plugins.

For themes, it relies on the theme directory since the main entry point into a theme is the style.css file (you can't use another CSS file to hold your theme details header).

For plugins, it relies on the plugin directory AND the main plugin file, since plugins can call their main file however they like.

The only thing the core uses theme/plugins slugs for is when it handles themes and plugins from the WordPress.org directory: fetching lists of plugins, checking for updates, reporting back to the directory usage data, and so on.


To wrap things up about plugin slugs: whenever you find plugin data with the slug entry, 99% of the time it will refer to the WordPress.org slug of the plugin.

How do we go about identifying plugins?

If you want to programmatically activate, update, deactivate or delete a certain plugin on a WordPress installation, you need to use the plugin file. You can get it like this from your plugin's main file:

$plugin_file = plugin_basename( __FILE__ );

If you want to target a certain plugin from another plugin, things get a bit trickier since you need to rely on a bit of "guesswork".

You could hard-code the plugin name, search the plugin in the list of all plugins (see get_plugins()) and get the plugin file from there.

If you know a class or function that is defined by that plugin you could use reflection (see this answer for classes and this one for functions).


I hope this helps you and others that might have a hard time dealing with "plugin slugs". It could have saved me a couple of hours :)

0

Just to clarify since the original post.

The way I have found the plugin slug is by first navigating to your plugin folder then opening the folder associated with the plugin, and finally finding the file that houses the below code. Once you have found this file, the file name minus the extension should be your plugin slug.

For example if the i found the below code in a file named advanced-plugin-awesomeness.php my slug would be advanced-plugin-awesomeness.

Hope this helps!

/*
Plugin Name: Name of plugin here
Version: 2.4.6
Description: plugin description here
Author: plugin author here
  • -1 as while most filenames and directories match, this is not a reliable method as the update slug is actually the directory - not the filename (unless there is no directory.) – majick Feb 7 '16 at 5:58
  • or not even the directory! see my updated answer, it actually comes from the Plugin Name. – majick Feb 7 '16 at 7:15
0

Unfortunately the slug is returned via the Update API, the answer to this question is not so obvious unless you query the API itself. However, if you want to see a list of your current plugin slugs and related plugin data you can simply do:

print_r(get_site_transient('update_plugins'));

But, this will not have info on a newly installed plugin for another 12 hours, you would have to do something different for those, eg. use a modified version of code from wp_update_plugins in wp-includes/update.php...

Having done this to test it out, it seems to confirm that, regardless of the plugin filename or location or uppercase, it is actually the Plugin Name that generates the update slug, most likely via sanitize_title. So I believe the correct answer should be:

// if you have the plugin basename:
// $pluginfile = WP_PLUGIN_DIR.'/'.$pluginbasename;

// otherwise if you have the absolute path already:
$plugin = get_plugin_data($pluginfile);
$pluginslug = sanitize_title($plugin['Name']);
  • +1 and an additional note is that this might as well change with time as it was made explicit by the wordpress.org team that the slug calculation methodology is not yet totally final – Mark Kaplun Jul 15 '16 at 5:27
  • I'm not sure this is accurate, I was attempting to do it for a plugin but this fails: plugin name as returned is "Responsive WordPress Slider - Soliloquy Lite" and the slug is: soliloquy-lite – Chris Jul 27 '16 at 14:29
  • I suspect this is because the slug is generated at the time the plugin is submitted to the repository and does not change with the name later... in most cases it does match the directory name, but if there are exceptions to both, it could be one or the other. it's not ideal :-/ – majick Jul 28 '16 at 2:04
0

You can get the plugin's folder name (PHP5.3+) by passing DIR to plugin_basename(), like so:

$plugin_foldername = plugin_basename( __DIR__ );
0

Try this:

function get_slugname(){
    $tmp = array();
    $plugins_all = get_plugins() ;
    $plugin_slug = explode('/',dirname(plugin_basename(__FILE__)));
    foreach ($plugins_all as $key=>$value) {
        if ($plugin_slug[0] == explode('/',$key)[0] ) {
        $tmp = $value;
        $tmp['slug'] = explode('/',$key)[0];
        $tmp['file'] = explode('/',$key)[1];
        }
    }
return $tmp;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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