Creating a new widget for my website

Hello all!

I would like to create a widget plugin for my site and I was thinking of using the "Recent Global Posts" widget as a starting point.

I don't want to mess up that widget, but I want to copy it as a starting point for my own. Where should I start with in order to make it a plugin that could be reused?

Really I want to heavily re-style the existing widget, but it will not be possible without editing it.

  • Timothy
    • Chief Pigeon

    Hey Jason, how are you?

    Are we talking about just styling here and if so could you not style the existing one?

    First and foremost you would need to take care of any functions and classes, PHP will not let your declare them multiple times. So you need to rename them to avoid conflicts. If you don't then you will get redeclare errors.

    So for example if you had:

    function mywidget()

    then you would need a unique name if you wanted to use the same function twice:

    function my_newwidget()

    Some developers will use their initials and the plugin name get a unique function name, so in the example above if it were me a global posts widget it might be:

    function tbglobalposts_widget()

    That really is the basics of it. You may hit other snags along the way which require some debugging but without doing it myself I wouldn't be sure what might pop up.

    Hope this helps guide you in the right directions. :slight_smile:

  • DavidM
    • DEV MAN’s Mascot

    Yep, as Tim's mentioned, it'd involve renaming the functions and classes as far as I can see. As an example, on line 47 of widget-recent-global-posts.php you'll see this:
    class widget_recent_global_posts extends WP_Widget {

    You'll need to change that class name to your own, perhaps like so:
    class j_widget_recent_global_posts extends WP_Widget {

    -David

  • Jason
    • The Incredible Code Injector

    Thanks guys! That gives me a great start!

    I have to re-do the html as well and enqueue the new javascript needed to make it work.

    Basically, I'm turning it into a Carousel using jquery to make a large widget on my homepage of my template. I'll post a link when it's done, and see if anyone else is interested. :smiley:

  • Timothy
    • Chief Pigeon

    Hey Jason, you are most welcome! :slight_smile:

    Basically, I'm turning it into a Carousel using jquery to make a large widget on my homepage of my template. I'll post a link when it's done, and see if anyone else is interested. :smiley:

    That would be awesome. :slight_smile:

  • Jason
    • The Incredible Code Injector

    http://poteau.me/

    Thanks guys, I've got what I wanted implemented into a new plugin!

    I could do a full on options customization for the widget, and release it, what do you think?

    I need to style it some more, but I think it's looking pretty good right off the bat.

    The only realy hangup is it requires timthumb, but that could be substituted for webshots if you wanted to easily. (Could be another plugin option)

    So, what do you think?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.