Using WordPress ‘favorite_actions()’ PHP function

The favorite_actions() WordPress PHP function was previously used to display favorite actions dropdown in the WordPress admin interface. This function has been deprecated since WordPress 3.2. It is advised to use the WP_Admin_Bar class instead.

Usage

This function did not require any parameters. The typical usage was:

favorite_actions();

However, as it is deprecated, it should not be used in modern WordPress development.

Parameters

  • This function did not have any parameters.

More information

See WordPress Developer Resources: favorite_actions()

This function has been deprecated since WordPress 3.2. For adding items to the admin interface, consider using the WP_Admin_Bar class instead.

Examples

As this function has been deprecated, it should no longer be used in code. Here are some alternatives using the WP_Admin_Bar class instead:

Adding a new item to the admin bar

function add_to_admin_bar($admin_bar){
    $admin_bar->add_menu( array(
        'id'    => 'my-item',
        'title' => 'My Item',
        'href'  => '#',
        'meta'  => array(
            'title' => __('My Item'),
        ),
    ));
}
add_action('admin_bar_menu', 'add_to_admin_bar', 100);

In the above code, we’re adding a new item to the WordPress admin bar. We define an ID, a title, a hyperlink reference (href), and a meta array for our new item. Then we attach our function to the admin_bar_menu action.

Removing an item from the admin bar

function remove_from_admin_bar($admin_bar){
    $admin_bar->remove_menu('my-item');
}
add_action('admin_bar_menu', 'remove_from_admin_bar', 100);

This code removes a previously added item from the WordPress admin bar. We simply specify the ID of the item we wish to remove.

Checking if an item exists in the admin bar

function check_admin_bar($admin_bar){
    if( $admin_bar->get_node('my-item') ) {
        echo 'Item exists in the admin bar.';
    } else {
        echo 'Item does not exist in the admin bar.';
    }
}
add_action('admin_bar_menu', 'check_admin_bar', 100);

This function checks whether a specific item is present in the WordPress admin bar. The get_node function retrieves the item with the specified ID from the admin bar.

Modifying an existing item in the admin bar

function modify_admin_bar($admin_bar){
    $admin_bar->add_node( array(
        'id'    => 'my-item',
        'title' => 'Modified Item',
    ));
}
add_action('admin_bar_menu', 'modify_admin_bar', 100);

This function modifies an existing item in the WordPress admin bar. We simply specify the ID of the item we want to modify, and provide the new properties.

Adding a sub-item to an existing item in the admin bar

function add_subitem_admin_bar($admin_bar){
    $admin_bar->add_node( array(
        'parent' => 'my-item',
        'id'     => 'my-sub-item',
        'title'  => 'My Sub-Item',
        'href'   => '#',
    ));
}
add_action('admin_bar_menu', 'add_subitem_admin_bar', 100);

This function adds a sub-item to an existing item in the WordPress admin bar. We specify the parent item’s ID, and then provide the ID, title, and hyperlink reference (href) for the new sub-item. This sub-item will now appear in the dropdown menu of the parent item.