I created a custom theme and writing a basic widget for my wordpress based website. If I paste the code in functions.php the code works fine, however if I paste the code in a separate file and keep it in the plugins directory, it doesn't show up in the admin > appearance > widgets.

The problem to keep the widget code in functions.php is it collides with some plugins I care.

I tried wp_register_widget as a wild guess but to no avail.

Here is the widget code (You can remove the 'include' line to test the code)

class Similar_Apps_Widget extends WP_Widget {

    public function __construct() {

        'Similar_Apps_Widget', // Base ID
        __('Similar Apps', 'text_domain'), // Name
        array( 'description' => __( 'Similar Apps Widget', 'text_domain' ), ) 

    public function form( $instance ) {


    public function update( $new_instance, $old_instance ) {


    public function widget( $args, $instance ) {

         ?> <h4>Similar Apps</h4> <?php
         include (TEMPLATEPATH . '/include/' . get_field('similarapps'). '-similar-products.php');

register_widget( 'Similar_Apps_Widget' );

and here is the functions.php code where I define the widgets placement


if (function_exists('register_sidebar')) {

    'name' => 'Widgetized Area',
    'id'   => 'widgetized-area',
    'description'   => 'This is a widgetized area.',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget'  => '</div>',
    'before_title'  => '<h4>',
    'after_title'   => '</h4>'

    'name' => 'Page Widget',
    'id'   => 'page-widget',
    'description'   => 'Page Widgets.',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget'  => '</div>',
    'before_title'  => '<h4>',
    'after_title'   => '</h4>'



and here is where it is inserted into the theme

<div id="page-widget">

    <?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('page-widget')) :     else : ?>
    <?php endif; ?>


You have to hook your widget registering like that:

add_action("widgets_init", "register_widgets");// Hook WP

 * Register widgets
function register_widgets() {
    require_once "FooWidget.php";

Same for the widget zones

add_action("widgets_init", "register_widgets_zones");

function register_widgets_zones() {
        "name" => "Foo Zone Page",
        "id" => "foo-zone-widgets",
        "class" => "panel",
        "before_widget" => '<div id="%1$s" class="widget %2$s">',
        "after_widget" => "</div>",
        "before_title" => "",
        "after_title" => "",
  • Where do I need to put that code? I tried in functions.php but it breaks the code. Tried first part in my-custom-widget.php and the second in functions.php but no success. – Tough Guy Jun 11 '14 at 13:07
  • Yes, you are supposed to put this declarations in your functions.php... What eroor did you get ? – Mike Hell Jun 11 '14 at 13:10
  • White blank page with a error in widgets.php on console. The character encoding of the HTML document was not declared. if the document contains characters from outside the US-ASCII range. --- I modified your code as add_action("widgets_init", "register_widgets");// Hook WP function register_widgets() { require_once "similar-apps-widget/similar-apps-widget.php"; register_widget("Similar_Apps_Widget"); } as my widget is in the similar-apps-widget directory in plugins directory. – Tough Guy Jun 11 '14 at 13:21

Your Answer

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