• Seems to be a bug in WordPress core:
    Tested in many ways. The following code example can allways reproduced.
    Registering is working in both cases. But when using namespaces the widget won’t be saved after inserting in a widgets area. After reloading the design/widgets page (admin backend) it has disappeared.

    This code works (not using namespace):
    File RPWidget:

    class RPWidget extends WP_Widget {
    ..
    }
    add_action( 'widgets_init', 'tcrp_register_rpwidget');

    plugin file:

    require_once 'classes/Widgets/RPWidget.php';
    
    new RPWidget ();
    
    function tcrp_register_rpwidget(){
        register_widget( 'RPWidget' );
    }

    This code doesn’t work (using namespace)

    File RPWidget:

    namespace TC_RP\Widgets;
    class RPWidget extends \WP_Widget {
    ..
    }
    add_action( 'widgets_init', 'tcrp_register_rpwidget');

    plugin file:

    new \TC_RP\Widgets\RPWidget();
    
    function tcrp_register_rpwidget(){
        register_widget( '\TC_RP\Widgets\RPWidget' );
    }
    • This topic was modified 4 years, 11 months ago by Tom.
    • This topic was modified 4 years, 11 months ago by Tom.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Why are you doing new RPWidget(); ?

    Look at https://developer.wordpress.org/reference/functions/register_widget/
    and you can see what it is expecting.

    Thread Starter Tom

    (@tomybyte)

    @joy OK you are right in the code without using namespcaces creating an instance of RPWidget class is quite not necessary.
    It comes from my original code with using namespaces. In this code I’m using an autoloader (like Justin Tadlock’s ) to require the file RPWidget.php.
    Definitly it is not working using namespaces and autoloading.

    Easiest way if your widget class is in the same namespace as your register function:

    
    namespace TC_RP\Widgets;
    
    add_action( 'widgets_init', __NAMESPACE__ . '\register_widgets' );
    
    function register_widgets() {
    
        register_widget( RPWidget::class );
    }
    

    If you’re keeping your functions in the global namespace, but the widget class is under a namespace:

    
    use TC_RP\Widgets\RPWidget;
    
    add_action( 'widgets_init', 'tcrp_register_widgets' );
    
    function tcrp_register_widgets() {
    
        register_widget( RPWidget::class );
    }
    
    Thread Starter Tom

    (@tomybyte)

    Thank you Justin @greenshady for your feedback. In my case as shown above I could register the widget but not save it in a widgets area. I’ll get in your code and test it and come back here tomorrow.

    Thread Starter Tom

    (@tomybyte)

    OK this is working with namespaces and autoloading the class files with Justins autoloader.
    in the plugin file (global namespace):

    add_action( 'widgets_init', array(new \TC_RP\Widgets\RPWidget, 'tcrp_register_rpwidget') );

    in the RPWidget.php file:

    namespace TC_RP\Widgets;
    
    class RPWidget extends \WP_Widget {
    
       function __construct() { 
            ...
        }
        
        ...
    
        public function tcrp_register_rpwidget(){
            register_widget( $this );
        }
    • This reply was modified 4 years, 10 months ago by Tom.
    • This reply was modified 4 years, 10 months ago by Tom.
    • This reply was modified 4 years, 10 months ago by Tom.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Bug register_widgets using php namespaces’ is closed to new replies.