Opened 6 months ago
Last modified 3 weeks ago
#57278 new enhancement
Add filter to allow filtering of the must-use plugins list in the admin
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | 6.3 | Priority: | normal |
Severity: | normal | Version: | 3.0 |
Component: | Plugins | Keywords: | has-patch has-unit-tests needs-testing has-testing-info |
Focuses: | administration | Cc: |
Description
Currently, the Must-Use plugin list in the WordPress admin only shows files that end in .php
, and there is no way to filter this list. This is a problem because we use a single autoloader that allows us to add plugins to mu-plugins
without needing to create a separate PHP file for each one.
As a result, the Must-Use plugin list appears like this:
Admins have no way of knowing which must-use plugins are actually installed.
If there was a filter, we could remove mu-loader.php
from the list and add each plugin individually, like this:
<?php function custom_mu_plugin_filter( $mu_plugins ) { unset( $mu_plugins['mu-loader.php'] ); $directories = glob( WPMU_PLUGIN_DIR . '/*', GLOB_ONLYDIR ); foreach ( $directories as $directory ) { $plugin_file = $directory . '/' . basename( $directory ) . '.php'; $mu_plugins[ $plugin_file ] = get_plugin_data( $plugin_file ); } return $mu_plugins; } add_filter( 'mu_plugins', 'custom_mu_plugin_filter' );
Which would result in:
There are probably other uses for this filter, like hiding a must-use plugin from the list that you don't want clients/users seeing.
Attachments (2)
Change History (21)
This ticket was mentioned in PR #3728 on WordPress/wordpress-develop by nate-allen.
6 months ago
#1
- Keywords has-patch added
#2
@
6 months ago
Test Report
This report confirms that the indicated patch addresses the enhancement.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/3728
Environment
OS: macOS 12.4
PHP: 8.0.0+2
WordPress: 6.1.1
Browsers: Brave v1.46.134, Safari 15.5
Theme: Twenty Twenty-Two
Steps
- Added a single file plugin to the mu-plugins directory (visible)
- Added Akismet Anti-Spam and Jetpack to the mu-plugins directory (not visible)
- Added the above filter to my theme
Results
After adding the example 'custom_mu_plugin_filter' to my theme, instead of just seeing my standalone single file plugin, I am now able to see the two directory based plugins as well
This ticket was mentioned in Slack in #core by nateallen. View the logs.
6 months ago
#5
@
6 months ago
- Keywords needs-unit-tests changes-requested added
- Version set to 3.0
Thanks @nateallen, Left one nit-pick document changes feedback on PR.
Can you please add unit tests for the new filter?
get_mu_plugins()
introduce in version to 3.0
#6
follow-up:
↓ 8
@
6 months ago
Instead of a filter for mu-plugins, I think a general purpose filter for items displayed in the plugin list table could be of more use here.
This would allow developers to show/hide plugins in each category on an as needs basis.
This ticket was mentioned in Slack in #core by costdev. View the logs.
4 months ago
#8
in reply to:
↑ 6
@
4 months ago
Replying to peterwilsoncc:
Instead of a filter for mu-plugins, I think a general purpose filter for items displayed in the plugin list table could be of more use here.
I tend to agree, this seems more flexible in the long run.
#9
@
4 months ago
Also, I'm unsure about the name of the filter in the proposed PR, maybe it's too general. Adding _list` would clarify that it is filtering the list items.
This ticket was mentioned in Slack in #core-test by nateallen. View the logs.
4 months ago
#11
@
4 months ago
- Keywords has-unit-tests added; needs-unit-tests removed
Thanks everyone for the suggestions! I implemented the change as a filter for all of the plugins instead of just must-use plugins.
The filter is added right before the totals get calculated. This ensures accuracy of the plugin count even if the filter is used for adding or removing plugins.
I also added a test for the filter.
#13
@
4 months ago
- Milestone changed from 6.2 to Future Release
As the PR still needs to be tested and we're releasing 6.2 Beta 1 today, I'm going to move this to Future Release.
P.S. There are some failures on the PR, but they don't appear to be related to the patch, I think?
Additional props: @mukesh27
#14
@
2 months ago
- Keywords has-testing-info added
The failures with the PR were caused by null
being passed to strlen
in class-wp-plugins-list-table.php
, which results in a TypeError in PHP 8. I added an isset
check before calling strlen
which fixes that issue.
Here are testing instructions for the new plugins_list
filter:
To reproduce the issue:
- Add a normal plugin to the
mu-plugins
directory (like Jetpack) - Add the
mu-loader.php
file to themu-plugins
directory. See code at the bottom of this comment - In the admin, go to Plugins > Installed Plugins and click on the "Must-Use" link
- Observe that the
mu-loader.php
is listed, but the plugin is not
To test the fix:
- Apply the patch attached to this ticket.
- Ensure a plugin and
mu-loader.php
are inmu-plugins
as before - Use the new
plugins_list
filter to add the plugin to the list. See example code at the bottom of this comment. - In the admin, go to Plugins > Installed Plugins and click on the "Must-Use" link
- Observe that the plugin is now listed and
mu-loader.php
is removed
Code used for testing:
The must-use autoloader example
Save this code to an mu-loader.php
file in your mu-plugins
directory
<?php require_once ABSPATH . 'wp-admin/includes/plugin.php'; foreach ( glob( WPMU_PLUGIN_DIR . '/*/*.php' ) as $file ) { $plugin_data = get_plugin_data( $file, false, false ); if ( empty( $plugin_data['Name'] ) ) { continue; } include_once $file; }
Using the new plugins_list
filter
Place this code in your theme's functions.php
file
<?php function custom_mu_plugin_filter( $mu_plugins ) { unset( $mu_plugins['mustuse']['mu-loader.php'] ); foreach ( glob( WPMU_PLUGIN_DIR . '/*/*.php' ) as $file ) { $plugin_data = get_plugin_data( $file, false, false ); if ( empty( $plugin_data['Name'] ) ) { continue; } $mu_plugins['mustuse'][ basename( $file ) ] = $plugin_data; } return $mu_plugins; } add_filter( 'plugins_list', 'custom_mu_plugin_filter' );
#17
@
7 weeks ago
Test Report
This report validates that the indicated patch addresses the issue.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/3728
Environment
- OS: macOS 13.3.1
- Web Server: Nginx
- PHP: 7.4.33
- WordPress: 6.3-alpha-55505-src
- Browser: Google Chrome 112.0.5615.121
- Theme: Twenty Twenty-Three
- Active Plugins:
- Jetpack 12.0
Actual Results
✅ After following the steps of @nateallen I confirm that the jetpack plugin appears in the list of "Must use" plugins and that the mu-loader.php does not appear in the list.
#18
@
3 weeks ago
Report
This report confirms that the suggested patch fixes the problem.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/3728
Environment
- OS: Windows 11 (22H2)
- Web Server: nginx/1.23.4
- PHP: 7.4.33
- WordPress: 6.3-alpha-55505-src
- Browsers: Chrome Version 113.0.5672.126 (Official Build) (64-bit)
- Theme: Twenty Twenty-Three
Steps:
- Added the file: mu-loader.php with the mentioned code to the mu-plugins directory.
- Added Jetpack to the mu-plugins directory
- Added the above filter code to functions.php of my current theme.
Results
I was able to replicate the feature as mentioned in this ticket.
Adds a new filter called
mu_plugins
that allows the filtering of the must-use plugins array.Trac ticket: https://core.trac.wordpress.org/ticket/57278