最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Widget Title is not saving in backend

programmeradmin1浏览0评论

I have developed a widget to show latest news. The widget functionality is working correctly but the problem is the Widget title is not saving in backend. What is the right way to do it?

class infotravel_news_list_widget extends WP_Widget {
    public function __construct() {
        parent::__construct( false, $name = __( 'Latest News', 'infotravel' ) );
    }

    public function form( $instance ) {

        $defaults = array(
                'title' => '',
        );

        extract( wp_parse_args( array( $instance, $defaults ) ) );

        // Widget title ?>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'infotravel' ); ?></label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $defaults['title'] ); ?>" />
        </p>

        <?php
    }

    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = isset( $new_instance['title'] ) ? wp_strip_all_tags( $new_instance['title'] ) : '';

        return $instance;
    }

    public function widget( $args, $instance ) {
        $news_args = array(
            'post_type'      => 'infotravel_news',
            'posts_per_page' => 5,
            'post_status'    => 'publish',
            'orderby'        => 'post_date',
            'order'          => 'desc'
        );
        $query = new WP_Query( $news_args );

        if( $query -> have_posts() ) :
            while( $query -> have_posts() ) : $query -> the_post(); ?>
                <h3><?php the_title(); ?></h3>
            <?php
            endwhile;
        endif;
    ?>

    <?php }
}

add_action( 'widgets_init', function(){
    register_widget( 'infotravel_news_list_widget' );
} );

I have developed a widget to show latest news. The widget functionality is working correctly but the problem is the Widget title is not saving in backend. What is the right way to do it?

class infotravel_news_list_widget extends WP_Widget {
    public function __construct() {
        parent::__construct( false, $name = __( 'Latest News', 'infotravel' ) );
    }

    public function form( $instance ) {

        $defaults = array(
                'title' => '',
        );

        extract( wp_parse_args( array( $instance, $defaults ) ) );

        // Widget title ?>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'infotravel' ); ?></label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $defaults['title'] ); ?>" />
        </p>

        <?php
    }

    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = isset( $new_instance['title'] ) ? wp_strip_all_tags( $new_instance['title'] ) : '';

        return $instance;
    }

    public function widget( $args, $instance ) {
        $news_args = array(
            'post_type'      => 'infotravel_news',
            'posts_per_page' => 5,
            'post_status'    => 'publish',
            'orderby'        => 'post_date',
            'order'          => 'desc'
        );
        $query = new WP_Query( $news_args );

        if( $query -> have_posts() ) :
            while( $query -> have_posts() ) : $query -> the_post(); ?>
                <h3><?php the_title(); ?></h3>
            <?php
            endwhile;
        endif;
    ?>

    <?php }
}

add_action( 'widgets_init', function(){
    register_widget( 'infotravel_news_list_widget' );
} );
Share Improve this question edited Sep 5, 2019 at 7:04 Subrata Sarkar asked Sep 5, 2019 at 6:57 Subrata SarkarSubrata Sarkar 2395 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

It's because you're always outputting the default value into the input, regardless of what the saved value is:

<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $defaults['title'] ); ?>" />

Specifically:

value="<?php echo esc_attr( $defaults['title'] ); ?>"

$defaults['title'] is always going to be an empty string. The saved title is inside $instance['title'], and, because of this line:

extract( wp_parse_args( array( $instance, $defaults ) ) );

It is also inside a variable called $title. This isn't immediately obvious, which is why using extract() like this is bad practice.

I suggest either setting the variables explicitly:

$title = isset( $instance['title'] ) ? $instance['title'] : $defaults['title']; 

In which case the value should be output like so:

value="<?php echo esc_attr( $title ); ?>"

Or putting the result of wp_parse_args() back into $instance:

public function form( $instance ) {
    $defaults = array(
        'title' => '',
    );

    $instance = wp_parse_args( array( $instance, $defaults ) );

    // ...etc.
}

In which case the value should be output like so:

value="<?php echo esc_attr( $instance['title'] ); ?>"
发布评论

评论列表(0)

  1. 暂无评论