Support » Developing with WordPress » Disable widget with simple php code

  • I want to disable a specific widget in this code.

    add_filter('wp_insert_post_data', 'aa_func_20150916080916', 20, 2);
    function aa_func_20150916080916($data , $postarr)
      $warn = "You have already posted your maximum number of posts";
      $current_user = get_current_user_id();
      $user_post_count = count_user_posts($current_user);
      if($user_post_count > 1 ) {
        >>>Disable specific widget here with PHP CODE<<<
      return $data;
    • This topic was modified 5 months, 4 weeks ago by  Tri Paloski.
Viewing 3 replies - 1 through 3 (of 3 total)
  • @4ever16,

    You can disable the specific widget through the wp_unregister_sidebar_widget() function.

    For details please see the Codex.

    Hi thanks for the answer but i dont understand how to put that function in my existing code #first post.

    Moderator bcworkz


    You do need the ID tag used to register the widget to do this. The most reliable way to determine this is locate the code that registers the widget. If you find it, also note what action and priority is used, as you need to ensure your removal code executes afterwards.

    If you cannot find this registration code, try temporarily adding this to a template file:

    // dump all registered widgets
    global $wp_registered_widgets;
    echo '<pre>';
    print_r( $wp_registered_widgets );
    echo '</pre>';

    Your widget ID will be in the output somewhere when the template is used.

    Once you have the ID tag, something like this should remove the widget, assuming it was registered prior to ‘init’ action, priority 999:

    // remove widget 'your_widget_ID'
    add_action('init', function(){ wp_unregister_sidebar_widget('your_widget_ID'); }, 999 );

    This can go in a theme’s functions.php, preferably a child theme so your code is protected from theme updates.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.