我目前正在开发一个WordPress插件,通过视图或评论显示热门帖子。我知道这已经在其他插件中完成了,但我正在使用这个项目作为学习插件开发的机会,因为我是WordPress的新手。
我似乎让插件部分工作,但我遇到了我认为是范围问题。在插件选项页面中,我有一个单选按钮设置,用户可以用它来选择按视图或注释显示帖子。我在我的插件中的if else语句中使用单选按钮的值,以便启用正确的参数并在窗口小部件中显示。
我能够在设置页面上获取单选按钮的值,但插件if else语句似乎没有看到这些值,因此小部件中没有显示任何内容。
以下是我的插件PHP文件的内容:
<?php
/**
* Plugin Name: Popularity
* Description: Popular Posts Plugin
* Version: 0.1
* Author: Daniel
* Author URI: wordpress.org
* License: GPL2
*/
include 'popularity-settings.php';
/* Post Popularity Counter */
function post_views($postID) {
$total_key = 'views';
// Get current 'views' field
$total = get_post_meta($postID, $total_key, true);
// If current 'views' field is empty, set it to zero
if ($total == '') {
delete_post_meta($postID, $total_key);
add_post_meta($postID, $total_key, '0');
} else {
// If current 'views' field has a value, add 1 to that value
$total++;
update_post_meta($postID, $total_key, $total);
}
}
/* Dynamically inject counter into single posts */
function count_popular_posts($post_id) {
// Check that this is a single post and that the user is a visitor
if (!is_single()) return;
if (!is_user_logged_in()) {
// Get the post ID
if (empty($post_id)) {
global $post;
$post_id = $post->ID;
}
// Run Post Counter on post
post_views($post_id);
}
}
add_action('wp_head', 'count_popular_posts');
/* Add popular post function data to All Posts table */
function add_views_column($defaults) {
$defaults['post_views'] = 'View Count';
return $defaults;
}
add_filter('manage_posts_columns', 'add_views_column');
function display_views($column_name) {
if ($column_name === 'post_views') {
echo (int) get_post_meta(get_the_ID(), 'views', true);
}
}
add_action('manage_posts_custom_column', 'display_views', 5, 2);
/* Adds Popular Posts widget */
class popular_posts extends WP_Widget {
/* Register widget with WordPress */
function __construct() {
parent::__construct(
'popular_posts', // Base ID
__('Popular Posts', 'text_domain'), // Name
array( 'description' => __( 'Displays the 5 most popular posts', 'text_domain' ), ) // Args
);
}
/* Front-end display of widget */
public function widget( $args, $instance ) {
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
// Query args
if ($myplugin_options['orderby'] == 'views') {
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'order' => 'DESC',
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'ignore_sticky_posts' => true
);
} else if ($myplugin_options['orderby'] == 'comments') {
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'order' => 'DESC',
'orderby' => 'comment_count',
'ignore_sticky_posts' => true
);
}
// The Query
$the_query = new WP_Query( $query_args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>';
echo '<a href="' . get_the_permalink() . '" rel="bookmark">';
echo get_the_title();
if ($myplugin_options['orderby'] == 'views') {
echo ' (' . get_post_meta(get_the_ID(), 'views', true) . ')';
} else if ($myplugin_options['orderby'] == 'comments') {
echo comments_number(' (0)', ' (1)', ' (%)');
}
echo '</a>';
echo '</li>';
}
echo '</ul>';
echo $myplugin_options['orderby'];
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
echo $args['after_widget'];
}
/* Back-end widget form */
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Popular Posts', 'text_domain' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
}
/* Sanitize widget form values as they are saved */
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // class popular_posts
// register popular_posts widget
function register_popular_posts_widget() {
register_widget( 'popular_posts' );
}
add_action( 'widgets_init', 'register_popular_posts_widget' );
?>
以下是我的设置PHP文件的内容:
<?php
// create custom plugin settings menu
add_action( 'admin_menu', 'myplugin_create_menu' );
function myplugin_create_menu() {
//create new top-level menu
add_menu_page( 'Plugin Options', 'Plugin Options', 'manage_options', 'myplugin_main_menu', 'myplugin_settings_page', plugins_url( '/images/wordpress.png', __FILE__ ) );
//call register settings function
add_action( 'admin_init', 'myplugin_register_settings' );
}
function myplugin_register_settings() {
//register our settings
register_setting( 'myplugin-settings-group', 'myplugin_options', 'myplugin_sanitize_options' );
}
function myplugin_sanitize_options( $input ) {
return $input;
}
function myplugin_settings_page() {
?>
<div class="wrap">
<h2>Popularity Plugin Options</h2>
<form method="post" action="options.php">
<?php settings_fields( 'myplugin-settings-group' ); ?>
<?php $myplugin_options = get_option( 'myplugin_options' ); ?>
<h3>Order popular posts by</h3>
<table class="form-table">
<tr valign="top">
<th scope="row">Views</th>
<td><input name="myplugin_options[orderby]" type="radio" value="views"/></td>
</tr>
<tr valign="top">
<th scope="row">Comments</th>
<td><input name="myplugin_options[orderby]" type="radio" value="comments"/></td>
</tr>
</table>
<?php echo $myplugin_options['orderby']; ?>
<p class="submit">
<input type="submit" class="button-primary" value="Save Changes" />
</p>
</form>
</div>
<?php
}
?>
提前感谢您对我的所有指示。这是我对任何类型的插件开发的第一次尝试,我仍然在学习PHP,因为我相信你可以告诉。
答案 0 :(得分:1)
您似乎没有在widget()
方法中初始化插件选项:
$myplugin_options = get_option( 'myplugin_options' );