Display adminbar only to a specific user id

Home » Snippets » Display adminbar only to a specific user id
0

Created with:

Visibility: 

public

Creator: Ahrale

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php

// Display admin bar only to specific user ID
function atar4u_display_admin_bar() {
    if (is_user_logged_in() && get_current_user_id() === 1) {
        add_filter('show_admin_bar', '__return_true');
    } else {
        add_filter('show_admin_bar', '__return_false');
    }
}
add_action('after_setup_theme', 'atar4u_display_admin_bar');
				

Explanation:

In the code snippet above, we're using the after_setup_theme action hook to add our custom function atar4u_display_admin_bar(). This function checks if the user is logged in and their user ID matches the specific ID you want to display the admin bar for (in this example, the ID is set to 1).

If the conditions are met, we use the add_filter() function to set the show_admin_bar filter to true, which displays the admin bar. If the conditions are not met, we set the filter to false, hiding the admin bar for all other users.

Remember to replace 1 with the actual user ID you want to target.

Register an account to save your snippets or go Pro to get more features.