Check user permissions works in plugin but produce error in admin panel

I have wrote a small plugin that check user permissions and send corresponding JSON to ExtJS client.

<?php
/*
Plugin Name: Check gallery user
Description: Check gallery user
Version: 1.0
*/

if (strpos(__FILE__, 'check_manage_options.php') !== false) {
    require('../../../wp-load.php');
    $manage_options = 'no';
    if (is_user_logged_in() && current_user_can('manage_options')) {
        $manage_options = 'yes';
    }
    $perms = array("perms" => array("perm" => $manage_options));
    echo json_encode($perms);
}
?>

With ExtJS it works correctly. But when I try to come on “/wordpress/wp-admin/” URL, I’ve get error:

Read More

Warning: require(../../../wp-load.php) [function.require]: failed to
open stream: No such file or directory in
Z:homelocalhostwwwwordpresswp-contentpluginsCheckGalleryUsercheck_manage_options.php
on line 9

Fatal error: require() [function.require]: Failed opening required
‘../../../wp-load.php’ (include_path=’.;/usr/local/php5/PEAR’) in
Z:homelocalhostwwwwordpresswp-contentpluginsCheckGalleryUsercheck_manage_options.php
on line 9

Also I tried another approach:

<?php
/*
Plugin Name: Check gallery user
Description: Check gallery user
Version: 1.0
*/
add_action('init', 'check_gallery_user');
function check_gallery_user() {
$manage_options = 'no';
if (is_user_logged_in() && current_user_can('manage_options')) {
    $manage_options = 'yes';
}
}
$perms = array("perms" => array("perm" => $manage_options));
echo json_encode($perms);
?>

Then I get such error:

Fatal error: Call to undefined function add_action() in
Z:homelocalhostwwwwordpresswp-contentpluginsCheckGalleryUsercheck_manage_options.php
on line 7

Related posts

Leave a Reply

2 comments

  1. please try below approach

    <?php
    
    /*
    Plugin Name: Check gallery user
    Description: Check gallery user
    Version: 1.0
    */
    
    function cgu_init() {
        if ( isset( $_GET['cgu_check'] ) && 'check' === $_GET['cgu_check'] ) {
            $manage_options = 'no';
            if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) {
                $manage_options = 'yes';
            }
            $perms = array( "perms" => array( "perm" => $manage_options ) );
            echo json_encode( $perms );
            die();
        }
    }
    
    add_action( 'init', 'cgu_init' );
    

    And then when you send request just send it to http://your-site/?cgu_check=check

  2. Problem solved!

    <?php
    /*
    Plugin Name: Check gallery user
    Description: Check gallery user
    Version: 1.0
    */
    if ( isset( $_GET['cgu_check'] ) && 'check' === $_GET['cgu_check'] ) {
        require('../../../wp-load.php');
        $manage_options = 'no';
        if (is_user_logged_in() && current_user_can('manage_options')) {
            $manage_options = 'yes';
        }
        $perms = array("perms" => array("perm" => $manage_options));
        echo json_encode($perms);
    }
    ?>
    

    In addition I have set output_buffering = On in php.ini because this code caused an “Warning: Cannot modify header information – headers already sent” warning on “/wordpress/wp-admin/” URL.
    Thank you!