custom_admin_bar_logo.php

custom_admin_bar_logo.php

The provided PHP code will create a new top-level settings menu in the WordPress dashboard named 'Custom Settings'. In this settings page, there is an input field where you can add the URL for the custom logo. This URL is saved as an option in the WordPress database. The custom logo is then added to the WordPress top admin bar using the 'admin_bar_menu' action. The logo is linked to the home URL of the website and is styled to a height of 32px with the width adjusting automatically. The code checks if the logo URL is set before adding it to the top bar to prevent errors.

<?php // Create custom plugin settings menu add_action('admin_menu', 'custom_create_menu'); function custom_create_menu() { //create new top-level menu add_menu_page('Custom Plugin Settings', 'Custom Settings', 'administrator', __FILE__, 'custom_settings_page' , plugins_url('/images/icon.png', __FILE__) ); //call register settings function add_action( 'admin_init', 'register_mysettings' ); } function register_mysettings() { //register our settings register_setting( 'custom-settings-group', 'logo_url' ); } function custom_settings_page() { ?> <div class="wrap"> <h1>Custom Plugin</h1> <form method="post" action="options.php"> <?php settings_fields( 'custom-settings-group' ); ?> <?php do_settings_sections( 'custom-settings-group' ); ?> <table class="form-table"> <tr valign="top"> <th scope="row">Logo URL</th> <td><input type="text" name="logo_url" value="<?php echo esc_attr( get_option('logo_url') ); ?>" /></td> </tr> </table> <?php submit_button(); ?> </form> </div> <?php } add_action('admin_bar_menu', 'custom_admin_bar_menu', 999); function custom_admin_bar_menu($wp_admin_bar) { $logo_url = get_option('logo_url'); if(!empty($logo_url)) { $args = array( 'id' => 'custom-logo', 'title' => '<img src="' . $logo_url . '" style="height: 32px; width: auto;">', 'href' => get_option('siteurl'), 'meta' => array( 'class' => 'custom-logo', 'title' => 'Home' ) ); $wp_admin_bar->add_node($args); } }

Frequently Asked Questions

The code adds a custom menu to the WordPress admin area where you can upload and save a logo URL, which is then displayed in the admin bar.