WordPress.org

Ready to get started?Download WordPress

Forums

Simple Image Widget
[resolved] Recommended practice for widget class extension (3 posts)

  1. NateWr
    Member
    Posted 1 year ago #

    Thanks for this simple to use plugin and for your blog post explaining clearly how to customize it through a class extension. I've worked with themes plenty but am less experienced with WP's plugin architecture, and the simplicity of extending Simple Image Widget has encouraged me to dive in.

    What is the best way (ie - future-proof) to create new widgets based on Simple Image Widget?

    I have created a separate plugin and included the Simple_Image_Widget class like this:

    require_once( plugin_dir_path( __FILE__ ) . '../simple-image-widget/class-simple-image-widget.php' );

    But is there a safer way to reference the plugin? If you change the folder name WP will break spectacularly. If I add my class into your plugin, the customizations will be overridden if my client updates your plugin (yes, I should perform the updates for them, but clients be clicky, ya know?).

    Is there a standard or safe way to handle plugin dependencies. A bit of Google research suggests it's a common problem, but I didn't see any documentation on best practices.

    http://wordpress.org/extend/plugins/simple-image-widget/

  2. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    Hi NateWr,

    You don't need to include the simple image widget class in your plugin and doing so doesn't really provide any benefits. Just check to see if the Simple_Image_Widget class exists before including your own widget. Something like this is how I handle it:

    function myprefix_widgets_init() {
    	if ( class_exists( 'Simple_Image_Widget' ) ) {
    		// Include your custom widget if the Simple Image Widget plugin is active.
    		include( 'class-my-custom-widget.php' );
    
    		// Register the widget.
    		register_widget( 'My_Custom_Widget' );
    	}
    }
    add_action( 'widgets_init', 'myprefix_widgets_init' );

    Now if the Simple Image Widget plugin is deactivated, your widget will no longer be registered (or throw any errors). You can take the concept a little further and display a message if you want your clients to know your custom widget requires the Simple Image Widget plugin to be activated.

    As far as plugin dependencies, just making sure the code you need exists before including any dependent code should be sufficient. You can usually do that with function_exists(), class_exists(), or check the active_plugins option. You can find a more thorough write-up on Otto's blog and it links to a library by Thomas Griffin if you want to get a little more fancy.

    Hope that helps.

    Cheers,
    Brady

  3. NateWr
    Member
    Posted 1 year ago #

    Thanks Brady! I was overcomplicating it as usual. Working well now.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.