search ]

Archives: Snippets | Page 17

Style and Customize the Tag Cloud in WordPress

To change the appearance of the tag cloud, use the following snippet. A full reference of the options is available at this link:

// Tag cloud custom
function style_tags($args) {
    $args = array(
         'largest'    => '10',
         'smallest'   => '10',
         'format'     => 'list',
         );
    return $args;
}
add_filter('widget_tag_cloud_args', 'style_tags');

For more on tags and taxonomies, see Taxonomies in WordPress.

Restrict Admin Menu Items by Username in WordPress

You can hide specific admin menu items from a specific user in the WordPress dashboard. Replace username with the login of the user you want to hide those menu items from:

function remove_menus()
{
    global $menu;
    global $current_user;
    get_currentuserinfo();

    if($current_user->user_login == 'username')
    {
        $restricted = array(__('Posts'),
                            __('Media'),
                            __('Links'),
                            __('Pages'),
                            __('Comments'),
                            __('Appearance'),
                            __('Plugins'),
                            __('Users'),
                            __('Tools'),
                            __('Settings')
        );
        end ($menu);
        while (prev($menu)) {
            $value = explode(' ',$menu[key($menu)][0]);
            if(in_array($value[0] != NULL ? $value[0] : "" , $restricted)) {
                unset($menu[key($menu)]);
            }
        } // end while

    } // end if
}
add_action('admin_menu', 'remove_menus');

This uses a WordPress hook. Learn more in WordPress Hooks Explained.

Remove Dashboard Meta Boxes in WordPress

The following snippet lets you remove specific meta boxes from the main Dashboard page in WordPress. These are the default meta boxes shown on the main admin screen.

To remove or change which boxes are displayed, add this snippet to your theme’s functions.php file and choose which boxes to remove:

function my_custom_dashboard_widgets()
{
    global $wp_meta_boxes;

    // Main column (left):
    // Browser Update Required
    unset($wp_meta_boxes['dashboard']['normal']['high']['dashboard_browser_nag']);
    // PHP Update Required
    unset($wp_meta_boxes['dashboard']['normal']['high']['dashboard_php_nag']);
    // Right Now - Comments, Posts, Pages at a glance
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
    // Right Now
    unset($wp_meta_boxes['dashboard']['normal']['core']['network_dashboard_right_now']);
    // Activity
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
    // Site Health Status
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_site_health']);

    // Side Column (right):
    // WordPress Events and News
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    // Quick Draft, Your Recent Drafts
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);

    // Recent Comments
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);

    // Incoming Links
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);

    // Plugins - Popular, New and Recently updated WordPress Plugins
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);

    // Other WordPress News Feed
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);


    // Recent Drafts List
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
}

add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');

This is the page we are talking about:

Removing default meta boxes from the main Dashboard page

For safe admin customizations, see What Are Child Themes and How to Use Them.

Display All Post Types in Category Archives

If you want WordPress category archive pages to display all posts regardless of post type, add the following code to your functions.php file:

function any_ptype_on_cat($request) {

    if ( isset($request['category_name']) )
        $request['post_type'] = 'any';

    return $request;
}
add_filter('request', 'any_ptype_on_cat');

For more on custom post types, see How to Create Custom Post Types.

Unregister Default WordPress Widgets

To remove the widgets that come by default with WordPress, add the following code to your theme’s functions.php file:

// Unregister all default WordPress Widgets
function unregister_default_wp_widgets() {
    unregister_widget('WP_Widget_Pages');
    unregister_widget('WP_Widget_Calendar');
    unregister_widget('WP_Widget_Archives');
    unregister_widget('WP_Widget_Links');
    unregister_widget('WP_Widget_Meta');
    unregister_widget('WP_Widget_Search');
    unregister_widget('WP_Widget_Text');
    unregister_widget('WP_Widget_Categories');
    unregister_widget('WP_Widget_Recent_Posts');
    unregister_widget('WP_Widget_Recent_Comments');
    unregister_widget('WP_Widget_RSS');
    unregister_widget('WP_Widget_Tag_Cloud');
}
add_action('widgets_init', 'unregister_default_wp_widgets', 1);

This uses the widgets_init action. Learn more in WordPress Hooks Explained.

Add Featured Image Column to Posts List in WordPress Admin

Want to know how to display the featured image next to each post and page when viewing the posts or pages list in the WordPress admin?

You can use a plugin for this, but it is unnecessary. Simply add the following code to your theme’s functions.php file.

function posts_columns($defaults){
    $defaults['sv_post_thumbs'] = __('Image');
    return $defaults;
}

function posts_custom_columns($column_name, $id){
    if($column_name === 'sv_post_thumbs'){
        the_post_thumbnail( 'thumbnail' );
    }
}

add_filter('manage_posts_columns', 'posts_columns', 5);
add_action('manage_posts_custom_column', 'posts_custom_columns', 5, 2);

The result will look something like this:

Adding featured image to posts column in WordPress admin

This uses WordPress hooks to add admin columns. Learn more in WordPress Hooks Explained.

Upgrade and Downgrade WordPress with WP-CLI

WP-CLI is a set of tools that provides functionality for managing WordPress sites. In this guide we describe the benefits of using WP-CLI and demonstrate several advanced commands that will make your life easier in a WordPress development environment. You can use wp-cli to upgrade or downgrade WordPress to a specific version.

Update to the latest WordPress version:

wp core update

Update to a specific WordPress version:

wp core update --version=5.5.3

Install or downgrade to a specific WordPress version:

wp core update --version=5.5.3 --force

Check the installed WordPress version:

wp core version

Or with extra info:

wp core version --extra

Multiple Excerpt Lengths in WordPress

You may or may not know that you can change or set the excerpt length in WordPress using the following filter:

function sv_excerpt_length( $length ) {
    return 15;
}
add_filter( 'excerpt_length', 'sv_excerpt_length' );

But that sets one excerpt length every time you use the the_excerpt function. What if you want different excerpt lengths in different places in your theme? In that case, you can use the following code in functions.php:

function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);

      if (count($excerpt) >= $limit) {
          array_pop($excerpt);
          $excerpt = implode(" ", $excerpt) . '...';
      } else {
          $excerpt = implode(" ", $excerpt);
      }

      $excerpt = preg_replace('`[[^]]*]`', '', $excerpt);

      return $excerpt;
}

Then you can set the excerpt length in your theme like this:

<?php echo excerpt(25); ?>
Savvy WordPress Development official logo