I have successfully hidden my plugin from the plugins page using $wp_list_table
however the pagination at the top still lists plugins as 'All (3)' etc.
I managed to successfully alter the $wp_list_table
array of _pagination_args = total_items
.
But it was still rendering Plugins - 'All (3)' at the top of the page.
Any ideas on how I can fix this?
I have found WP_Plugins_List_Table::prepare_items()
has a global $totals
variable but I am unsure of how I would change this,
Inside this function it has
$totals = array();
foreach ( $plugins as $type => $list )
$totals[ $type ] = count( $list );
All I am looking for is to get the total plugins number and - 1 before it renders on the page.
I have successfully hidden my plugin from the plugins page using $wp_list_table
however the pagination at the top still lists plugins as 'All (3)' etc.
I managed to successfully alter the $wp_list_table
array of _pagination_args = total_items
.
But it was still rendering Plugins - 'All (3)' at the top of the page.
Any ideas on how I can fix this?
I have found WP_Plugins_List_Table::prepare_items()
has a global $totals
variable but I am unsure of how I would change this,
Inside this function it has
$totals = array();
foreach ( $plugins as $type => $list )
$totals[ $type ] = count( $list );
All I am looking for is to get the total plugins number and - 1 before it renders on the page.
Share Improve this question edited Feb 27, 2018 at 9:33 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Feb 27, 2018 at 8:41 IcarusIcarus 1 3- If you want a plugin that doesn't appear in the main plugins list and can't be turned off, you should create a 'must-use' plugin. – Jacob Peattie Commented Feb 27, 2018 at 9:07
- developer.wordpress/reference/classes/wp_plugins_list_table This is the class I'm talking about. – Icarus Commented Feb 27, 2018 at 9:08
- Thanks for your comment Jacob. I wasn't aware of MU plugins. Although I am building a plugin that does require a handful of users to be able to see the plugin and disable it in a normal manner. I have already built this functionality and got it working to hide the plugin from the plugin table for specific users, it is just the number of plugins counted at top eg 'All (3) Active (2)" that is left to change. Thanks again! – Icarus Commented Feb 27, 2018 at 9:12
2 Answers
Reset to default 1The values haven't any hook to change this. However it is easy to identify the counter via Javascript. You can change this with short lines of Javascript.
Hook
You should hook in this page hook of the plugin page admin_footer-plugins.php
, like add_action( 'admin_footer-plugins.php', [ $this, 'change_view_values' ], 11 );
Function include javascript
As example a function below that change counter for the must-use
count, but you should only change the selector for the plugin counter. Small hint, the php variable $this->mustuse_total
is my store for the new count. You should replace this with your count, a function that get the count of plugins.
/**
* Change total count for must use values
*
* @since 01/09/2014
* @return void
*/
public function change_view_values() {
$current_screen = get_current_screen();
if ( null === $current_screen || 'plugins-network' !== $current_screen->id ) {
return;
}
$item = sprintf( _n( 'item', 'items', $this->mustuse_total ), number_format_i18n( $this->mustuse_total ) ); ?>
<script type="text/javascript">
jQuery( document ).ready( function($) {
let text,
value,
mustuse,
selector;
// replace the brackets and set int value
selector = '.mustuse span';
text = $( selector ).text();
value = text.replace( '(', '' );
value = parseInt( value.replace( ')', '' ) );
// replace and add strings
mustuse = value + <?php echo $this->mustuse_total; ?>;
$( selector ).replaceWith( '(' + mustuse + ')' );
mustuse = mustuse + ' <?php echo esc_attr( $item ); ?>';
if ( document.URL.search( /plugin_status=mustuse/ ) !== - 1 ) {
$( '.tablenav .displaying-num' ).replaceWith( mustuse );
}
} );
</script>
<?php
}
The code is currently in usage in this mu-plugin loader - https://github/bueltge/must-use-loader/blob/master/must_use_loader.php#L290
Sreenshot say more
In my context I change the must use counter, easier to understand via the image below.
It's very simple. Just add -1 to count( $list ) on third line like below:
$totals = array();
foreach ( $plugins as $type => $list )
$totals[ $type ] = count( $list )-1;