#57278 closed enhancement (fixed)
Add filter to allow filtering of the must-use plugins list in the admin
Reported by: | nateallen | Owned by: | audrasjb |
---|---|---|---|
Milestone: | 6.3 | Priority: | normal |
Severity: | normal | Version: | 3.0 |
Component: | Plugins | Keywords: | has-patch has-unit-tests has-testing-info commit add-to-field-guide has-dev-note |
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 (33)
This ticket was mentioned in PR #3728 on WordPress/wordpress-develop by nate-allen.
2 years ago
#1
- Keywords has-patch added
#2
@
2 years 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.
2 years ago
#5
@
2 years 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
@
23 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.
22 months ago
#8
in reply to:
↑ 6
@
22 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
@
22 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.
22 months ago
#11
@
21 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
@
21 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
@
20 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
@
19 months 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
@
18 months 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.
This ticket was mentioned in Slack in #core-test by zunaid321. View the logs.
18 months ago
#20
@
17 months ago
@audrasjb and @costdev can you please look at this PR, if it is ready, it can go into the trunk for not to wait another release. Thanks 🙏
#21
@
17 months ago
- Keywords changes-requested needs-refresh added; needs-testing removed
Thanks for the ping @oglekler!
- I've left a review on the PR with minor changes requested. Adding
changes-requested
. - There are merge conflicts with
trunk
. Addingneeds-refresh
.
Test results for the PR look good, so once the feedback and merge conflicts are addressed, this seems ready for commit
consideration, and should be able to land in 6.3.
During the next scrub, if you see that Nate hasn't had a chance to update the PR and you're about to punt this ticket, ping me and I'll submit an updated PR so this can be committed ahead of 6.3 Beta 1.
@audrasjb commented on PR #3728:
17 months ago
#22
I committed fb4a416 to solve a conflict against trunk.
This ticket was mentioned in Slack in #core by mukeshpanchal27. View the logs.
17 months ago
This ticket was mentioned in PR #4717 on WordPress/wordpress-develop by @costdev.
17 months ago
#24
- Keywords needs-refresh removed
#25
@
17 months ago
- Keywords needs-dev-note commit added; changes-requested removed
- Owner set to audrasjb
- Status changed from new to accepted
I tested this filter using the following code:
function wporg_filter_plugins_list( $plugins ) { unset( $plugins['all']['hello-dolly/hello.php'] ); return $plugins; } add_filter( 'plugins_list', 'wporg_filter_plugins_list' );
The plugin is removed from the list. Works fine.
Self assigning for commit
.
@audrasjb commented on PR #4717:
17 months ago
#27
committed in https://core.trac.wordpress.org/changeset/56068
#29
@
16 months ago
Added to misc dev note.
draft: https://make.wordpress.org/core/?p=106236&preview=1&_ppp=2977223417
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