Call to widgets_init function from controller in a controller it does not work

Hello

i have a conrtoller (app\controllers) with this code becouse i want to disable widgets from the admin area,
however the widgets_init function does not execute therefore the unregister_widget call does not run.
what am I doing wrong?
Is it possible to make calls to wordpress functions from controllers?


<?php

namespace App\Controllers;

use Sober\Controller\Controller;

class CustomUserOptions extends Controller
{

public function __construct() {
     add_action('widgets_init', [ $this, 'wpdocs_remove_calendar_widget' ], 99);

   }

function wpdocs_remove_calendar_widget() {      

        unregister_widget('WP_Widget_Calendar');     
      
        

    }
}

Thank you so much

hello

i have wrote this code in filters.php file


add_action('widgets_init', function () {  

      unregister_widget('WP_Widget_Pages');    

      unregister_widget('WP_Widget_Custom_HTML');

    

});

this works fine, so i can make calls to unregister_widget and widgets_init from filters.php

why canĀ“t do the same inside controller file from controllers folders?

thanks

Controllers are for modifying and passing data to blade templates. They are not executed during an admin request, therefore admin-specific code in them will never be executed.

thank alwaysblank

i have added the code in filters.php and works fine

This topic was automatically closed after 42 days. New replies are no longer allowed.