Search code examples
phpwordpresswordpress-themingcustom-wordpress-pages

Replace user-edit.php page within WordPress admin


I need to replace the page template used for the user-edit.php view within the WordPress admin interface.

I am not looking to change any core files, however I am wanting to create my own user-edit.php to be called instead.

So far I have tried creating the user-edit.php file and placing it in my child theme.

Can this be done with a custom plugin?


Solution

  • The WordPress admin pages don't really use templates in the same way that themes do. So, there isn't really a template to replace.

    That being said, here are your options to accomplish replacing the default WordPress user-edit.php page.

    1. [brute force method] straight up replace the core wp-admin/user-edit.php file with your own. I know you already said you don't want to replace core files; I'm just listing this for others who show up here. Obviously, this is not ideal because your file would get overwritten any time you update WordPress.

    2. [server redirect method] you could add the appropriate entry in your site's .htaccess file to redirect calls for https://yourdomain.tld/wp-admin/user-edit.php to your new page/file. Also not ideal because if your site ever switches to a new theme that doesn't have the replacement file, that redirect link would be broken.

    3. [additive method] Leave the existing core/default Users menu alone and just add your own admin menu (via add_menu_page) to include your new page(s). Then, just know that you just have to use that new menu instead of the Users menu. Better, but not ideal either since any links on other admin pages that point to user-edit.php would still point there and take admin users to the old/core page.

    4. [php intercept method] intercept calls to wp-admin/user-edit.php in your theme's php code and load your replacement file instead. This is probably the best method, although not the easiest to pull off. And you have to make sure that your replacement page still allows any plugins to function properly that might also use the user-edit page filters and hooks.

    To accomplish #4 [php intercept]:

    The first thing the core user-edit.php file does is loads wp-admin/admin.php. That file loads all the relevant admin stuff and eventually hands control back to user-edit.php. But, right BEFORE it does, it fires the action "load-user-edit.php"

    So, in your theme's functions.php file, use a function like this to intercept the request and redirect to your file:

    function scottfive_override_user_edit(){
        require_once( get_template_directory() . '/myadminfiles/user-edit-replacement.php' );
        die();
    }
    add_action( 'load-user-edit.php', 'scottfive_override_user_edit', 1 );
    

    Note that this method replaces the entire file, along with all of its functionality and security. I suggest taking a look at the original core user-edit.php and duplicate the authentication/authorization code to make sure your file is at least as secure as the original.

    Happy coding! :D