Display list of installed plugins in Wordpress admin area - wordpress

I would like to find a way of displaying a list of all my installed plugin in a neat widget on the home admin area of Wordpress.

Often when I'm doing performance audits on websites I like to clearly see exactly what I'm working with and having a complete list of plugins whereby I can copy and paste into an Google doc or sheet is really handy so I can go through the list and see what plugins I can remove, upgrade, and keep.
This solution is an adaptation of a similar solution found here.
However, that solution only displays active plugins and not a complete list. For example plugins like Easy Updates Manager wouldn't show as it's only admin-based.
The solution below shows a complete list of all installed plugins. Regardless whether they are active or not.
add_action('wp_dashboard_setup', 'wpse_54742_wp_dashboard_setup');
function wpse_54742_wp_dashboard_setup() {
wp_add_dashboard_widget( 'wpse_54742_active_site_plugins', __( 'Installed Plugins' ), 'wpse_54742_active_site_plugins' );
}
function wpse_54742_active_site_plugins() {
$all_plugins = get_plugins();
$active_plugins = get_option('active_plugins');
echo '<ul>';
foreach ( $active_plugins as $index => $plugin ) {
if ( array_key_exists( $plugin, $all_plugins ) ) {
//var_export( $all_plugins[ $plugin ] );
echo '<h4 style="margin-bottom:15px;font-weight:400;font-size:12px">', $all_plugins[ $plugin ][ 'Name' ], ' ('. $all_plugins[ $plugin ][ 'Version' ] .')</h4>';
}
}
echo '</ul>';
}
Unless you have a child theme installed, I wouldn't recommend adding this to your functions.php file. Instead, use a plugin like code snippets if you want to have better control over your code snippets.
Hope this helps some of you as I feel this is pretty handy to have. Equally, if any has a more elegant solution, I'd love to hear about it.

Related

Register Stylesheet if a particular plugin is installed

I use a basic custom plugin I have built for various WordPress admin functions.
One of them adds a custom CSS file that hooks into the admin as per the example below:
// Sets A Custom Admin Colour Scheme
function hits_admin_colour_scheme() {
wp_register_style('hits_admin_colour_scheme', plugins_url('/assets/css/admin.css',__FILE__ ));
wp_enqueue_style('hits_admin_colour_scheme');
}
add_action( 'admin_init','hits_admin_colour_scheme');
Is it possible to have an additional stylesheet that is only loaded if a particular plugin is installed? Say WooCommerce or ACF? This way I can reduce the size of the default CSS file and only load what is relevant.
Sorry if this is a newbie question. I appreciate any help.
You can check whether woocommerce(or others by same way) is activated before enqueue the file
function hits_admin_colour_scheme() {
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
wp_register_style('hits_aditional_colour_scheme', plugins_url('/assets/css/additional.css',__FILE__ ));
wp_enqueue_style('hits_aditional_colour_scheme');
}
}
add_action( 'admin_enqueue_scripts', 'hits_admin_colour_scheme');
and please use admin_enqueue_scripts hook to enqueue admin assets.

How Can I Change the Elementor Popup Location Settings?

I have an issue with one of the websites I work on where the "edit_in_content" location setting for Elementor popups is set to FALSE for all popups, and it is preventing the admin from going to the Elementor editor.
This issue presents itself as the common "the_content not found" error message, however, debugging leads to the location settings for popups being the issue. The problem is in the builder_wrapper method of \ElementorPro\Modules\ThemeBuilder\Classes\Locations_Manager, which is in /elementor-pro/modules/theme-builder/classes/locations-manager.php.
public function builder_wrapper( $content ) {
$post_id = get_the_ID();
if ( $post_id ) {
$document = Module::instance()->get_document( $post_id );
if ( $document ) {
$document_location = $document->get_location();
$location_settings = $this->get_location( $document_location );
/**
* Custom Modification Begin
* ------------------------------------
*/
if( $document_location == 'popup' )
return $content;
/**
* Custom Modification End
* ------------------------------------
*/
// If is a `content` document or the theme is not support the document location (header/footer and etc.).
if ( $location_settings && ! $location_settings['edit_in_content'] ) {
$content = '<div class="elementor-theme-builder-content-area">' . __( 'Content Area', 'elementor-pro' ) . '</div>';
}
}
}
return $content;
}
I'm aware that this is a plugin hack, and not recommended, but I have found no other way to handle the issue.
I'm not sure why edit_in_content is FALSE for all popups.
I've found no way to ensure that edit_in_content is TRUE so that the admin can work with popups.
To be fair, I should disclose that the theme is custom, but rather minimal. When switching to twentytwenty the issue goes away. ALL plugins have been disabled during testing, and it seems that the popup location setting is somehow being affected by the custom theme.
I've found nothing in twentytwenty that references elementor in any way.
I've been through Elementor's docs related to the_content issues, and nothing noted there leads to success. Ref: https://docs.elementor.com/article/56-content-area-not-found
So, I'm hoping somebody will be able to shed some light on this issue. What can I do so I don't have to hack the plugin?
I'm posting an answer because I found something that I could do to not hack the plugin, although I'm still curious to why it needs to be done. I basically just add a hook/action that reprocesses the popup location after it was initially registered. This could go in the theme's functions.php file:
function fixElementorPopupLocation( $that )
{
$loc = $that->get_location('popup');
if( ! $loc['edit_in_content'] )
{
$args = [
'label' => $loc['label'],
'multiple' => $loc['multiple'],
'public' => $loc['public'],
'edit_in_content' => TRUE,
'hook' => $loc['hook'],
];
$that->register_location('popup', $args);
}
}
add_action(
'elementor/theme/register_locations',
'fixElementorPopupLocation',
93062220
);
If anyone could tell me why I need to do this, and why I don't need this in the twentytwenty theme, I'd be glad to accept that as an answer, provided I can use the advice to "fix" my theme.

Using Timber for WordPress admin screens

I am building a WordPress plugin to show documentation related to the project. There is a good amount of HTML required in the views, with the content coming from markdown files.
I was wondering if it's at all possible to use Timber Plugin for admin screens, to help keep everything clean? I cannot find anything in the docs.
I have a function that renders a view (called by add_menu_page()) which is working fine. If I echo some text or HTML or even echo the contents of a file, I see what I'd expect.
Attempting to use Timber in the same way as I would in a theme, like this:
$context = Timber::get_context();
Timber::render(plugin_dir_url( __DIR__ ) . 'views/docs.twig', $context);
This just gives me an empty page, with no errors thrown at all.
Do I need to initialise Timber in some way first?
This is the first plugin I have made and my PHP isn't brilliant so apologies if this question isn't easy to follow or understand.
According to the Template Locations documentation, by default Timber looks in the views/ directory of the current theme for the template files. You can add other paths this way:
Timber\Timber::$locations = array(
plugin_dir_path( __FILE__ ) . 'views/'
);
Timber will look in that location first, so templates in the plugin will override any in the theme if they share the same name.
Yes! Just figured it out.
So, on your functions.php, do the usual procedure of adding a page on the admin side
prepare the function:
function documents_menu() {
add_menu_page('Docs', 'Docs', 'manage_options', '', 'myplguin_admin_page', 'dashicons-heart', 6);
}
then the hook:
add_action( 'admin_menu', 'documents_menu' );
then the renderer function:
function myplguin_admin_page(){
if ( ! class_exists( 'Timber' ) ) {
// if you want to show some error message, this is the right place
echo "Timber doesn't exist!";
return;`enter code here`
}
here's the key part:
Timber::render( 'docs.html.twig', array(
'args' => $args,
'instance' => $instance,
/* any other arguments */
));
}
then on your [theme]/views, create docs.html.twig and create your twig file!

Hide or remove comments box from wordpress pages

I am developing a plugin everything is going fine but there is a problem.
How can I hide or remove the comments box? I lost many hours but could not develop the right script. My current script is this:
add_filter( 'comments_template', 'remove_comments_template_on_pages', 11 );
function remove_comments_template_on_pages( $file ) {
if ( is_page() )
comments_template('', true);
}
I don't know how to develop this script. Any help could lead me to complete my plugin.
Add the below code to you functions.php file.
add_action('init', 'remove_page_feature');
function remove_page_feature() {
remove_post_type_support( 'page', 'comments' );
}
And now provide a check before the comments template is executed, as below:
if ( post_type_supports( 'page', 'comments' ); )
comments_template('', true);
}
Note: Using the above code will disable comments on pages.

How do I get the name of the file that is being used to render the current page?

Let's say I have a WordPress installation, with a page named "About". If I go to http://example.com/about, I know from WordPress' template hierarchy page that I'm looking at the theme file page.php.
I'm wondering if there's a way to display that fact (for theme debugging) on the page somewhere? Like what function (or code) would I call to display the current PHP page that is being used to render the page I'm looking at.
I could do something with $_SERVER['PHP_SELF'], but I'm looking for a way where I don't have to edit every PHP file. Like something that spits out the list of files it's using as the pages are called.
It can be printed in the Html source code like this:
add_action( 'wp_head', 'so_9405896_show_template', 999 );
function so_9405896_show_template() {
global $template;
echo '
<!--
TEMPLATE = ' . basename($template) .'
-->
';
}
Or for easier visualization, directly in the content with this:
add_filter( 'the_content', 'so_9405896_the_content_filter', 20, 1 );
function so_9405896_the_content_filter( $content )
{
if( is_admin() || !current_user_can( 'administrator' ) )
return $content;
global $template;
$the_templ = '<strong style="background-color: #CCC;padding:10px">TEMPLATE = '
. basename( $template ) . '</strong><br />';
$content = sprintf( $the_templ . '%s', $content );
return $content;
}
Which results in:
As far as I've seen there's no built in option to enable such logging, only for errors.
I'm not sure what editor you use, but most common text editors allow you to do a find replace across an entire folder.
I'd suggest doing a temporary replace on includes and require's to add an echo of the PHP_SELF. Just make sure to add a comment or something before the echo so that you can easily replace them with nothing when you're done.
A quick search on the WordPress plugin repository brings up a WordPress Debug Bar Template Trace.
I just manually type it into the template, i.e. ARCHIVE.PHP, CATEGORY-1.PHP when I'm building it. Just remember to delete it once the site goes live.
Simple and easy, if not so graceful.
For those looking for a newer answer:
<?php if ( is_user_logged_in() ) { global $template; echo basename($template); } ?>
This checks if a user is logged in, then only shows the template name. This can be handy if you need to hack your way on a live site without adding functions.

Resources