Force theme or disallow theme change

Can I force a theme? I mean that the user cannot change intentionaly or accidentaly the theme I made for that website.
I would like to do this from the core because I also want to lock all updates.

Related posts

2 comments

  1. Lot of useless comments on this post from what seems like a pretty simple question.

    change below $theme_directory_name = 'twentyfourteen'; to your theme… thats all you have to do. First function sets your theme no matter what. Second one disables interface to edit themes.

    function selfish_hardcoded_theme() {
        $theme_directory_name = 'twentyfourteen';
        add_filter( 'template', create_function( '$template', 'return "' . $theme_directory_name . '";' ) );
        add_filter( 'stylesheet', create_function( '$stylesheet', 'return "' . $theme_directory_name . '";' ) );
    }
    add_action( 'setup_theme', 'selfish_hardcoded_theme', -1 );
    
    
    add_filter( 'map_meta_cap', function( $required_caps, $cap ) {
        if ( 'edit_themes' == $cap || 'switch_themes' == $cap || 'install_themes' == $cap || 'delete_themes' == $cap || 'update_themes' == $cap ) {
            $required_caps[] = 'do_not_allow';
        }
        return $required_caps;
    }, 10, 2 );
    

    Theres other ways too :

    $theme = wp_get_theme(); 
    if ('twentyfourteen' !== $theme->name || 'twentyfourteen' !== $theme->parent_theme) { 
        switch_theme( 'twentyfourteen' );
    }
    

    You can also set the default theme constant in your wp-config.

    Lastly, remove themes menu from admin:

    function remove_themes_submenus() {
      global $submenu;
      unset($submenu['themes.php'][5]); // Removes 'Themes'
      unset($submenu['themes.php'][15]); // Removes Theme Installer tab
    }
    add_action('admin_menu', 'remove_themes_submenus');
    
  2. You could create a custom admin role and remove these standard admin capabilities:

    function remove_admin_capabilities() {
    
        $admin = get_role( 'administrator' );
    
         $caps = array(
            'install_themes',
            'update_themes',
            'delete_themes',
            'edit_themes',
            'switch_themes',
            'edit_theme_options',
        );
    
        foreach ( $caps as $cap ) {
    
            $admin->remove_cap( $cap );
        }
    }
    add_action( 'init', 'remove_admin_capabilities' );
    

    Or simply force them to use the Editor role.

Comments are closed.