保存插件时出现权限错误

时间:2014-04-13 作者:Dannyw24

当我将报价传递到输入字段并保存它时,我在wordpress中收到此错误消息-我使用的是最新的安装版本。

错误“您没有足够的权限访问此页面。”

我得到的错误代码是针对一个插件的,我正在开发这样的插件,这是我的第一次尝试。

   <?php

/*idea to develop further would be, add a text box that the user can input the quote in
this then gets added to the DB and passed to the $quotes array. From here the results get
output the same way*/
/*
Plugin Name: Random Quotes
Plugin URI: xxx
Description: This Plugin randomly generates Quotes input by the user.
Version: 0.0.1
Author: xxx
Author URI: xxx
License: GPL2
*/

/*

Function Reference 
is_numeric() - Finds whether a variable is a number or a numeric string
unserialize() - takes a single serialized variable and converts it back into a PHP value.
isset() - tests to see if a variable isset
array_values($array) - Return all the values of an array input



*/

add_action(\'admin_menu\', \'dw_quotes_create_menu\');

function dw_quotes_create_menu() {
    //create custom top-level menu
    add_menu_page(\'Quotes Settings\', \'Quotes Styling\', \'manage_options\', __FILE__, \'dw_styling_quotes_settings\');
}

    //generating the random quote
function dw_get_random_quote() {
    $quotes = get_option(\'dw_quotes\', null); 
    $quotes = unserialize($quotes);
    $rand_quotes = array_rand($quotes);
    $result_quote = $quotes[$rand_quotes]; // assigns $result_quote the random quote

    return $result_quote; //output


}
    //styling the admin menu
function dw_styling_quotes_settings() {

    // load quotes
    $quotes = get_option(\'dw_quotes\', null);
    $quotes = unserialize($quotes); // unserialize the data from the $quotes array

    if (is_null($quotes)) {
        $quotes = array();
    }

    if (isset($_GET[\'delete\']) && is_numeric($_GET[\'delete\'])) { // $variable is set and its numeric
        unset($quotes[$_GET[\'delete\']]); // remove that quote from the array
        $quotes = array_values($quotes); // reorder the keys
        update_option(\'dw_quotes\', serialize($quotes)); // store results
        echo \'<p style="font-size:110%;color:green;"><strong>Quote Deleted</strong></p>\';
    }

    if ($_POST && isset($_POST[\'random_quote\']) && $_POST[\'random_quote\'] !== \'\') { //???
        array_push($quotes, $_POST[\'random_quote\']);
        update_option(\'dw_quotes\', serialize($quotes));
        echo \'<p style="font-size:110%;color:green;"><strong>Quote Added</strong></p>\';
    }

    ?>

    <div class="wrap">
        <?php global $wp_version;
            if ($wp_version < 3.1 ) {
                screen_icon( \'plugins\' );
        } ?>
        <h2>Quotes Page</h2>


        <form action="admin.php?page=wse140202.php" method="post">
            Add Quote:  <input style="width:600px;" type="textarea" name="random_quote" value="" />
            <br/><input type="submit" />
        </form>
    </div>

    <h3>Current Quotes</h3>
    <ul>
        <?php
        if ($quotes !== null) { // checks if $quotes is not set to 0
            $index = 0; //counter
            if (is_array($quotes)) { // checks if $quotes is an array to prevent error
            foreach ($quotes as $quote) {
                echo \'<li><strong>[ <a href="admin.php?page=wse140202.php&delete=\' . $index . \'">Delete</a> ]</strong>&nbsp;&nbsp;\' . $quote . \'</li>\';
                $index++; //increment counter
                }
            }
        }?>
    </ul>

    <h3>A Random Quote</h3>

   <?php // echo dw_get_random_quote();
}

?>
似乎是此页面导致权限错误admin.php?page=wse140202.php

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

它存储在一个名为quote的文件中。php

假设这意味着您的插件存储在wp-content/plugins/quote.php, 那么问题是您的代码是硬编码的,要提交给wse140202.php-- 大概你抄袭了一些代码from another question here.

换句话说,这一行:

add_menu_page(\'Quotes Settings\', \'Quotes Styling\', \'manage_options\', __FILE__, \'dw_styling_quotes_settings\');
根据文件名注册页面--quote.php, 不wse140202.php. 意味着您注册的页面位于admin.php?page=quote.php 而不是在admin.php?page=wse140202.php. 更正地址,它应该会起作用。

作为记录,按文件名注册这些页面对我来说毫无意义。它创建了一个外观奇怪、凌乱的URL,它揭示了文件系统数据可能对黑客有用,而这并不是必需的。使用简单的字符串名称注册页面。

add_menu_page(\'Quotes Settings\', \'Quotes Styling\', \'manage_options\', \'dw_quotes\', \'dw_styling_quotes_settings\');
和使用admin.php?page=dw_quotes 访问页面。

然后解决这些其他问题:)

结束