Mimo Estudio
Mimo Estudio

Reputation: 11

Wordpress Widget form function

I have this 2 functions in my Wordpress Widget, but I cannot get the form function to get the default or saved values of the widget options.

private static function get_defaults() {
    $defauls = array(
            'title' => '' , 
            'subtitle' => '' ,
            'columns' => '4' ,
            'ntax' => '' ,
            'showposts' => '12' ,
            'imagesize' => 'medium' ,
            'posttype' => '' ,
            'exclude' => '' ,
            'terms' => '' ,
            'border' => '' ,
            'padding' => ''
        );

    return $defaults;
}
/**
 * Generates the administration form for the widget.
 *
 * @param array instance The array of keys and values for the widget.
 */
public function form( $instance ) {
    $instance = wp_parse_args(
        (array)$instance,self::get_defaults()
    );

Upvotes: 1

Views: 612

Answers (1)

Robin Vinzenz
Robin Vinzenz

Reputation: 1277

I don't understand what you're trying to achieve but to build a widget please follow the Widget API.

To get the values of a specific widget "option" / attribute, you'll have to use $instance. For example, you're trying to get the title of the widget:

$instance['title']

If you're trying to get the widget's title in the form function, use:

 /**
 * Generates the administration form for the widget.
 *
 * @param array instance The array of keys and values for the widget.
 */
public function form( $instance ) {
    $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'New title', 'text_domain' );
);

What you're doing instead, is to override $instance with some args.

To learn more please consider to do some tutorials:

1: How To Build WordPress Widgets Like A Pro

2: How to Create a Custom WordPress Widget

Upvotes: 1

Related Questions