WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] My first simple widget, and it doesnt work! (2 posts)

  1. Inogo
    Member
    Posted 5 years ago #

    Hi there wordpressers

    Im trying to wrap my head around widgets and how to create them. Im having moderate success :)

    I've made a simple widget with a textarea, nothing else.
    It shows up under "Available Widgets", and I can add it to "Current Widgets" without problems. But when I hit "Save Changes" it pops back to "Available Widgets" and doesn't show up in the widget area on the site.

    I checked in the wp-options table in the database, and the text is saved like it should be.
    Can anyone help me figuring out what Im doing wrong? This is the code:

    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar(array('name' => 'Sidebar',
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '<h2>',
    'after_title' => '</h2>',
    ));
    if ( function_exists('register_sidebar') )
    register_sidebar(array('name' => 'Below Content',
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '<h2>',
    'after_title' => '</h2>',
    ));
    
    function myTestWidget() {
    
    	$settings = get_option("widget_mytestwidget");
    	$text = $settings['text'];?>
    	<div class="widget">
        <div class="wrap">
        <h2>Test Widget</h2>
        <p><?php echo $number; ?></p>
        </div>
    	</div>
    
    <?php }
    
    function myTestWidgetAdmin() {
    
    	$settings = get_option("widget_mytestwidget");
    
    	// check if anything's been sent
    	if (isset($_POST['update_test'])) {
    		$settings['text'] = strip_tags(stripslashes($_POST['mytest_text']));
    		update_option("widget_mytestwidget",$settings);
    	}
    
    	echo '<p style="text-align: right;">
    			<label for="mytest_text"> Text:
    			<textarea id="mytest_text" name="mytest_text" type="text" style="width: 200px;" value="'.$settings['text'].'" /></textarea></label></p>';
    	echo '<input type="hidden" id="update_test" name="update_test" value="1″ />';
    
    }
    
    register_sidebar_widget('My Test Widget', 'myTestWidget');
    register_widget_control('My Test Widget', 'myTestWidgetAdmin', 300, 200);
    ?>

    Oh and im doing it in the functions.php theme file, if that matters.

  2. Inogo
    Member
    Posted 5 years ago #

    Update: The code above almost works. The problem was this line..

    echo '<input type="hidden" id="update_test" name="update_test" value="1″ />';

    Typo in the value attribute - doh! (the quotes around "1")

Topic Closed

This topic has been closed to new replies.

About this Topic