WP_REST_Plugins_Controller::check_read_permission() WordPress Method

The WP_REST_Plugins_Controller::check_read_permission() method is used to check if the current user has permission to make a REST request to read plugin data. This is a core Wordpress method.

WP_REST_Plugins_Controller::check_read_permission( string $plugin ) #

Checks if the given plugin can be viewed by the current user.


Description

On multisite, this hides non-active network only plugins if the user does not have permission to manage network plugins.


Top ↑

Parameters

$plugin

(string)(Required)The plugin file to check.


Top ↑

Return

(true|WP_Error) True if can read, a WP_Error instance otherwise.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php

	protected function check_read_permission( $plugin ) {
		require_once ABSPATH . 'wp-admin/includes/plugin.php';

		if ( ! $this->is_plugin_installed( $plugin ) ) {
			return new WP_Error( 'rest_plugin_not_found', __( 'Plugin not found.' ), array( 'status' => 404 ) );
		}

		if ( ! is_multisite() ) {
			return true;
		}

		if ( ! is_network_only_plugin( $plugin ) || is_plugin_active( $plugin ) || current_user_can( 'manage_network_plugins' ) ) {
			return true;
		}

		return new WP_Error(
			'rest_cannot_view_plugin',
			__( 'Sorry, you are not allowed to manage this plugin.' ),
			array( 'status' => rest_authorization_required_code() )
		);
	}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.