Blog post: WordPress functions.php snippets, 5 essential code examples for 2026

EEOS Growth Partners · Technical SEO

WordPress functions.php snippets: 5 essential code examples for 2026

9 min read · Updated August 2026 · By EEOS Growth Partners

wordpress functions.php snippets

Photo by Jakub Żerdzicki on Unsplash

WordPress functions.php snippets are one of the fastest ways to solve a specific problem without installing another plugin, and this guide covers five you'll actually use, starting with how to create an admin user when you're locked out of your dashboard.

WordPress functions.php snippets for creating an admin user

To create a new administrator account programmatically, hook a function into init that calls wp_create_user. Replace the username, password, and email with your own values before adding it to your child theme's functions.php:

add_action( 'init', 'create_admin_user' );

function create_admin_user() {
    if ( ! username_exists( 'newadmin' ) ) {
        $user_id = wp_create_user(
            'newadmin',
            'StrongPassword123!',
            'newadmin@yourdomain.com'
        );
        $user = new WP_User( $user_id );
        $user->set_role( 'administrator' );
    }
}

The username_exists check prevents duplicate accounts on repeated page loads, but this snippet should still be removed from functions.php the moment you've confirmed the new account works, leaving it in place means it runs on every page request indefinitely.

4 more essential snippets for functions.php

1. Hide the admin bar for non-admin users

add_action( 'after_setup_theme', 'hide_admin_bar_for_non_admins' );

function hide_admin_bar_for_non_admins() {
    if ( ! current_user_can( 'administrator' ) ) {
        show_admin_bar( false );
    }
}

2. Redirect users after login by role

add_filter( 'login_redirect', 'redirect_by_role', 10, 3 );

function redirect_by_role( $redirect_to, $request, $user ) {
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        if ( in_array( 'administrator', $user->roles ) ) {
            return admin_url();
        }
        return home_url( '/my-account/' );
    }
    return $redirect_to;
}

3. Enqueue scripts and styles correctly

add_action( 'wp_enqueue_scripts', 'enqueue_custom_assets' );

function enqueue_custom_assets() {
    wp_enqueue_style( 'custom-style', get_stylesheet_uri() );
    wp_enqueue_script(
        'custom-script',
        get_template_directory_uri() . '/js/custom.js',
        array( 'jquery' ),
        '1.0',
        true
    );
}

Passing true as the fifth argument loads the script in the footer, which improves perceived load speed and is directly relevant to Core Web Vitals scores.

4. Register a custom post type

add_action( 'init', 'register_case_studies_cpt' );

function register_case_studies_cpt() {
    register_post_type( 'case_study', array(
        'labels'       => array( 'name' => 'Case Studies' ),
        'public'       => true,
        'has_archive'  => true,
        'show_in_rest' => true,
        'supports'     => array( 'title', 'editor', 'thumbnail' ),
    ) );
}

Set public and show_in_rest to true if you want the post type indexed by search engines and available to block editor and sitemap plugins.

A clean functions.php file is not just tidy code, it's a security posture. Every line that doesn't need to be there is a line that can't cause problems.

How to safely test and remove snippets

Always add custom snippets to a child theme's functions.php, never the parent theme, since parent theme updates will silently overwrite your changes. Test on a staging site first, and if a snippet causes a white screen, revert the file via FTP or your host's file manager rather than trying to fix it live. For up-to-date guidance on which hooks are available in the current version of core, refer to WordPress's official Plugin Hooks documentation. If your site's broader technical health needs a review beyond individual snippets, our website development guide covers the full picture.

How EEOS helps you build and maintain WordPress sites

We review functions.php and plugin configurations for security risks, make sure custom post types and sitemaps are structured correctly for indexing, and confirm site performance isn't being undermined by unnecessary code, all as part of our standard technical audit process.

Need help with your WordPress site's technical setup?

Talk to EEOS about a technical review covering functions.php, security, and site performance.

Book a discovery call ↗