WordPress.org

Ready to get started?Download WordPress

Forums

Custom widget won't save (1 post)

  1. jstudios
    Member
    Posted 1 year ago #

    Trying to make a custom "recent entries" widget (not plugin) and when I drag it into the sidebar and save the settings and refresh the page it's not there.

    This is the code found on recent-entries-widget.php:

    <?php
    /*
    Plugin Name: Recent Entries Widget
    Description: Recent posts widget with more options
    Version: 1.0
    
    */
    
    // Add recent_entries_widget function to widgets_init, this will load the widget
    
    add_action( 'widgets_init', 'register_recent_entries_widget' );
    
    // Register the widget
    
    function register_recent_entries_widget() {
    	register_widget( 'Recent_Entries_Widget' );
    }
    
    // Extend WP_Widget with our widget
    
    class Recent_Entries_Widget extends WP_Widget {
    
    	/* Initial Widget Setup */
    
    	function Recent_Entries_Widget() {
    
    		// Widget setup
    		$widget_ops = array( 'classname' => 'recent-entries-widget', 'description' => __('Recent posts widget with more options.', 'framework') );
    
    		// Widget UI
    		$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'Recent_Entries_Widget' );
    
    		// Widget name and description
    		$this->WP_Widget( 'Recent_Entries_Widget', __('Recent Entries', 'framework'), $widget_ops, $control_ops );
    
    	}
    
    	/* Widget Content */
    
    	function widget( $args, $instance ) {
    		extract( $args );
    
    		//Widget title, entered in the widget settings
    		$title = apply_filters('widget_title', $instance['title'] );
    
    		/* Custom Options */
    
    		// Our options from the widget settings.
    		$number = $instance['number'];
    
    		// Before widget - as defined in your specific theme.
    		echo $before_widget;
    
    		/* Display The Widget */
    
    		// Output the widget title if the user entered one in the widget options.
    		if ( $title )
    			echo $before_title . $title . $after_title;
    
    			//Test The Widget Is Working
    			echo "WIDGET TEST OUTPUT"; 
    
    			/* After widget - as defined in your specific theme. */
    			echo $after_widget;
    	}	
    
    	/* Update Options */
    
    	function update( $new_instance, $old_instance ) {
    
    		$instance = $old_instance;
    		$instance['title'] = strip_tags( $new_instance['title'] );
    		$instance['number'] = strip_tags( $new_instance['number'] );
    
    		return $instance;
    	}
    
    	/* Widget Settings */
    
    	function form( $instance ) {
    
    		/* Default Widget Settings */
    
    		$defaults = array(
    			'title' => 'Recent Entries',
    			'number' => '4'
    		);
    
    		$instance = wp_parse_args( (array) $instance, $defaults ); 
    
    ?>
    
    	<p>
    		<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
    		<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="" />
    	</p>
    
    	<p class="post-amount">
    		<label for="<?php echo $this->get_field_id( 'number' ); ?>">Number of entries to show:</label>
    		<input style="width:15%;" type="text" class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="" />
    	</p>
    
    <?php
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic