Create a simple Wordpress widget?

justsmallsteps

New member
Joined
Mar 11, 2014
Messages
29
Points
0
Hello,

I'm not a programmer but today I must make any changes on my wordpress themes
I want to create a simple wordpress widget like widget Text got in wp widget at admin page
for example, I want create a new widget text named Text2

which you would suggest me?
 

webdesign

New member
Joined
Jul 5, 2012
Messages
229
Points
0
Hello,

I'm not a programmer but today I must make any changes on my wordpress themes
I want to create a simple wordpress widget like widget Text got in wp widget at admin page
for example, I want create a new widget text named Text2

which you would suggest me?
Go to your wordpress theme folder

Open functions.php and add these codes into it.

Code:
class My_Widget_Text2 extends WP_Widget {
    function My_Widget_Text2() {
        $widget_ops = array( 'classname' => 'widget_Text2', 'description' => __( "My Text2 Widget" ) );
        $this->WP_Widget('my_Text2', __('My Text2'), $widget_ops);
    }

    function widget( $args, $instance ) {
        extract($args);
        $text = apply_filters( 'widget_text', $instance['text'], $instance );
        echo $before_widget;
        ?>
            <div class="textwidget">
                <div class="Text2"> 
                    <div class="Text2text">
                        <p><?php echo $text; ?></p>
                    </div>
                </div>
                <div class="Text2bot"></div> 
            </div> 
        <?php
        echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        if ( current_user_can('unfiltered_html') )
            $instance['text'] =  $new_instance['text'];
        else
            $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
        return $instance;
    }

    function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'text' => '' ) );
        $text = format_to_edit($instance['text']);
?>

        <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
<?php
    }
}


function lbi_widgets_init() {
    register_widget( 'My_Widget_Text2' );
}
add_action( 'widgets_init', 'lbi_widgets_init' );
Go to widget section in your admin page, i.e yoursite(.)com/wp-admin/widgets.php

Find My Text2 widget on right coloum and adding to widget area as you want.


Good luck!
 

justsmallsteps

New member
Joined
Mar 11, 2014
Messages
29
Points
0
It worked, cool @webdesign

I have one question more, I want to create an area (widget area) to contain this widget on my homepage or category page, it's possible?
 

webdesign

New member
Joined
Jul 5, 2012
Messages
229
Points
0
It worked, cool @webdesign

I have one question more, I want to create an area (widget area) to contain this widget on my homepage or category page, it's possible?
Once again, go to functions.php

paste this code

Code:
function mywtext2() {

	register_sidebar( array(
		'name'          => 'My Text2 Sidebar',
		'id'            => 'home_right_1',
		'before_widget' => '<div>',
		'after_widget'  => '</div>',
		'before_title'  => '<h2 class="myclass">',
		'after_title'   => '</h2>',
	) );

}
add_action( 'widgets_init', 'mywtext2' );
Save it, back to theme folder

open file index.php or sidebar.php

add to where you want your widget My Text2 displayed.

<?php if ( is_home() ) { ?>
<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
<?php dynamic_sidebar( 'home_right_1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
<?php } ?>
Note if you only want it show at home or categories or single page or even only pages

add this is_category() or is_single() or is_page()

replace for if ( is_home() ) above

Best of luck.
 
Newer threads
Replies
6
Views
2,943
Replies
6
Views
4,501
Latest threads
Replies
2
Views
368
Replies
1
Views
366
Replies
4
Views
1,888
Replies
3
Views
794
Recommended threads
Replies
67
Views
16,959
Replies
0
Views
949
Replies
8
Views
3,941
Replies
5
Views
2,594
Replies
13
Views
3,867

Referral contests

Referral link for :

Sponsors

Popular tags

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top