Reputation: 339
I am currently working on a WordPress plugin that displays popular posts by view or comments. I know this has been done in other plugins, but I am using this project as a chance to learn plugin development, as I am new to WordPress.
I seem to have the plugin partially working, but I have ran into what I believe to be a scope problem. In the plugin options page I have a radio button set that the user can use to choose between displaying posts by views or by comments. I am using the value of the radio button in an if else statement in my plugin in order to enable the correct arguments and display in the widget.
I am able to grab the value of the radio button on the settings page, but the plugin if else statement doesn't seem to be seeing those values so nothing gets displayed in the widget.
Here is the contents of my plugin PHP file:
<?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' );
?>
And here is the contents of my settings PHP file:
<?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
}
?>
Thanks in advance for any pointers you all might have for me. This is my first attempt at any type of plugin development, and I am still learning PHP as I am sure you can tell.
Upvotes: 0
Views: 236
Reputation: 6828
You did not seem to have initialized the plugin options in the widget()
method:
$myplugin_options = get_option( 'myplugin_options' );
Upvotes: 1