• Hello community

    I have a problem.
    I want to customize the widgets.php page.

    Idea:
    Each user has to have an own Dashboard with own widgets.

    First try:
    Copy the widgets.php file into my theme direcotry, add a new menu item that render the php file and change all links into the widgets.php so that it should work… with no success. Isn’t it possible or did I something wrong?

    Second try:
    Add a hook, so that ‘Inactive Sidebar (not used)’ and ‘Inactive Widgets’ will be not shown in the edit widget area. But I can’t find the appropriate hook/filter/action

    Thank you,
    Francesco Schirinzi

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi there,
    what are you trying to achieve exactly?
    Even if you manage to have different widgets appear to different users in the back-end, how would that work in the front end? What would a non-logged in user see?

    Thread Starter fschirinzi

    (@fschirinzi)

    Hello,

    The page is for only logged-in users. So i don’t have the problem with the non-logged in users.

    I like the drag & drop method to edit the Widgets. So i thought i could use it for my purpose.

    On the front end:
    dynamic_sidebar( $current_user->ID.’-homescreen-widgets’ );

    My thoughts:
    I create for each user a sidebar with the Sidebar-ID: [userID]-homescreen-widgets

    So each user only sees his own Sidebar.

    Now my problem is, that on the widgets.php site, will be shown all Sidebars. But I want that it show only the one for the user.

    The only solution I can think of this, is saving a different copy of $sidebars_widgets in each user’s meta, and overwriting the global one depending on who is logged in.

    I’d go about it like this. I assume all requests are from logged in users, so I’ll not add any is_user_logged_in() checks.

    This would go on your functions.php or similar:

    // Registed a sidebar for the user.
    add_action( 'widgets_init', 'my_widgets_init' );
    function my_widgets_init() {
    	register_sidebar( array(
    		'name'          => 'Sidebar Name',
    		'id'            => get_current_user_id() . '-homescreen-widgets',
    		'description'   => 'Place here the widgets that you want to display on your homescreen.',
    		'before_widget' => '<aside id="%1$s" class="%2$s widget group">',
    		'after_widget'  => '</aside>',
    		'before_title'  => '<h3 class="widget-title">',
    		'after_title'   => '</h3>'
    	) );
    }
    
    // This fires whenever sidebars_widgets gets updated.
    add_action( 'updated_option', 'my_updated_option' );
    function my_updated_option( $option ) {
    	if( 'sidebars_widgets' == $option ) {
    		update_user_meta( get_current_user_id(), 'sidebars_widgets', wp_get_sidebars_widgets() );
    	}
    }
    
    // Overwrite the array for all wp_get_sidebars_widgets() calls.
    add_filter('sidebars_widgets', 'my_sidebars_widgets');
    function my_sidebars_widgets($sidebars_widgets) {
    	$sidebars_widgets = get_user_meta(get_current_user_id(), 'sidebars_widgets');
    	return $sidebars_widgets;
    }

    And in your frontpage template:
    dynamic_sidebar( get_current_user_id() . '-homescreen-widgets' );

    Hope this helps.

    Thread Starter fschirinzi

    (@fschirinzi)

    @anastis Sourgoutsidis

    Thank you very much for your help!

    I don’t know why… but the widgets don’t get saved. I tried to change the code and debug. But I don’t know why it’s not working.

    For what is the overwrite function?

    Thanks a lot,
    Francesco

    Try changing:

    // This fires whenever sidebars_widgets gets updated.
    add_action( 'updated_option', 'my_updated_option' );
    function my_updated_option( $option ) {
    	if( 'sidebars_widgets' == $option ) {
    		update_user_meta( get_current_user_id(), 'sidebars_widgets', wp_get_sidebars_widgets() );
    	}
    }

    to:

    // This fires whenever sidebars_widgets gets updated.
    add_action( 'updated_option', 'my_updated_option', 10, 3 );
    function my_updated_option( $option, $old_value, $value ) {
    	if( 'sidebars_widgets' == $option ) {
    		update_user_meta( get_current_user_id(), 'sidebars_widgets', $value );
    	}
    }

    Thread Starter fschirinzi

    (@fschirinzi)

    unfortunately it isn’t working. I added the PHP errors:

    Is there a documentation to lookup how the widget get saved in the DB?

    PHP Errors:
    Illegal offset type in isset or empty – wp-includes/widgets.php:1483 – retrieve_widgets()
    Illegal offset type – wp-includes/widgets.php:1151 – is_active_widget()
    wp_list_widgets()

    Thanks for your help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Change widgets.php’ is closed to new replies.