0
votes

i really love wordpress with tons of plugin and theme available out there, i just learn how to write my own custom widget.

my widget is work fine when i include it on my theme function. its show on backend and also on frontend just like another widget.

The only problem i got, when i add/create another widgets, the widgets(all widgets) start act wierd.

  • when i add the widget(s) to widget area it show the options(form) but then option suddently dissapear.

  • when i try to refresh the page (widget.php) on admin area the options on that widget appear normal and i can modify the value on that widget and save it just like normal widget.

This behavior affect the other functionality in my admin area such ass add image thumbnail is wierd(i can add thumbnail image, but image not shows on the thumbnail section on editor page.

Also with the other functionality, for example when i add new post it show on blank page when i hit save, also the same condition for adding category, page, menu, or anything that say add new. even on saving my theme options it direct me to blank page after i hit save.

this is the widget code.

<?php

class chalax_try_widget extends WP_Widget {

    /**
     * Register widget with WordPress.
     */
    function __construct() {
        parent::__construct(
            'chalax_try_widget', // Base ID
            __('Chalax try List', 'text_domain'), // Name
            array( 'description' => __( 'Widget untuk menampilkan daftar artikel/berita tertentu', 'text_domain' ), ) // Args
        );
    }

    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance ) {
        echo $args['before_widget'];
        echo $args['before_title'].$instance['title1'].$args['after_title'];
        echo "string";
        echo $args['after_widget'];
    }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form( $instance ) {
        if (isset($instance['title1'])) {
            # code...
            $title1=$instance['title1'];
        }else{
            $title1="jajal";
        }
        ?>
            <input id="<?php echo $this->get_field_id( 'title1' ); ?>" name="<?php echo $this->get_field_name( 'title1' ); ?>" value="<?php echo $title1 ;?>">
        <?php
    }

    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update( $new_instance, $old_instance ) {
        $instance  = array();
        $instance['title1'] = ( ! empty( $new_instance['title1'] ) ) ? strip_tags( $new_instance['title1'] ) : '';
        return $instance;
    }

} // class Foo_Widget

?>

the widget is work, but when i try to add another custome widget, say this..

<?php

class chalax_try_widget extends WP_Widget {

    /**
     * Register widget with WordPress.
     */
    function __construct() {
        parent::__construct(
            'chalax_try_widget', // Base ID
            __('Chalax try List', 'text_domain'), // Name
            array( 'description' => __( 'Widget untuk menampilkan daftar artikel/berita tertentu', 'text_domain' ), ) // Args
        );
    }

    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance ) {
        echo $args['before_widget'];
        echo $args['before_title'].$instance['title1'].$args['after_title'];
        echo "string";
        echo $args['after_widget'];
    }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form( $instance ) {
        if (isset($instance['title1'])) {
            # code...
            $title1=$instance['title1'];
        }else{
            $title1="jajal";
        }
        ?>
            <input id="<?php echo $this->get_field_id( 'title1' ); ?>" name="<?php echo $this->get_field_name( 'title1' ); ?>" value="<?php echo $title1 ;?>">
        <?php
    }

    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update( $new_instance, $old_instance ) {
        $instance  = array();
        $instance['title1'] = ( ! empty( $new_instance['title1'] ) ) ? strip_tags( $new_instance['title1'] ) : '';
        return $instance;
    }

} // class Foo_Widget

?>

then the problem comes up..

i follow the tutorial even copy this functions from codex..

1

1 Answers

0
votes

You should be getting a php error if you do the above. Basically you are declaring a class with the same name twice (same effect as declaring a function twice) Just rename the 2nd class.

Also you are missing the code to register the widgets, i assume it is there as something is changing. If not, add it in, its in the codex as well.