【发布时间】:2018-01-07 18:36:11
【问题描述】:
当前问题: 我已经能够在仪表板管理区域中从我的 WP 的 sql 数据库中成功创建各种表列表,并使用 WP_LIST_TABLE 创建插件,但是在如何使用短代码。通常我会添加如下内容:
add_shortcode('joblist', 'Job_List_Plugin');
但是在这种情况下,它似乎不起作用。页面没有生成表格,而是您只看到添加到页面的短代码,所以在这种情况下 [joblist]
问题: 如何将简码元素添加到以下代码中,以允许我在前端页面上显示此表以供登录的访问者查看?示例/片段将不胜感激。
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class Job_List extends WP_List_Table {
/** Class constructor */
public function __construct() {
parent::__construct( [
'singular' => __( 'Custom List', 'red' ), //singular name of the listed records
'plural' => __( 'Custom Lists', 'red' ), //plural name of the listed records
'ajax' => false //does this table support ajax?
] );
}
/**
* Retrieve members data from the database
*
* @param int $per_page
* @param int $page_number
*
* @return mixed
*/
public static function get_jobs( $per_page = 10, $page_number = 1 ) {
global $wpdb;
$query = "SELECT * FROM custom_table ORDER BY PrintOrder";
if( ! empty( $_REQUEST['s'] ) ){
$search = esc_sql( $_REQUEST['s'] );
$query .= " WHERE Description LIKE '%{$search}%'";
}
if ( ! empty( $_REQUEST['orderby'] ) ) {
$query .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] );
$query .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC';
}
$query .= " LIMIT $per_page";
$query .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
$result = $wpdb->get_results( $query, 'ARRAY_A' );
return $result;
}
/**
* Returns the count of records in the database.
*
* @return null|string
*/
public static function record_count() {
global $wpdb;
$query = "SELECT COUNT(*) FROM custom_table";
if( ! empty( $_REQUEST['s'] ) ){
$search = esc_sql( $_REQUEST['s'] );
$query .= " WHERE Description LIKE '%{$search}%'";
}
return $wpdb->get_var( $query );
}
/** Text displayed when no member data is available */
public function no_items() {
_e( 'There is nothing display at this time.', 'red' );
}
/**
* Render a column when no column specific method exist.
*
* @param array $item
* @param string $column_name
*
* @return mixed
*/
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'Description':
case 'Class':
case 'EmpName':
case 'StartTime':
return $item[ $column_name ];
default:
return print_r( $item, true ); //Show the whole array for troubleshooting purposes
}
}
/**
* Associative array of columns
*
* @return array
*/
function get_columns() {
$columns = [
'Description' => __( 'Classification', 'red' ),
'Class' => __( 'Class', 'red' ),
'EmpName' => __( 'Employer Name', 'red' ),
'StartTime' => __( 'Start Time', 'red' )
];
return $columns;
}
/**
* Columns to make sortable.
*
* @return array
*/
public function get_sortable_columns() {
$sortable_columns = array(
'Description' => array( 'Classification', true ),
'Class' => array ( 'Class', true)
);
return $sortable_columns;
}
/**
* Handles data query and filter, sorting, and pagination.
*/
public function prepare_items() {
$this->_column_headers = $this->get_column_info();
/** Process bulk action */
$this->process_bulk_action();
$per_page = $this->get_items_per_page( 'jobs_per_page', 7 );
$current_page = $this->get_pagenum();
$total_items = self::record_count();
$this->set_pagination_args( [
'total_items' => $total_items, //calculate the total number of items
'per_page' => $per_page //determine how many items to show on a page
] );
$this->items = self::get_jobs( $per_page, $current_page );
}
}
class Job_List_Plugin {
// class instance
static $instance;
// joblist WP_List_Table object
public $joblist_obj;
// class constructor
public function __construct() {
add_filter( 'set-screen-option', [ __CLASS__, 'set_screen' ], 10, 3 );
add_action( 'admin_menu', [ $this, 'plugin_menu' ] );
}
public static function set_screen( $status, $option, $value ) {
return $value;
}
public function plugin_menu() {
$hook = add_menu_page(
'Job List',
'Job List',
'manage_options',
'joblist_viewer',
[ $this, 'plugin_settings_page' ]
);
add_action( "load-$hook", [ $this, 'screen_option' ] );
}
/**
* Plugin page
*/
public function plugin_settings_page() {
?>
<style>
table {display: block;overflow-x: scroll;}
th {min-width:100px;font-size:10px;}
p.search-box {float:none;}
#post-body-content {width:98%;}
</style>
<h2>IBEW 353 Member Management Portal</h2>
<p>To locate a specific Member enter their Card Number in the search field provided.</p>
<div id="post-body-content">
<div class="meta-box-sortables ui-sortable">
<form method="post">
<input type="hidden" name="page" value="joblist_viewer" />
<?php
$this->joblist_obj->prepare_items();
$this->joblist_obj->search_box('Search', 'search');
$this->joblist_obj->display(); ?>
</form>
</div>
</div>
<br class="clear">
<?php
}
/**
* Screen options
*/
public function screen_option() {
$option = 'per_page';
$args = [
'label' => 'Jobs Per Page:',
'default' => 5,
'option' => 'jobs_per_page'
];
add_screen_option( $option, $args );
$this->joblist_obj = new Job_List();
}
/** Singleton instance */
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
}
add_action( 'plugins_loaded', function () {
Job_List_Plugin::get_instance();
} );
更新 在@Jared Chu 的帮助下,我取得了一些进展,但现在出现了错误。但至少我看到简码开始起作用了。
这是我编辑的内容。
我添加的第 203 行(类构造)
// class constructor
public function __construct() {
add_filter( 'set-screen-option', [ __CLASS__, 'set_screen' ], 10, 3 );
add_action( 'admin_menu', [ $this, 'plugin_menu' ] );
add_shortcode('joblist', [ $this, 'plugin_settings_page' ] ); // Added shortcode as per suggestion from @Jared Chu
}
公共函数“plugin_settings_page”包含此代码
/**
* Plugin page
*/
public function plugin_settings_page() {
?>
<style>
table {display: block;overflow-x: scroll;}
th {min-width:100px;font-size:10px;}
p.search-box {float:none;}
#post-body-content {width:98%;}
</style>
<h2>IBEW 353 Member Management Portal</h2>
<p>To locate a specific Member enter their Card Number in the search field provided.</p>
<div id="post-body-content">
<div class="meta-box-sortables ui-sortable">
<form method="post">
<input type="hidden" name="page" value="joblist_viewer" />
<?php
$this->joblist_obj->prepare_items();
$this->joblist_obj->search_box('Search', 'search');
$this->joblist_obj->display(); ?>
</form>
</div>
</div>
<br class="clear">
<?php
}
但是,当我查看页面时,我将短代码放入其中,但出现以下错误:
致命错误:未捕获的错误:在第 249 行的 C:\wamp64\www\dev1\wp-content\plugins\Job-List-Plugin\index.php 中调用 null 上的成员函数 prepare_items()
【问题讨论】:
-
您是否收到错误消息?请更好地描述它。
-
页面未生成表格,而是您只看到添加到页面的简码,因此在这种情况下 [joblist] 更新了问题以反映。
-
也许这是一个愚蠢的问题,但你把它放在functions.php中了吗?
-
不,我已经成功创建了一个在仪表板中工作的插件,但我现在也想在我的网站前端显示它并且遇到了困难。没有愚蠢的问题。只有愚蠢的答案:) 希望我的不是大声笑
-
哈,没关系,这是整个过程的一部分。您的插件中是否有某些东西可能会阻止它在前端呈现?