Creating a text widget WordPress open

Creating a text widget WordPress

<?php

class Another_Text_Widget extends WP_Widget {

    function __construct() {
        parent::__construct('another_text_widget', esc_html__('Another Text Widget', 'clean'), array('description'=>esc_html('Our', 'clean')));
    }

    public function widget($args, $instance) {
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);
        $title = apply_filters('the_content', $instance['text']);
    
    echo $before_widget;
    
    if($title) {
        echo $before_title . esc_html($title) . $after_title;
    }

    if($text) {
        echo wp_kses_post($text);
    }

    echo $after_widget;

    }


    public function form($instance) {
        if(isset($instance['title'])) {
            $title = $instance['title'];
        } else {
            $title = '';
        }
        if(isset($instance['text'])) {
            $title = $instance['text'];
        } else {
            $text = '';
        }
    
?>


<p>
    <label for="<?php echo $this->get_field_id('title'); ?>">
        <?php esc_html_e('Title','clean'); ?>
    </label>

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

</p>

<p>
    <label for="<?php echo $this->get_field_id('text'); ?>">
        <?php esc_html_e('Text','clean'); ?>
    </label>

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

</p>

<?php }


    public function update ($new_instance, $old_instance) {
        $instance = $old_instance;

        $instance['title'] = strip_tags($new_instance['title']);
        $instance['text'] = strip_tags($new_instance['text']);

        return $instance;
    }
}

function another_text_widget_load() {
	register_widget( 'another_text_widget' );
}
add_action( 'widgets_init', 'true_top_posts_widget_load' );
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation