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.