显示自定义固定链接结构的管理部分因此导致权限错误

时间:2015-09-22 作者:dh47

使用插件,并且它在后端有分页,但分页结构采用自定义永久链接结构,因此在单击分页时会导致权限错误,

http://mydomain/wp-admin/admin.php?page=gift_registry/page/2/
导致错误的上述URL

http://mydomain/wp-admin/admin.php?page=gift_registry&paged=2
这很好用。

如何使其成为默认的永久链接结构,以便我可以访问页面而没有任何问题。

简而言之,如何在管理部分为这个插件提供默认永久链接,因为我们已经在管理部分为页面和后期分页提供了默认永久链接。

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

我通过如下所示的操作解决了此问题。

我使用Woo commerce礼品注册插件将代码替换为中的以下代码magenest-giftregistry-admin.php

<?php
if ( ! defined( \'ABSPATH\' ) ) exit; // Exit if accessed directly
if (! defined ( \'ABSPATH\' ))
    exit (); // Exit if accessed directly
class Magenest_Giftregistry_Admin {
        /**
         * Constructor.
         */
    public function __construct() {

    }
    public function giftregistry_manage() {
        if (isset ( $_REQUEST [\'delete\'] )) {
            if (isset ( $_REQUEST [\'id\'] ))
                $this->delete ( $_REQUEST [\'id\'] );
        } elseif (isset ( $_REQUEST [\'edit\'] )) {
            if (isset ( $_REQUEST [\'id\'] ))
                $this->edit ( $_REQUEST [\'id\'] );
        } elseif (isset ( $_REQUEST [\'delete\'] )) {
        } else {
            $this->index ();
        }
    }
    public function giftregistry_manages() {
        $rows_per_page = 10;
        $current = (intval(get_query_var(\'paged\'))) ? intval(get_query_var(\'paged\')) : 1;

        //$rows = $wpdb->get_results(\'SELECT * FROM subscriber ORDER BY sub_lname ASC\');

        $rows = Magenest_Giftregistry_Model::get_all_giftregistry();
        $start = ($current - 1) * $rows_per_page;
        $end = $start + $rows_per_page;
        $end = (sizeof($rows) < $end) ? sizeof($rows) : $end;

        $pagination_args = array(
                \'base\' => esc_url_raw(@add_query_arg(\'paged\',\'%#%\')),
                \'format\' => \'?page=%#%\',
                \'total\' => ceil(sizeof($rows)/$rows_per_page) + 1,
                \'current\' => $current,
                \'show_all\' => False,
                \'prev_next\'    => True,
                \'prev_text\'    => __(\' Previous\'),
                \'next_text\'    => __(\'Next \'),
                \'type\' => \'plain\',
                \'add_args\'     => False
        );

        echo paginate_links($pagination_args);
    }

    public static function index() {
        // Test the use of paginate_links

        $rows_per_page = 5;

        $current = (isset($_REQUEST[\'paged\'])&&intval($_REQUEST[\'paged\']) ) ? intval($_REQUEST[\'paged\']) : 1;

        // $rows is the array that we are going to paginate.
        $rows = Magenest_Giftregistry_Model::get_all_giftregistry();

        $max_page = ceil(sizeof($rows)/$rows_per_page);

        global $wp_rewrite,$wp_query;

        $pagination_args = array(
                \'base\' => esc_url_raw(@add_query_arg(\'paged\',\'%#%\')),
                \'format\' => \'\',
                \'total\' => ceil(sizeof($rows)/$rows_per_page),
                \'current\' => $current,
                \'show_all\' => false,
                \'type\' => \'plain\',
        );

        //if( $wp_rewrite->using_permalinks() )
        //  $pagination_args[\'base\'] = user_trailingslashit( trailingslashit( remove_query_arg(\'s\',get_pagenum_link(1) ) ) . \'page/%#%/\', \'paged\');

        if( !empty($wp_query->query_vars[\'s\']) )
            $pagination_args[\'add_args\'] = array(\'s\'=>get_query_var(\'s\'));

        echo paginate_links($pagination_args);

        $start = ($current - 1) * $rows_per_page;
        $end = $start + $rows_per_page;
        $end = (sizeof($rows) < $end) ? sizeof($rows) : $end;

        echo \'<table id="wishlit-tbl" class="wp-list-table widefat fixed">\';
        ?>
<thead>
    <tr>
        <th>
        <?php echo __(\'User id\')?>
        </th>
        <th>
        <?php echo __(\'Registrant name\')?>
        </th>
        <th>
        <?php echo __(\'Registrant email\')?>
        </th>
        <th>
        <?php echo __(\'Date Time\')?>
        </th>
        <th>
        <?php echo __(\'Delete\')?>
        </th>
        <th>
        <?php echo __(\'Edit\')?>
        </th>
    </tr>
</thead>
<?php 

        for ($i=$start;$i < $end ;++$i ) {
            $row = $rows[$i];
            $phpdate = strtotime( $row[\'event_date_time\'] );
            $order_date = date(\'d M, Y h:i A\', $phpdate);
            $http_schema = \'http://\';
            if (isset($_SERVER[\'HTTPS\']) && $_SERVER[\'HTTPS\'])  {
                $http_schema = \'https://\';
            }
            $delete_link = $http_schema. $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . \'&delete=1\';
            $edit_link = $http_schema. $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . \'&edit=1\';
            echo \'<tr>\';
            ?>
<td><?php echo $row[\'user_id\']?> </td>
<td><?php echo $row[\'registrant_firstname\']?> </td>
<td><?php echo $row[\'registrant_email\']?> </td>
<td><?php echo $order_date ?> </td>
<td><a href="<?php echo $delete_link.\'&id=\'.$row[\'id\'] ?>"><?php echo __(\'Delete\', GIFTREGISTRY_TEXT_DOMAIN)?></a>
</td>
<td><a href="<?php echo $edit_link.\'&id=\'.$row[\'id\'] ?>"><?php echo __(\'Edit\', GIFTREGISTRY_TEXT_DOMAIN)?></a>
</td>
<td></td>


<?php 
            echo \'</tr>\';

        }
        echo \'</table>\';
    }

    public function delete($id) {

        Magenest_Giftregistry_Model::delete_giftregistry($id);
    }
    public function edit($id) {
        ?>
        <br>
        <button onclick="window.location.href=\'<?php echo get_admin_url(null,\'admin.php?page=gift_registry\')?>\'" name="back" type="button" class="button button-primary button-large" id="back" accesskey="p" value="Back"><?php echo  __(\'Gift registry manage\')?> </button>
        <?php 
        echo \'<h2>Gift registry</h2>\';
        ob_start();
        $template_path = GIFTREGISTRY_PATH.\'template/account/\';
        $default_path = GIFTREGISTRY_PATH.\'template/account/\';

        wc_get_template( \'add-giftregistry.php\', array(
        \'wid\'       =>$id,

        ),$template_path,$default_path
        );
        echo  ob_get_clean();

        /////////////////////////////////////////////////////////////////////
        /////////////////////////////GIFT REGISTRY ITEMS/////////////////////
        ////////////////////////////////////////////////////////////////////
        $items = Magenest_Giftregistry_Model::get_items_in_giftregistry($id);
        ob_start();

        $template_path = GIFTREGISTRY_PATH.\'template/account/\';
        $default_path = GIFTREGISTRY_PATH.\'template/account/\';


        wc_get_template( \'my-giftregistry.php\', array(
        \'items\'         =>$items,
        \'wid\'       =>$id
        ),$template_path,$default_path
        );
        echo  ob_get_clean();
    }

}

相关推荐

如何从wp-admin/edit.php重定向到特定定制帖子类型的前端页面?

我正在基于Wordpress构建一个非常小的web应用程序。为此,我为用户可以在应用程序中管理的项目创建了自定义帖子类型。我已经创建了一个非常漂亮、优化的概览页面作为前端页面。然而,为了节省开发时间,编辑每个项目仍然在后端进行。后端中的某些操作(即删除项目)会将用户带到后端中的项目概述页面(/wp-admin/edit.php?post_type=my_item_post_type) 而不是我漂亮的前端页面。我想防止这种情况发生,这样当用户/wp-admin/edit.php?post_type=my_i