1

So I am trying to create a widget-area. And after adding some code to the functions.php I noticed that I can not see "Widgets" in my menu. So I figured I had to add "widget support" to my theme so I started to search for this, but it seems there is function to register support(?) Only to registrer a widget-area.

So at this point I have no idea why "Widgets" are not showing up. The only possability is that there is something wrong in my functions.php/widget function.

This is what I have in my functions.php so far:

// Register widget area

// Function for initialize widget areas
function widget_areas () {

    // Widget area properties/args
    register_sidebar( array(
        'name'      =>      'singlepage_bottom', // Widget Area Name
        'id'      =>      'singlepage_bottom_1', // Widget Area ID
        'before_widget'      =>      '<div>', // Echo before widget area
        'after_widget'      =>      '</div>', // Echo after widget area
        'before_title'      =>      '<h2>', // Echo before widget title
        'after_title'      =>      '</h2>', // Echo after widget title
        ) );
}
add_action( 'widget_init', 'widget_areas' );

// END REGISTER WIDGET AREA

// Create widget

// Create a class that extends WP_Widet
class featured_widget extends WP_Widget {
    // function the defines the widget
    function __construct() {

        parent::__construct(
            // Widget Base ID
            'featured_widget',
            // Widget Name
            __( 'Featured Properties', 'John Doe' ),
            // Widget Option
            array(
                'description'   =>  __( 'Displays a carousel of featured properties.', 'John Doe' )
                )

            );
    }

    // Form function for the widget admin forms
    function form( $instance ) {
    }

    // Function to ensure WordPress updates user inputs
    function update( $new_instance, $old_instance ) {
    }

    // Function for the widget front-end
    function widget( $args, $instance ) {
    }
}

/* END WDIGETS */

If anybody can point me in the right direction for what I have done wrong. Or what I need to do I will be greatful.

  • Please also post the code that defines register_widgetArea(...). – Ejaz Aug 21 '15 at 11:56
  • Umm.. I have no code that defines register_widgetArea(), this is all I have in my functions.php file. Have I missed something? – Alex Aug 21 '15 at 12:00
  • 1
    I do not think that register_widgetArea(...) is a native WP function. May be you're after register_sidebar() codex.wordpress.org/Function_Reference/register_sidebar? – Ejaz Aug 21 '15 at 12:01
  • Oh, my bad! I have fixed that now, but I still can't see "Widgets" in the Apperance menu.. – Alex Aug 21 '15 at 12:12
  • 1
    you might also want to correct widget_init to widgets_init. That'll probably do it. – Ejaz Aug 21 '15 at 12:15
1

There are 2 problems here ( and a third unrelated problem )

Problem 1

You're calling register_widgetArea, a function that doesn't exist. Instead, you want to register a sidebar. If no sidebars are registered, the widgets menu does not appear in the admin area. Instead, use register_sidebar to register your sidebar.

I would expect calling register_widgetArea would generate a fatal error in PHP, but that doesn't happen because of Problem 2

Problem 2

Sidebars should be registered on the widgets_init action, but you've used the widget_init action, which is never called. This is why your code did not generate fatal errors, as the function is never being called.

Problem 3

You've created a new widget, but it's not enough to define a class inheriting from WP_Widget, it needs to be registered too. A word of warning, a lot of examples try to use create_function when registering a widget to make the code shorter. This is bad, and equivalent to using eval, and encourages bad practices. Instead, declare a function the normal way and use its name.

I would also recommend that you either use PHP namespaces, or add prefixes to your functions and classes. Generic names can clash when other developers use the same names. Make sure that doesn't happen by prefixing your names, e.g. jg_widget_areas or jg_featured_widget

|improve this answer|||||
  • Extra thanks or pointing out Problem 3! :) – Alex Aug 21 '15 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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