0

Ultimately I'm wanting to add some custom widget fields to the existing default image widget, and define it as a new custom widget.

I'm trying to create my own by extending the WP_Widget_Media_Image class, like this:

class Theme_Image_Widget extends WP_Widget_Media_Image
{
    /**
     * Widget identifier.
     */
    const WIDGET_SLUG = 'theme_image_widget';

    /**
     * Widget friendly name.
     */
    const WIDGET_NAME = 'Theme Image Widget';

    /**
     * Widget description.
     */
    const WIDGET_DESCRIPTION = 'foo';

    public function __construct()
    {
        WP_Widget_Media::__construct(
            self::WIDGET_SLUG,
            self::WIDGET_NAME,
            array(
                'classname'     => self::WIDGET_SLUG,
                'description'   => self::WIDGET_DESCRIPTION
            )
        );
    }

This code gets the new widget to appear in the widgets area, but it doesn't contain the normal UI that the default image widget has. I'm essentially looking to get the same UI over into this widget, and then add some additional fields to it.

Is there anyway to do this without having to re-write the JS UI code for the image widget?

  • You're calling WP_Widget_Media::__construct(). You probably want to call WP_Widget_Media_Image::__construct() to get all the benefits of the image widget. – MikeNGarrett May 1 at 20:21
  • 1
    It looks like the only thing that the WP_Widget_Media_Image::__construct does is set $this->l10n, other than that it just calls the WP_Widget_Media::__construct in the end. It also doesn't accept any constructor arguments, so I wouldn't be able to name it differently. – Timothy Fisher May 1 at 20:36

Your Answer

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

Browse other questions tagged or ask your own question.