Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress插件选项页面问题_Php_Wordpress_Plugins - Fatal编程技术网

Php WordPress插件选项页面问题

Php WordPress插件选项页面问题,php,wordpress,plugins,Php,Wordpress,Plugins,我目前正在开发一个WordPress插件,它通过查看或评论来显示热门帖子。我知道这已经在其他插件中完成了,但我正在利用这个项目作为学习插件开发的机会,因为我是WordPress的新手 我似乎有插件部分工作,但我遇到了我认为是一个范围问题。在插件选项页面中,我设置了一个单选按钮,用户可以使用它在通过视图或评论显示帖子之间进行选择。我在插件的if-else语句中使用单选按钮的值,以便启用正确的参数并在小部件中显示 我可以在设置页面上获取单选按钮的值,但是pluginif-else语句似乎没有看到这些

我目前正在开发一个WordPress插件,它通过查看或评论来显示热门帖子。我知道这已经在其他插件中完成了,但我正在利用这个项目作为学习插件开发的机会,因为我是WordPress的新手

我似乎有插件部分工作,但我遇到了我认为是一个范围问题。在插件选项页面中,我设置了一个单选按钮,用户可以使用它在通过视图或评论显示帖子之间进行选择。我在插件的if-else语句中使用单选按钮的值,以便启用正确的参数并在小部件中显示

我可以在设置页面上获取单选按钮的值,但是pluginif-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
// 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
}
?>



您似乎没有初始化
widget()
方法中的插件选项:

$myplugin_options = get_option( 'myplugin_options' );

哇,这正是问题所在。谢谢你指出这一点。现在,如果我能想出如何让单选按钮保持在设置检查。我曾经有过它,但现在它没有保存。小部件设置保存在
update()
方法中。你可能应该做一些类似于
update\u option('myplugin\u options',$new\u array)
我通过添加到每个输入中使它们再次工作。因此,我还有一个问题似乎无法解决。当插件激活时,我如何将其中一个单选按钮选项设置为默认值?我尝试使用register_activation_hook()将'views'指定为$myplugin_options['orderby']的默认值,但它似乎不起作用。
register_activation_hook
是设置默认插件选项的方法。也许发布一个新的Q?另外,
get\u option()
还有第二个参数,允许您设置默认或回退选项值,这可能对您很有用。