0

I have almost no experience in creating widget area's and I am stuck at saving the widgets in the widget area 'red menu'. I have searched a dozen posts but I can't seem to get it working.

I am currently working in Wordpress 4.0.1 and this is the code I have:

Widgets.php:

class Red_Menu_Widget extends WP_Widget {
    function redmenu_widget( $args, $instance ) {
         $widget_ops = array(
         'classname' => 'redmenu_widget',
         'description' => __('Description in here')
         $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'redmenu_widget' );  

        $this->WP_Widget( 'redmenu_widget', __('Rood menu rechts', 'redmenu_widget'), $widget_ops, $control_ops );  
         );
        //parent::WP_Widget(false, 'Rood menu (rechts)', $widget_ops );
    }
    function widget() { 
        $title = 'Rood menu (rechts)';

        echo $before_widget;  
        echo $title;         
        echo '<p>This is my widget output</p>';
        echo $after_widget;  
    }
}
add_action( 'widgets_init', 'prefix_register_widgets' );
function prefix_register_widgets() {
    register_widget( 'redmenu_widget' );
}

Functions.php:

function pfp_widgets_init() {

  register_sidebar( array (
  'name' => __( 'Rood menu (rechts)', 'powerforpaws' ),
  'id' => 'redmenu_widget',
  'description'   => __( 'Descriptionhere', 'powerforpaws' ),
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => "</li>",
  'before_title' => '<h3 class="widget-title">',
  'after_title' => '</h3>',
  ) );
} // end pfp_widgets_init

add_action( 'widgets_init', 'pfp_widgets_init' );

// Pre-set Widgets
$preset_widgets = array (
        'redmenu_widget'  => array( 'search', 'pages', 'categories', 'archives' )
);

if ( !isset( $_GET['activated'] ) ) {
        update_option( 'sidebars_widgets', $preset_widgets );
}

Sidebar.php:

<aside id="widgets"> 
    <?php if ( !function_exists('redmenu_widget') || !dynamic_sidebar('redmenu_widget') ) : ?>
        <div id="primary" class="widget-area">
            <ul class="xoxo">
                <?php dynamic_sidebar('redmenu_widget'); ?>
            </ul>
        </div><!-- #primary .widget-area -->
        <?php endif; ?>   
</aside>

What am I doing wrong?

Thanks in advance!

0

2 Answers 2

2

This line of code is incorrect

<?php if ( !function_exists('redmenu_widget') || !dynamic_sidebar('redmenu_widget') ) : ?>

More to the point, your negative operator (!) is wrong here

That line of code simply translates to

If the function redmenu_widget does not exist OR if the sidebar redmenu_widget does not exists, do something

You should just drop the operator and also the !function_exists('redmenu_widget') part. You should also rather do a check to see if a sidebar is active. That part of code should look like this

<aside id="widgets"> 
    <?php if ( is_active_sidebar('redmenu_widget') ) : ?>
        <div id="primary" class="widget-area">
            <ul class="xoxo">
                <?php dynamic_sidebar('redmenu_widget'); ?>
            </ul>
        </div><!-- #primary .widget-area -->
        <?php endif; ?>   
</aside>

Also, your widget is a complete mess unfortunately. There are are lot of errors. You should check the Widget API and how to correctly create a widget. Also, when registering your widget, you want to use the class name.

EDIT

From your comments, you need a custom text widget. To accomplish this, simply copy the default text widget and customize as needed. Here is the default text widget, I have already renamed it, so you can just copy and paste it into your theme

class Red_Menu_Widget extends WP_Widget { 

    public function __construct() { 
        $widget_ops = array( 'classname' => 'redmenu_widget', 'description' =>  __('Rood menu rechts', 'redmenu_widget') ); 
        $control_ops = array( 'width' => 400, 'height' => 350 ); 
        parent::__construct( 'redmenu_widget', __('Rood menu rechts', 'redmenu_widget'),  $widget_ops, $control_ops ); 
    } 

    public function widget( $args, $instance ) { 

        /** This filter is documented in wp-includes/default-widgets.php */ 
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); 

        /** 
         * Filter the content of the Text widget. 
         * 
         * @since 2.3.0 
         * 
         * @param string    $widget_text The widget content. 
         * @param WP_Widget $instance    WP_Widget instance. 
         */ 
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance ); 
        echo $args['before_widget']; 
        if ( ! empty( $title ) ) { 
            echo $args['before_title'] . $title . $args['after_title']; 
        } ?> 
            <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div> 
        <?php 
        echo $args['after_widget']; 
    } 

    public function update( $new_instance, $old_instance ) { 
        $instance = $old_instance; 
        $instance['title'] = strip_tags($new_instance['title']); 
        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 
        $instance['filter'] = isset($new_instance['filter']); 
        return $instance; 

    public function form( $instance ) { 
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) ); 
        $title = strip_tags($instance['title']); 
        $text = esc_textarea($instance['text']); 
 ?> 
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p> 

        <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> 

        <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p> 
 <?php 
    } 
 } 

And then to register it

add_action( 'widgets_init', 'prefix_register_widgets' );
function prefix_register_widgets() {
    register_widget( 'Red_Menu_Widget' );
}

You can register your sidebar as per example in your question (just change the id as it might create conflict with your widget, call it something like redmenu-sidebar) and then call your sidebar as follows in your template file

<aside id="widgets"> 
    <?php if ( is_active_sidebar( 'redmenu_sidebar' ) ) : ?>
        <div id="primary" class="widget-area">
            <ul class="xoxo">
                <?php dynamic_sidebar('redmenu_sidebar'); ?>
            </ul>
        </div><!-- #primary .widget-area -->
        <?php endif; ?>   
</aside>

This should do it for you. Modify the code and widget to suit your needs

4
  • Yes the code is messed up. Your solution did not work for me. I am just trying to insert a little widget area so the user can put some text in there through the wordpress admin environment. I tried creating a widgetized area with the widget API you gave me, but I don't understand a thing :( But thank you for your detailed explaination! Jan 2, 2015 at 16:37
  • Please see my edit. Hope this helps Jan 3, 2015 at 5:05
  • Hi Pieter, my goal is to add a little area in my custom theme where I can put standard widgets in them. Jan 3, 2015 at 18:28
  • Then you should add that to your question. I've answered what was given. Please make sure to add all details to your question before posting so that no one wastes their time answering irrelevant info Jan 3, 2015 at 18:32
0

First, you should remove (!) and replace redmenu_widget with redmenuWidget. Because (_) not using in widget slugs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.