admin管理员组

文章数量:1289868

StackExchange Community,

I am working on some Block Editor formats and blocks and would like to add a check to see if a certain plugin is installed. This would be specific to the back-end admin area of WordPress. For example something along the lines of:

if ( isPluginActive('plugin-name') ) {
 console.log('Plugin is active.')
}

I am aware that I could localize the data from PHP to my script but it would be much better to use any built in functions or packages if it is available. I did come across the getPlugins() method from the plugins package but this seems to refer to gutenberg plugins rather than WordPress plugins.

Update: I attempted to use apiFetch and was able to retrieve a list of installed plugins along with the status of each.

const isPluginActive = () => {
    apiFetch( { path: '/wp/v2/plugins' } ).then( ( plugins ) => {
        plugins.forEach( ( plugin ) => {
            const name = plugin.plugin
            const status = plugin.status
            
            if ( name === 'plugin-name/plugin-name' && status === 'active') {
                return true
            }
        })
    })
}

However, I am unsure how to use this to return a boolean response as a variable or function which I can use on my conditionals. It currently returns undefined.

StackExchange Community,

I am working on some Block Editor formats and blocks and would like to add a check to see if a certain plugin is installed. This would be specific to the back-end admin area of WordPress. For example something along the lines of:

if ( isPluginActive('plugin-name') ) {
 console.log('Plugin is active.')
}

I am aware that I could localize the data from PHP to my script but it would be much better to use any built in functions or packages if it is available. I did come across the getPlugins() method from the plugins package but this seems to refer to gutenberg plugins rather than WordPress plugins.

Update: I attempted to use apiFetch and was able to retrieve a list of installed plugins along with the status of each.

const isPluginActive = () => {
    apiFetch( { path: '/wp/v2/plugins' } ).then( ( plugins ) => {
        plugins.forEach( ( plugin ) => {
            const name = plugin.plugin
            const status = plugin.status
            
            if ( name === 'plugin-name/plugin-name' && status === 'active') {
                return true
            }
        })
    })
}

However, I am unsure how to use this to return a boolean response as a variable or function which I can use on my conditionals. It currently returns undefined.

Share Improve this question edited Jun 30, 2021 at 21:58 bula asked Jun 30, 2021 at 20:12 bulabula 636 bronze badges 2
  • What are you trying to achieve that requires this check? Any context you add will help inform an appropriate answer, and could even reveal approaches you weren't aware of – Tom J Nowell Commented Jun 30, 2021 at 20:39
  • Basically I have a cpt with it's own <PluginDocumentSettingPanel> adding fields specific to the cpt itself. I have some specific fields and conditionals that have been added to make provision for another plugin that has been added. I want those provisions to be removed if the plugin is inactive. – bula Commented Jun 30, 2021 at 21:50
Add a comment  | 

1 Answer 1

Reset to default 3

None of Gutenberg's data stores expose selectors for this purpose yet, to the best of my knowledge. If any did, I would have expected to find it on core. If and when the Gutenberg experience expands to the plugin management screen I reckon we'll see such a thing implemented.

For the time being, if the relevant users have the activate_plugins capability, you could acquire this data from the Plugins REST API endpoint using the @wordpress/api-fetch package:

import apiFetch from '@wordpress/api-fetch';

// ...
const plugins = await apiFetch( { path: '/wp/v2/plugins' } );

The objects produced by the request will have the fields described by the endpoint's schema, including the relative path to the main file, the name, version number, and active/inactive status.

It is possible to modify the permissions check callback for a core REST endpoint in order to expose this functionality to more users - but it's not very direct, and generally inadvisable. Creating a new endpoint or controller would probably be a better solution if you need to customize it's behavior.

本文标签: Check for Active WordPress Plugins from the Block Editor