Title: Bug register_widgets using php namespaces
Last modified: September 11, 2019

---

# Bug register_widgets using php namespaces

 *  [Tom](https://wordpress.org/support/users/tomybyte/)
 * (@tomybyte)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/bug-register_widgets-using-php-namespaces/)
 * 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 6 years, 9 months ago by [Tom](https://wordpress.org/support/users/tomybyte/).
    -  This topic was modified 6 years, 9 months ago by [Tom](https://wordpress.org/support/users/tomybyte/).

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/bug-register_widgets-using-php-namespaces/#post-11922090)
 * Why are you doing `new RPWidget();` ?
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/bug-register_widgets-using-php-namespaces/#post-11922098)
 * Look at [https://developer.wordpress.org/reference/functions/register_widget/](https://developer.wordpress.org/reference/functions/register_widget/)
   
   and you can see what it is expecting.
 *  Thread Starter [Tom](https://wordpress.org/support/users/tomybyte/)
 * (@tomybyte)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/bug-register_widgets-using-php-namespaces/#post-11922705)
 * [@joy](https://wordpress.org/support/users/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](http://justintadlock.com/archives/2018/12/14/php-namespaces-for-wordpress-developers))
   to require the file RPWidget.php. Definitly it is not working using namespaces
   and autoloading.
 *  [Justin Tadlock](https://wordpress.org/support/users/greenshady/)
 * (@greenshady)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/bug-register_widgets-using-php-namespaces/#post-11922723)
 * 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](https://wordpress.org/support/users/tomybyte/)
 * (@tomybyte)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/bug-register_widgets-using-php-namespaces/#post-11923434)
 * Thank you Justin [@greenshady](https://wordpress.org/support/users/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](https://wordpress.org/support/users/tomybyte/)
 * (@tomybyte)
 * [6 years, 9 months ago](https://wordpress.org/support/topic/bug-register_widgets-using-php-namespaces/#post-11926154)
 * 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 6 years, 9 months ago by [Tom](https://wordpress.org/support/users/tomybyte/).
    -  This reply was modified 6 years, 9 months ago by [Tom](https://wordpress.org/support/users/tomybyte/).
    -  This reply was modified 6 years, 9 months ago by [Tom](https://wordpress.org/support/users/tomybyte/).

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Bug register_widgets using php namespaces’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 3 participants
 * Last reply from: [Tom](https://wordpress.org/support/users/tomybyte/)
 * Last activity: [6 years, 9 months ago](https://wordpress.org/support/topic/bug-register_widgets-using-php-namespaces/#post-11926154)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
