Copy my user account’s wp-admin dashboard layout to other users?

Original code (License: MIT)

The following code is altered from my “Meta Box Order debug” plugin (available on GitHub as Gist). The explanation can be found in the code comments.

Benefit

The nice thing with this solution is, that it does not alter the DB values, but alters the data on the fly. This way you can any time revert the changes and/or use another user as “data origin”.

Another benefit is, that you can add a single dummy user that you simply use as blueprint. Every time you want to change the layout for everyone, simply log in with this user and alter the meta boxes on the dashboard. This way you can alter the configuration without writing a single line of code.

@TODO

You will have to change the user ID – this is marked as @TODO – to the user ID that you actually want to clone from/use as “origin”.

<?php

namespace WPSE;

/**
 * Plugin Name: (#144583) Clone Meta Box Order
 */

add_filter( 'get_user_metadata', 'WPSE\disableMetaBoxOrder', 10, 4 );
function disableMetaBoxOrder( $abort, $userID, $key, $single )
{
    // Too early: abort
    if (
        ! function_exists( 'get_current_screen' )
        OR NULL === get_current_screen()
        OR ! property_exists( get_current_screen(), 'base' )
    )
        return $abort;

    // Don't trigger on the wrong screens
    if ( 'dashboard' !== get_current_screen()->base  )
    {
        remove_filter( current_filter(), __FUNCTION__ );
        return $abort;
    }

    $target="meta-box-order_dashboard";

    // This is the part that actually alters/clones the user data
    // Trigger on default as on site specific settings
    if (
        $key === $target
        OR $key === $GLOBALS['wpdb']->prefix.$target
        )
    {
        // Prevent endless loop
        remove_filter( current_filter(), __FUNCTION__ );

        return get_user_meta(
            # @TODO Replace the next line with the user ID 
            # that you want to clone from (the origin)
            get_current_user_id(),
            $target,
            TRUE
        );
    }

    return $abort;
}

Leave a Comment