检查用户权限是否适用于插件但在管理面板中产生错误

时间:2014-12-27 18:33:10

标签: php wordpress

我编写了一个小插件,用于检查用户权限并将相应的JSON发送到ExtJS客户端。

<?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);
}
?>

使用ExtJS,它可以正常工作。但是当我尝试使用“/ wordpress / wp-admin /”URL时,我收到错误:

  

警告:require(../../../ wp-load.php)[function.require]:失败   open stream:没有这样的文件或目录   Z:\家\本地主机\ WWW \ WordPress的\可湿性粉剂内容\插件\ CheckGalleryUser \ check_manage_options.php   第9行

     

致命错误:require()[function.require]:需要打开失败   '../../../wp-load.php'(include_path ='。; / usr / local / php5 / PEAR')in   Z:\家\本地主机\ WWW \ WordPress的\可湿性粉剂内容\插件\ CheckGalleryUser \ check_manage_options.php   第9行

我也尝试了另一种方法:

<?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);
?>

然后我得到了这样的错误:

  

致命错误:调用未定义的函数add_action()   Z:\家\本地主机\ WWW \ WordPress的\可湿性粉剂内容\插件\ CheckGalleryUser \ check_manage_options.php   第7行

2 个答案:

答案 0 :(得分:0)

请尝试以下方法

<?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' );

然后当您发送请求时,只需将其发送至http://your-site/?cgu_check=check

答案 1 :(得分:0)

问题解决了!

<?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);
}
?>

此外,我在php.ini中设置了output_buffering = On,因为此代码在“/ wordpress / wp-admin /”URL上导致“警告:无法修改标头信息 - 标头已发送”警告。 谢谢!