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.
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.
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:
%22%20transform%3D%22translate(2.9%202.9)%20scale(5.71094)%22%20fill-opacity%3D%22.5%22%3E%3Cpath%20fill%3D%22%23141414%22%20d%3D%22M259.1-27.4l3.8%20216-44%20.8-3.8-216z%22%2F%3E%3Cellipse%20fill%3D%22%23fff%22%20rx%3D%221%22%20ry%3D%221%22%20transform%3D%22rotate(62.8%20-69%20126.6)%20scale(107.27896%20131.31111)%22%2F%3E%3Cellipse%20fill%3D%22%23b0b0b0%22%20rx%3D%221%22%20ry%3D%221%22%20transform%3D%22matrix(5.81048%20184.89241%20-13.62194%20.42809%20213%2045.5)%22%2F%3E%3Cellipse%20fill%3D%22%23fff%22%20cx%3D%2273%22%20cy%3D%22155%22%20rx%3D%22114%22%20ry%3D%22114%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E)
For safe admin customizations, see What Are Child Themes and How to Use Them.
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.
To display which template file a page uses in the header, add the following code to your functions.php file:
add_action('wp_head', 'show_template');
function show_template() {
global $template;
print_r($template);
}
For more on template files, see WordPress Template Hierarchy.
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.
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:
%22%20transform%3D%22translate(1.8%201.8)%20scale(3.57422)%22%20fill-opacity%3D%22.5%22%3E%3Cellipse%20fill%3D%22%23c6c6c6%22%20cx%3D%2267%22%20cy%3D%2293%22%20rx%3D%2243%22%20ry%3D%22234%22%2F%3E%3Cellipse%20fill%3D%22%23a8a8a8%22%20rx%3D%221%22%20ry%3D%221%22%20transform%3D%22rotate(46.4%20-120.7%20145.3)%20scale(25.80892%2024.00646)%22%2F%3E%3Cellipse%20fill%3D%22%23fff%22%20rx%3D%221%22%20ry%3D%221%22%20transform%3D%22matrix(-63.3357%2043.34615%20-66.35241%20-96.95155%20196.1%2094.6)%22%2F%3E%3Cellipse%20fill%3D%22%23fff%22%20rx%3D%221%22%20ry%3D%221%22%20transform%3D%22matrix(-22.6948%20-2.6348%2014.86208%20-128.01413%200%2082.4)%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E)
This uses WordPress hooks to add admin columns. Learn more in WordPress Hooks Explained.
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
When registering a taxonomy (register_taxonomy), you can simply use an array like this:
register_taxonomy( 'artist', array( 'profile', 'cd' ), $args );
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); ?>