Using WordPress ‘admin_xml_ns’ PHP action

The admin_xml_ns WordPress PHP action fires inside the HTML tag in the admin header.

Usage

add_action('admin_xml_ns', 'your_custom_function');
function your_custom_function() {
    // your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: admin_xml_ns

Examples

Adding a custom attribute to the admin HTML tag

This example adds a custom attribute data-custom-attribute="value" to the admin HTML tag.

add_action('admin_xml_ns', 'add_custom_attribute_to_admin_html_tag');
function add_custom_attribute_to_admin_html_tag() {
    echo ' data-custom-attribute="value"';
}

Adding lang attribute to the admin HTML tag

This example adds a lang attribute with the value “en-US” to the admin HTML tag.

add_action('admin_xml_ns', 'add_lang_attribute_to_admin_html_tag');
function add_lang_attribute_to_admin_html_tag() {
    echo ' lang="en-US"';
}

Adding a custom prefix for RDFa

This example adds a custom prefix for RDFa to the admin HTML tag.

add_action('admin_xml_ns', 'add_custom_rdfa_prefix');
function add_custom_rdfa_prefix() {
    echo ' prefix="custom: http://example.com/custom#"';
}

Adding xmlns attributes for Facebook Open Graph

This example adds xmlns attributes for Facebook Open Graph to the admin HTML tag.

add_action('admin_xml_ns', 'add_facebook_open_graph_xmlns');
function add_facebook_open_graph_xmlns() {
    echo ' xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml"';
}

Adding a custom attribute based on user role

This example adds a custom attribute data-user-role with the current user’s role to the admin HTML tag.

add_action('admin_xml_ns', 'add_user_role_attribute');
function add_user_role_attribute() {
    $user = wp_get_current_user();
    $user_role = $user->roles ? $user->roles[0] : '';
    echo ' data-user-role="' . esc_attr($user_role) . '"';
}