Skip to content

Commit 66996c5

Browse files
committed
Added option to change Howdy in admin bar
1 parent 447a7d0 commit 66996c5

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Fixed: Firing sequence of `common_toolkit_loaded` hook
66
- Added: `ctk_environment` filter
7+
- Added: Ability to change or remove Howdy from admin bar
78
- Added: Ability to change login errors
89
- Added: Ability to cache JSON config file
910
- Added: Ability to disable WordPress core, plugin and/or theme updates

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ All variables are optional.
4040
| `feed_links` | Include RSS feed links in page head | bool | true |
4141
| `heartbeat` | Modify or disable the WordPress heartbeat. Set to integer to change, `false` to disable | bool/int | null |
4242
| `hide_login_errors` | Replaces login errors with generic "Login failed" text rather than specific reason | bool/string | null |
43+
| `howdy_message` | Change (string) or remove (false/null) Howdy message in WP admin bar | bool/string/null | true |
4344
| `meta_generator` | Enable or change meta generator tags in page head and RSS feeds | bool/string | false |
4445
| `script_attributes` | Enable support for [additional attributes](#add-attributes-to-enqueued-scripts) to script tags via wp_enqueue_script() | bool | flase |
4546
| `shortcodes` | Enable custom [shortcodes](#shortcodes) created by this class | bool | false |

common-toolkit.php

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ final public static function init() {
5050
'feed_links' => true,
5151
'heartbeat' => null,
5252
'hide_login_errors' => false,
53+
'howdy_message' => true,
5354
'meta_generator' => true,
5455
'script_attributes' => false,
5556
'shortcodes' => false,
@@ -144,6 +145,11 @@ final public static function init() {
144145
add_filter( 'login_errors', array( self::$instance, 'hide_login_errors' ) );
145146
}
146147

148+
// Change or remove Howdy message in admin bar
149+
if( self::get_config( 'common_toolkit/howdy_message' ) !== true ) {
150+
add_filter( 'admin_bar_menu', array( self::$instance, 'admin_bar_howdy' ), 25 );
151+
}
152+
147153
// Add filter to retrieve configuration values
148154
add_filter( 'ctk_config', array( self::$instance, 'ctk_config_filter' ) );
149155

@@ -167,7 +173,7 @@ final public static function init() {
167173
*
168174
* @param string $key Configuration variable path to retrieve
169175
* @param mixed $default The default value to return if $key is not found
170-
* @since 0.7.0
176+
* @since 0.8.0
171177
* @see https://github.com/dmhendricks/wordpress-toolkit/blob/master/core/ConfigRegistry.php
172178
*/
173179
public static function get_config( $key = null, $default = null ) {
@@ -214,7 +220,7 @@ public static function ctk_config_filter( $key = null ) {
214220
* Usage: echo apply_filters( 'ctk_environment', null ); // Echos current environment string
215221
* var_dump( apply_filters( 'ctk_environment', 'is_production' ) ); // Returns true if in production mode
216222
*
217-
* @since 0.8.0
223+
* @since 0.9.0
218224
*/
219225
public static function ctk_environment_filter( $key = null ) {
220226

@@ -232,7 +238,7 @@ public static function ctk_environment_filter( $key = null ) {
232238
* Remove Emoji code in page header.
233239
* Usage: define( 'CTK_CONFIG', [ 'disable_emojis' => true ] );
234240
*
235-
* @since 0.7.0
241+
* @since 0.8.0
236242
*/
237243
public function disable_emojis() {
238244

@@ -254,7 +260,7 @@ public function disable_emojis() {
254260
* Remove WordPress core, plugin and/or theme update notices
255261
* Usage: define( 'CTK_CONFIG', [ 'disable_updates' => [ 'core', 'plugin', 'theme' ] ] );
256262
*
257-
* @since 0.8.0
263+
* @since 0.9.0
258264
*/
259265
public function disable_updates() {
260266

@@ -266,7 +272,7 @@ public function disable_updates() {
266272
/**
267273
* Disables WordPress site search and return 404
268274
*
269-
* @since 0.8.0
275+
* @since 0.9.0
270276
*/
271277
public function disable_search( $query, $error = true ) {
272278

@@ -286,7 +292,7 @@ public function disable_search( $query, $error = true ) {
286292
* Set a different admin bar color color. Useful for differentiating among environnments.
287293
* Usage: define( 'CTK_CONFIG', [ 'admin_bar_color' => '#336699' ] );
288294
*
289-
* @since 0.7.0
295+
* @since 0.8.0
290296
*/
291297
public function change_admin_bar_color() {
292298

@@ -300,7 +306,7 @@ public function change_admin_bar_color() {
300306
* wp_enqueue_script( 'script-handle-defer-example', get_template_directory_uri() . '/assets/js/script.js#defer' );
301307
* wp_enqueue_script( 'script-custom-attributes', get_template_directory_uri() . '/assets/js/script.js?custom_attribute[]=custom-element|amp-ad' );
302308
*
303-
* @since 0.7.0
309+
* @since 0.8.0
304310
* @see http://php.net/manual/en/domdocument.loadhtml.php
305311
* @see http://php.net/manual/en/domelement.setattribute.php
306312
*/
@@ -355,7 +361,7 @@ public function defer_async_scripts( $tag, $handle, $src ) {
355361
* Usage: $parse_uri = parse_url( 'https://example.com/?hello=world#hash );
356362
* $uri = \MU_Plugins\CommonToolkit::build_url( $parse_uri );
357363
*
358-
* @since 0.7.0
364+
* @since 0.8.0
359365
* @see https://stackoverflow.com/a/35207936
360366
*/
361367
public static function build_url( array $parts ) {
@@ -388,7 +394,7 @@ public static function build_url( array $parts ) {
388394
* @param array $pairs Entire list of supported attributes and their defaults.
389395
* @param array $atts User defined attributes in shortcode tag.
390396
* @return array Combined and filtered attribute list.
391-
* @since 0.7.0
397+
* @since 0.8.0
392398
*/
393399
public static function set_default_atts( $pairs, $atts ) {
394400

@@ -410,7 +416,7 @@ public static function set_default_atts( $pairs, $atts ) {
410416
/**
411417
* Modify the WordPress heartbeat
412418
*
413-
* @since 0.8.0
419+
* @since 0.9.0
414420
* @see https://codex.wordpress.org/Function_Reference/wp_heartbeat_settings
415421
*/
416422
public function modify_heartbeat( $settings ) {
@@ -426,14 +432,16 @@ public function modify_heartbeat( $settings ) {
426432
/**
427433
* Hide login errors to mitigate brute force attacks
428434
*
429-
* @since 0.8.0
435+
* @since 0.9.0
430436
* @see https://codex.wordpress.org/Plugin_API/Filter_Reference/login_errors#Example
431437
*/
432438
public function hide_login_errors( $error ) {
433439

434440
global $errors;
435441
$err_codes = $errors->get_error_codes();
436442
$hide_login_errors = self::get_config( 'common_toolkit/hide_login_errors' );
443+
if( is_string( $hide_login_errors ) && empty( $hide_login_errors ) ) return null;
444+
437445
$custom_message = is_string( $hide_login_errors ) ? $hide_login_errors : '<strong>ERROR</strong>: Login failed. <a href="%s">Lost your password</a>?';
438446

439447
// Invalid username
@@ -450,10 +458,26 @@ public function hide_login_errors( $error ) {
450458

451459
}
452460

461+
/**
462+
* Change or remove Howdy message in admin bar
463+
*
464+
* @since 0.9.0
465+
*/
466+
public function admin_bar_howdy( $wp_admin_bar ) {
467+
468+
$message = trim( self::get_config( 'common_toolkit/howdy_message' ) ) ?: '';
469+
$my_account = $wp_admin_bar->get_node( 'my-account' );
470+
$wp_admin_bar->add_node([
471+
'id' => 'my-account',
472+
'title' => str_replace( 'Howdy,', $message, $my_account->title )
473+
]);
474+
475+
}
476+
453477
/**
454478
* Remove or modify meta generator tag.
455479
*
456-
* @since 0.7.0
480+
* @since 0.8.0
457481
*/
458482
public function modify_meta_generator_tags( $current, $type ) {
459483

@@ -479,7 +503,7 @@ public function modify_meta_generator_tags( $current, $type ) {
479503
* Usage: Current date/time: [get_datetime]
480504
* Copyright &copy;[get_datetime format="Y"] Your Company
481505
*
482-
* @since 0.7.0
506+
* @since 0.8.0
483507
* @see https://php.net/date
484508
*/
485509
public function shortcode_get_datetime( $atts ) {
@@ -494,7 +518,7 @@ public function shortcode_get_datetime( $atts ) {
494518
/**
495519
* Remove extra HTML tags added by DomDocument
496520
*
497-
* @since 0.7.0
521+
* @since 0.8.0
498522
*/
499523
private function strip_extra_dom_elements( $element ) {
500524

@@ -510,7 +534,7 @@ private function strip_extra_dom_elements( $element ) {
510534
* @param string $key The name of the cache key to set/retrieve
511535
* @param function $callback The callback function that return the uncached value
512536
* @return mixed
513-
* @since 0.8.0
537+
* @since 0.9.0
514538
*/
515539
public function get_cache_object( $key, $callback, $force = false ) {
516540

@@ -550,7 +574,7 @@ public function delete_config_cache() {
550574
/**
551575
* Magic method to return config as JSON string.
552576
*
553-
* @since 0.7.0
577+
* @since 0.8.0
554578
*/
555579
public function __toString() {
556580
return json_encode( self::get_config() );

0 commit comments

Comments
 (0)