> Hide or Modify Elements feature * * @param $wp_admin_bar object The admin bar. * @since 1.9.0 */ public function modify_admin_bar_menu( $wp_admin_bar ) { $options = get_option( ASENHA_SLUG_U, array() ); // Hide WP Logo Menu if ( array_key_exists( 'hide_ab_wp_logo_menu', $options ) && $options['hide_ab_wp_logo_menu'] ) { remove_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 ); // priority needs to match default value. Use QM to reference. } // Hide Customize Menu if ( array_key_exists( 'hide_ab_customize_menu', $options ) && $options['hide_ab_customize_menu'] ) { remove_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 ); // priority needs to match default value. Use QM to reference. } // Hide Updates Counter/Link if ( array_key_exists( 'hide_ab_updates_menu', $options ) && $options['hide_ab_updates_menu'] ) { remove_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 ); // priority needs to match default value. Use QM to reference. } // Hide Comments Counter/Link if ( array_key_exists( 'hide_ab_comments_menu', $options ) && $options['hide_ab_comments_menu'] ) { remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 ); // priority needs to match default value. Use QM to reference. } // Hide New Content Menu if ( array_key_exists( 'hide_ab_new_content_menu', $options ) && $options['hide_ab_new_content_menu'] ) { remove_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 ); // priority needs to match default value. Use QM to reference. } // Hide 'Howdy' text if ( array_key_exists( 'hide_ab_howdy', $options ) && $options['hide_ab_howdy'] ) { // Remove the whole my account sectino and later rebuild it remove_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 ); $current_user = wp_get_current_user(); $user_id = get_current_user_id(); $profile_url = get_edit_profile_url( $user_id ); $avatar = get_avatar( $user_id, 26 ); // size 26x26 pixels $display_name = $current_user->display_name; $class = 'with-avatar'; $wp_admin_bar->add_menu( array( 'id' => 'my-account', 'parent' => 'top-secondary', 'title' => $display_name . $avatar, 'href' => $profile_url, 'meta' => array( 'class' => $class, ), ) ); } } /** * Wrapper for admin notices being output on admin screens * * @since 1.2.0 */ public function admin_notices_wrapper() { if ( current_user_can( 'manage_options' ) ) { echo '
' ; } } /** * Admin bar menu item for the hidden admin notices * * @link https://developer.wordpress.org/reference/classes/wp_admin_bar/add_menu/ * @link https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/ * @since 1.2.0 */ public function admin_notices_menu( WP_Admin_Bar $wp_admin_bar ) { if ( current_user_can( 'manage_options' ) ) { $wp_admin_bar->add_menu( array( 'id' => 'asenha-hide-admin-notices', 'parent' => 'top-secondary', 'grou' => null, 'title' => 'Notices0', 'meta' => array( 'class' => 'asenha-admin-notices-menu', 'title' => 'Click to view hidden admin notices', ), ) ); } } /** * Inline CSS for the admin bar notices menu * * @since 1.2.0 */ public function admin_notices_menu_inline_css() { if ( current_user_can( 'manage_options' ) ) { wp_add_inline_style( 'admin-bar', ' #wpadminbar .asenha-admin-notices-counter { box-sizing: border-box; margin: 1px 0 -1px 6px ; padding: 2px 6px 3px 5px; min-width: 18px; height: 18px; border-radius: 50%; background-color: #ca4a1f; color: #fff; font-size: 11px; line-height: 1.6; text-align: center; } ' ); // Below we pre-emptively hide notices to avoid having them shown briefly before being moved into the notices panel via JS ?> get_dashboard_widgets(); $extra_options = get_option( 'admin_site_enhancements_extra', array() ); $extra_options['dashboard_widgets'] = $dashboard_widgets; update_option( 'admin_site_enhancements_extra', $extra_options ); // Disable widgets if ( is_array( $disabled_dashboard_widgets ) || is_object( $disabled_dashboard_widgets ) ) { foreach ( $disabled_dashboard_widgets as $disabled_widget_id_context_priority => $is_disabled ) { // e.g. dashboard_activity__normal__core => true/false if ( $is_disabled ) { $disabled_widget = explode( '__', $disabled_widget_id_context_priority ); $widget_id = $disabled_widget[0]; $widget_context = $disabled_widget[1]; $widget_priority = $disabled_widget[2]; // remove_meta_box( $widget_id, get_current_screen()->base, $widget_context ); unset( $wp_meta_boxes['dashboard'][$widget_context][$widget_priority][$widget_id] ); } } } } /** * Get dashboard widgets * * @since 4.2.0 */ public function get_dashboard_widgets() { global $wp_meta_boxes ; $dashboard_widgets = array(); if ( !isset( $wp_meta_boxes['dashboard'] ) ) { $extra_options = get_option( 'admin_site_enhancements_extra', array() ); if ( !array_key_exists( 'dashboard_widgets', $extra_options ) ) { require_once ABSPATH . '/wp-admin/includes/dashboard.php'; set_current_screen( 'dashboard' ); wp_dashboard_setup(); } } if ( isset( $wp_meta_boxes['dashboard'] ) ) { foreach ( $wp_meta_boxes['dashboard'] as $context => $priorities ) { foreach ( $priorities as $priority => $widgets ) { foreach ( $widgets as $widget_id => $data ) { $widget_title = ( isset( $data['title'] ) ? wp_strip_all_tags( preg_replace( '/
' ;
}
}
}
/**
* Show excerpt column in list tables for pages and post types that support excerpt.
*
* @since 1.0.0
*/
public function show_excerpt_column()
{
$post_types = get_post_types( array(
'public' => true,
), 'names' );
foreach ( $post_types as $post_type_key => $post_type_name ) {
if ( post_type_supports( $post_type_key, 'excerpt' ) ) {
add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'add_excerpt_column' ] );
add_action(
"manage_{$post_type_name}_posts_custom_column",
[ $this, 'add_excerpt' ],
10,
2
);
}
}
}
/**
* Add a column called Featured Image as the first column
*
* @param mixed $columns
* @return void
* @since 1.0.0
*/
public function add_excerpt_column( $columns )
{
$new_columns = array();
foreach ( $columns as $key => $value ) {
$new_columns[$key] = $value;
if ( $key == 'title' ) {
$new_columns['asenha-excerpt'] = 'Excerpt';
}
}
return $new_columns;
}
/**
* Echo featured image's in thumbnail size to a column
*
* @param mixed $column_name
* @param mixed $id
* @since 1.0.0
*/
public function add_excerpt( $column_name, $id )
{
if ( 'asenha-excerpt' === $column_name ) {
$excerpt = get_the_excerpt( $id );
// about 310 characters
$excerpt = substr( $excerpt, 0, 160 );
// truncate to 160 characters
$short_excerpt = substr( $excerpt, 0, strrpos( $excerpt, ' ' ) );
echo wp_kses_post( $short_excerpt ) ;
}
}
/**
* Add ID column list table of pages, posts, custom post types, media, taxonomies, custom taxonomies, users amd comments
*
* @since 1.0.0
*/
public function show_id_column()
{
// For pages and hierarchical post types list table
add_filter( 'manage_pages_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_pages_custom_column',
[ $this, 'add_id_echo_value' ],
10,
2
);
// For posts and non-hierarchical custom posts list table
add_filter( 'manage_posts_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_posts_custom_column',
[ $this, 'add_id_echo_value' ],
10,
2
);
// For media list table
add_filter( 'manage_media_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_media_custom_column',
[ $this, 'add_id_echo_value' ],
10,
2
);
// For list table of all taxonomies
$taxonomies = get_taxonomies( [
'public' => true,
], 'names' );
foreach ( $taxonomies as $taxonomy ) {
add_filter( 'manage_edit-' . $taxonomy . '_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_' . $taxonomy . '_custom_column',
[ $this, 'add_id_return_value' ],
10,
3
);
}
// For users list table
add_filter( 'manage_users_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_users_custom_column',
[ $this, 'add_id_return_value' ],
10,
3
);
// For comments list table
add_filter( 'manage_edit-comments_columns', [ $this, 'add_id_column' ] );
add_action(
'manage_comments_custom_column',
[ $this, 'add_id_echo_value' ],
10,
3
);
}
/**
* Add a column called ID
*
* @param mixed $columns
* @return void
* @since 1.0.0
*/
public function add_id_column( $columns )
{
$columns['asenha-id'] = 'ID';
return $columns;
}
/**
* Echo post ID value to a column
*
* @param mixed $column_name
* @param mixed $id
* @since 1.0.0
*/
public function add_id_echo_value( $column_name, $id )
{
if ( 'asenha-id' === $column_name ) {
echo esc_html( $id ) ;
}
}
/**
* Return post ID value to a column
*
* @param mixed $value
* @param mixed $column_name
* @param mixed $id
* @since 1.0.0
*/
public function add_id_return_value( $value, $column_name, $id )
{
if ( 'asenha-id' === $column_name ) {
$value = $id;
}
return $value;
}
/**
* Add ID in the action row of list tables for pages, posts, custom post types, media, taxonomies, custom taxonomies, users amd comments
*
* @since 4.7.4
*/
public function show_id_in_action_row()
{
add_filter(
'page_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'post_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'cat_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'tag_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'media_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'comment_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
add_filter(
'user_row_actions',
array( $this, 'add_id_in_action_row' ),
10,
2
);
}
/**
* Output the ID in the action row
*
* @since 4.7.4
*/
public function add_id_in_action_row( $actions, $object )
{
if ( current_user_can( 'edit_posts' ) ) {
// For pages, posts, custom post types, media/attachments, users
if ( property_exists( $object, 'ID' ) ) {
$id = $object->ID;
}
// For taxonomies
if ( property_exists( $object, 'term_id' ) ) {
$id = $object->term_id;
}
// For comments
if ( property_exists( $object, 'comment_ID' ) ) {
$id = $object->comment_ID;
}
$actions['asenha-list-table-item-id'] = 'ID: ' . $id . '';
}
return $actions;
}
/**
* Hide comments column in list tables for pages, post types that support comments, and alse media/attachments.
*
* @since 1.0.0
*/
public function hide_comments_column()
{
$post_types = get_post_types( array(
'public' => true,
), 'names' );
foreach ( $post_types as $post_type_key => $post_type_name ) {
if ( post_type_supports( $post_type_key, 'comments' ) ) {
if ( 'attachment' != $post_type_name ) {
// For list tables of pages, posts and other post types
add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'remove_comment_column' ] );
} else {
// For list table of media/attachment
add_filter( 'manage_media_columns', [ $this, 'remove_comment_column' ] );
}
}
}
}
/**
* Add a column called ID
*
* @param mixed $columns
* @return void
* @since 1.0.0
*/
public function remove_comment_column( $columns )
{
unset( $columns['comments'] );
return $columns;
}
/**
* Hide tags column in list tables for posts.
*
* @since 1.0.0
*/
public function hide_post_tags_column()
{
$post_types = get_post_types( array(
'public' => true,
), 'names' );
foreach ( $post_types as $post_type_key => $post_type_name ) {
if ( $post_type_name == 'post' ) {
add_filter( "manage_posts_columns", [ $this, 'remove_post_tags_column' ] );
}
}
}
/**
* Add a column called ID
*
* @param mixed $columns
* @return void
* @since 1.0.0
*/
public function remove_post_tags_column( $columns )
{
unset( $columns['tags'] );
return $columns;
}
/**
* Show custom (hierarchical) taxonomy filter(s) for all post types.
*
* @since 1.0.0
*/
public function show_custom_taxonomy_filters( $post_type )
{
$post_taxonomies = get_object_taxonomies( $post_type, 'objects' );
// Only show custom taxonomy filters for post types other than 'post'
if ( 'post' != $post_type ) {
array_walk( $post_taxonomies, [ $this, 'output_taxonomy_filter' ] );
}
}
/**
* Output filter on the post type's list table for a taxonomy
*
* @since 1.0.0
*/
public function output_taxonomy_filter( $post_taxonomy )
{
// Only show taxonomy filter when the taxonomy is hierarchical
if ( true === $post_taxonomy->hierarchical ) {
wp_dropdown_categories( array(
'show_option_all' => sprintf( 'All %s', $post_taxonomy->label ),
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'hide_if_empty' => true,
'selected' => filter_input( INPUT_GET, $post_taxonomy->query_var, FILTER_SANITIZE_STRING ),
'hierarchical' => true,
'name' => $post_taxonomy->query_var,
'taxonomy' => $post_taxonomy->name,
'value_field' => 'slug',
) );
}
}
}