Run
Run

Reputation: 57176

wordpress how to customize text widget name?

I created a custom sidebar for adding text widgets.

function register_child_widgets() {
    register_sidebar(array(
        'name' => 'Social Media (Follow)',
        'id' => 'sidebar-follow',
        'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '',
        'after_title' => '',
    ));
    register_widget( 'Follow_Text_Widget' );
}
add_action( 'widgets_init', 'register_child_widgets' );

class Follow_Text_Widget extends WP_Widget_Text {
    function widget( $args, $instance ) {
        extract($args);
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
        echo $before_widget;
        if ( !empty( $title ) ) { echo $before_title . $after_title; } ?>
            <?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?>
        <?php
        echo $after_widget;
    }
}

Then I have the problem below when I use Vantage theme:

enter image description here

How can I customize the name of my text widget, let's say MyText? Is it possible?

Upvotes: 1

Views: 226

Answers (2)

Prakash Rao
Prakash Rao

Reputation: 2398

Try only passing the construct function in the class, It should create a widget naming "List Related Pages"

class Follow_Text_Widget extends WP_Widget_Text {
 function __construct() {

    parent::__construct(

        // base ID of the widget
        'tutsplus_list_pages_widget',

        // name of the widget
        __('List Related Pages', 'tutsplus' ),

        // widget options
        array (
            'description' => __( 'Identifies where the current page is in the site structure and displays a list of pages in the same section of the site. Only works on Pages.', 'tutsplus' )
        )

    );

 }
} 

and remove the other functions inside the class. So that you can detect if the name is changed then the problem is in other functions

Upvotes: 1

Robin Carlo Catacutan
Robin Carlo Catacutan

Reputation: 13679

You need to add this in your class.

  /**
   * Register widget with WordPress.
   */
  function __construct() {
    parent::__construct(
      'mytext', // Base ID
      __( 'MyText', 'text_domain' ), // Name
      array( 'description' => __( 'MyText description', 'text_domain' ), ) // Args
    );
  }

I see that you are using WP_Widget_Text that's why the constructor doesn't work.

You have to put the constructor to the class that extends WP_Widget.

e.g.

/**
 * Adds MyText widget.
 */
class MyText_Widget extends WP_Widget {

  /**
   * Register widget with WordPress.
   */
  function __construct() {
    parent::__construct(
      'mytext', // Base ID
      __( 'MyText', 'text_domain' ), // Name
      array( 'description' => __( 'MyText description', 'text_domain' ), ) // Args
    );
  }

}  

Source

Upvotes: 1

Related Questions