自定义主题中未显示 Wordpress 屏幕选项

Wordpress Screen Options not showing up in custom theme

我正在尝试将自定义屏幕选项写入我的子主题之一,但我似乎无法让它工作。我浏览了在 google 上找到的许多文章,但无论出于何种原因,它都没有显示出来。希望有人能指出我正确的方向。提前致谢。

以下是通过主题安装创建自定义管理选项页面的所有 类。

class country_class {

    public function __construct(){
        if (!class_exists('WP_List_Table')) {
            require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
        }

        add_action("load-lp_manage_countries", "lp_screen_options");
    }

    function lp_screen_options() {

        global $pippin_sample_page;
        $lp_manage_countries_page = "lp_manage_countries";

        $screen = get_current_screen();

        // get out of here if we are not on our settings page
        if(!is_object($screen) || $screen->id != $lp_manage_countries_page)
            return;

        $args = array(
            'label' => __('Countries per page'),
            'default' => 25,
            'option' => 'per_page'
        );
        add_screen_option( 'per_page', $args );
    }

    public function install_countries_page(){
        add_menu_page('LP Countries', 'LP Countries', 'manage_options', 'lp_manage_countries', array($this, 'show_country_page'));
    }

    public function show_country_page(){
        echo "<div class=\"wrap\">
                <h2>Manage Countries</h2>
                <form method=\"post\" action=\"options.php\">";

                //Prepare Table of elements
                $categories_list_table = new category_list_table();
                $categories_list_table->prepare_items();
                //Table of elements
                $categories_list_table->display();


        echo "<input type=\"submit\" value=\"Submit\">
                </form>
                </div>";
    }

    public function create_countries_table(){
        global $wpdb;
        $charset = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE IF NOT EXISTS `countries` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `name` varchar(45) NOT NULL,
            `parent_id` int(11) DEFAULT NULL,
            `image` varchar(45) DEFAULT NULL,
            PRIMARY KEY (`id`)
            ) $charset; ";

        $wpdb->query( $sql );

    }

    public function drop_countries_table(){
        global $wpdb;
        $sql = "DROP TABLE `countries`;";
        $wpdb->query($sql);
    }

}


class category_list_table extends WP_List_Table {

public function __construct(){
    parent::__construct( array(
        'singular' => 'Country',
        'plural' => 'Countries',
            'ajax' => false)
    );
}

public function get_columns(){
    return $columns = array(
        'cb' => '<input type="checkbox" />',
        'id' => __('ID'),
        'name' => __('Name'),
        'parent_id' => __('Parent ID'),
        'image' => __('Image')
    );
}

function column_name($item) {
    $actions = array(
        'edit' => sprintf('<a href="?page=%s&action=%s&country=%s">Edit</a>',$_REQUEST['page'],'edit',$item['id']),
        'delete' => sprintf('<a href="?page=%s&action=%s&country=%s">Delete</a>',$_REQUEST['page'],'delete',$item['id']),
    );
    return sprintf('%1$s %2$s', $item['name'], $this->row_actions($actions) );
}

public function get_sortable_columns(){

    return $sortable = array(
        'id' => false,
        'name' => true
    );
}

public function prepare_items(){
    global $wpdb, $_wp_column_headers;


    $screen = get_current_screen();

    /* Prepare the query */
    $query = "SELECT * FROM `countries`";

    /** Process bulk action */
    $this->process_bulk_action();

    /* Order Parameters */
    $orderby = !empty($_GET["orderby"]) ? mysql_real_escape_string($_GET["orderby"]) : 'ASC';
    $order = !empty($_GET["order"]) ? mysql_real_escape_string($_GET["order"]) : '';
    if(!empty($orderby) & !empty($order)){ $query.=' ORDER BY '.$orderby.' '.$order; }

    /* Pagination Params */
    //Number of elements in your table?
    $totalitems = $wpdb->query($query); //return the total number of affected rows

    //How many to display per page?
    $perpage = 30;

    //Which page is this?
    $paged = !empty($_GET["paged"]) ? mysql_real_escape_string($_GET["paged"]) : '';

    //Page Number
    if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }

    //How many pages do we have in total?
    $totalpages = ceil($totalitems/$perpage);

    //adjust the query to take pagination into account
    if(!empty($paged) && !empty($perpage)){
        $offset=($paged-1)*$perpage;
        $query.=' LIMIT '.(int)$offset.','.(int)$perpage;
    }

    /* Register pagination */
    $this->set_pagination_args( array(
        "total_items" => $totalitems,
        "total_pages" => $totalpages,
        "per_page" => $perpage
    )
    );

    /* register the columns */
    $columns = $this->get_columns();
    $_wp_column_headers[$screen->id]=$columns;

    /* Get the items */
    $this->items = $wpdb->get_results($query, 'ARRAY_A');

    $hidden_columns = array();
    $sortable_columns = array();
    $this->_column_headers = array($columns, $hidden_columns, $sortable_columns, "name");
}

public function column_default($item, $column_name) {
    return $item[$column_name];
}

public function no_items() {
    return 'No countries avaliable.';
}

function column_cb( $item ) {
    return sprintf(
        '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['ID']
    );
}

public function get_bulk_actions() {
    $actions = [
        'bulk-delete' => 'Delete'
    ];
    return $actions;
}

public function process_bulk_action() {

    //Detect when a bulk action is being triggered...
    if ( 'delete' === $this->current_action() ) {

        // In our file that handles the request, verify the nonce.
        $nonce = esc_attr( $_REQUEST['_wpnonce'] );

        if ( ! wp_verify_nonce( $nonce, 'lp_delete_country' ) ) {
            die( 'Go get a life script kiddies' );
        }
        else {
            self::delete_country( absint( $_GET['country'] ) );

            wp_redirect( esc_url( add_query_arg() ) );
            exit;
        }

    }

    // If the delete bulk action is triggered
    if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' )
        || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' )
    ) {

        $delete_ids = esc_sql( $_POST['bulk-delete'] );

        // loop over the array of record IDs and delete them
        foreach ( $delete_ids as $id ) {
            self::delete_country( $id );

        }

        wp_redirect( esc_url( add_query_arg() ) );
        exit;
    }

}

function delete_country( $id ) {
    global $wpdb;

    $wpdb->delete(
        "countries",
        [ 'ID' => $id ],
        [ '%d' ]
    );
}

}

您应该将 function lp_screen_options() 更改为 public function lp_screen_options(),然后在您的操作挂钩中使用以下内容定位它:

add_action("load-lp_manage_countries", array( $this, "lp_screen_options" ));

阅读有关在 类、in the Codex 中使用 add_action() 的更多信息。

我建议将添加菜单别名中的下划线更改为连字符。

发件人:

add_menu_page('LP Countries', 'LP Countries', 'manage_options', 'lp_manage_countries', array($this, 'show_country_page'));

收件人:

add_menu_page('LP Countries', 'LP Countries', 'manage_options', 'lp-manage-countries', array($this, 'show_country_page'));

更多信息请查看之前关于 url 中下划线的回答。

这是我发现的第一个问题。

这是解决此问题的方法。考虑到上述建议很好,存在一个潜在问题,即页面 ID 未被正确调用。

这是我改的 $lp_manage_countries_page = "lp_manage_countries"; 变成 //$this->page_name = 'lp_manage_countries'; $lp_manage_countries_page = "toplevel_page_" . $this->page_name;