jamper
jamper

Reputation: 277

WordPress widget not displaying on the front

Have a custom widget using the class API structure that will not display on the front end. The data collected is being stored in the database and updated via wp-admin. When viewed through the customizer the widget is greyed out with opacity and is being rendered on the front end. I can't see where it's going wrong:

function widget( $args, $instance ) {

    extract( $args );

    echo $args['before_widget'];
    $title = apply_filters( 'widget_title', $instance['title'] );
    $description = empty( $instance['description'] ) ? ' ' : $instance['description'];
    if( !empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; }

    echo '<div class="cjd">';
    echo '<input type="text" class="email" name="cjd-email"/>';
    echo '<input type="button" class="subscribe-button" name="subscribe" value="'. _e( 'Add your email', 'cjd') .'" />';
    echo '</div>';
    echo $args['after_widget'];

}

Code Calling the widget:

 function cjd_subscribe_widget() {
    $widget_ops = array( 
        'classname' => 'cjd-widget', 
        'description' => __( 'Adds a input form allowing newsletter subscription.', 'cjd' )
    );

    $this->WP_Widget( 'cjd_subscribe_widget', 'CJD: ' . __( 'Client Subscribe', 'cjd' ), $widget_ops );
}

Widget registration:

function cjd_register_widgets() {
register_widget( 'cjd_subscribe_widget' );
}
add_action( 'widgets_init', 'cjd_register_widgets' );

Upvotes: 0

Views: 1222

Answers (2)

jamper
jamper

Reputation: 277

SOLVED: Turns out I had placed the widget in a is_admin() function call. once outside the widget was displaying. Thanks @JH

Upvotes: 1

Haring10
Haring10

Reputation: 1557

I think you need to register the widget before you can use it.

class MyNewWidget extends WP_Widget {

function MyNewWidget() {
    // Instantiate the parent object
    parent::__construct( false, 'My New Widget Title' );
}

function widget( $args, $instance ) {
    // Widget output
}

function update( $new_instance, $old_instance ) {
    // Save widget options
}

function form( $instance ) {
    // Output admin widget options form
}
}

function myplugin_register_widgets() {
register_widget( 'MyNewWidget' );
}

add_action( 'widgets_init', 'myplugin_register_widgets' );

and in your functions.php file:

register_widget( $widget_class );

Function Reference with Wordpress Source

EDIT

Try calling the widget like this:

<?php the_widget( $widget, $instance, $args ); ?> 

Upvotes: 0

Related Questions