10+ Useful WordPress Code Snippets

Aug 25, 2013Blog, Web design, WordPress

We have collected a useful list of 10+ WordPress Code Snippets that we believe you will find really useful especially if you develop sites like we do. These snippets are added directly to a themes functions.php file in order to add additional functionality to your site.

It’s being more than decade for us now to use WordPress and within all these years we worked on number of WordPress projects. While working on all these projects, we have been able to build up a collection of little snippets and tricks that helps us from time to time.

10+ Useful WordPress Code Snippets

All the snippets we’ve been adding have been tested with the WordPress 3.6 version. Based on all the experimentation’s we did, you will find the collection of WordPress Code Snippets, that we used for our WordPress projects.

Remove Comments menu from Admin Dashboard

// Remove Comments menu from Admin Dashboard
function remove_menus () {
global $menu;
$restricted = array(__('Comments'));
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}
}
add_action('admin_menu', 'remove_menus');

Source

Remove Dashboard Widgets

// Remove Dashboard Widgets
function remove_dashboard_widgets() {
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}
if (!current_user_can('manage_options')) {
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
}

Source

Remove Metaboxes from Custom Post Type Edit Screens

// Remove Metaboxes from Custom Post Type Edit Screens
if (is_admin()) :
function remove_post_meta_boxes() {
if(!current_user_can('administrator')) {
remove_meta_box('tagsdiv-post_tag', 'custom_post_type', 'normal');
remove_meta_box('categorydiv', 'custom_post_type', 'normal');
remove_meta_box('authordiv', 'custom_post_type', 'normal');
remove_meta_box('postexcerpt', 'custom_post_type', 'normal');
remove_meta_box('trackbacksdiv', 'custom_post_type', 'normal');
remove_meta_box('commentstatusdiv', 'custom_post_type', 'normal');
remove_meta_box('postcustom', 'custom_post_type', 'normal');
remove_meta_box('commentstatusdiv', 'custom_post_type', 'normal');
remove_meta_box('commentsdiv', 'custom_post_type', 'normal');
remove_meta_box('revisionsdiv', 'custom_post_type', 'normal');
remove_meta_box('authordiv', 'custom_post_type', 'normal');
remove_meta_box('pageparentdiv', 'custom_post_type', 'normal');
remove_meta_box('slugdiv', 'custom_post_type', 'normal');
}
}
add_action('admin_menu', 'remove_post_meta_boxes');
endif;

Source

Remove Featured Image Metabox from Custom Post Type Edit Screens

// Remove Featured Image Metabox from Custom Post Type Edit Screens
function remove_image_box() {
if ($current_user->user_level < 10){
remove_meta_box('postimagediv','custom_post_type','side');
}
}
add_action('do_meta_boxes', 'remove_image_box');

Source

Remove Dynamic Widget Metabox from Custom Post Type Edit Screens

// Remove Dynamic Widget Metabox from Custom Post Type Edit Screens
function remove_dynwid_box() {
if ($current_user->user_level < 10){
remove_meta_box('dynwid','custom_post_type','side');
}
}
add_action('do_meta_boxes', 'remove_dynwid_box');

Source

Remove editor only for author based on Custom Post Types

// Remove editor only for author based on Custom Post Types
function remove_editor() {
if (current_user_can('author')){
remove_post_type_support('custom_post_type','editor');
}
}
add_action('admin_init','remove_editor');

Source

Remove Edit Title, Add new, Permalink, View Post buttons from Custom Post Type Edit Screens

// Remove Edit Title, Add new, Permalink, View Post buttons from Custom Post Type Edit Screens
function posttype_admin_css() {
if (current_user_can('author')){
global $post_type;
if($post_type == 'custom_post_type') {
echo '<style type="text/css">#titlediv,.add-new-h2,#edit-slug-box,#view-post-btn,.updated p a{display: none;}</style>';
}
}
}
add_action('admin_head', 'posttype_admin_css');

Source

Remove Items from admin bar for authors

// Remove Items from admin bar for authors
function remove_admin_bar_links() {
if (current_user_can('author')){
global $wp_admin_bar;
$wp_admin_bar->remove_menu('new-content'); // Remove Add New
$wp_admin_bar->remove_menu('comments'); // Remove Comments
$wp_admin_bar->remove_menu('wpseo-menu'); // Remove Yoast SEO links menu
$wp_admin_bar->remove_menu('documentation'); // Remove the WP documentation link
$wp_admin_bar->remove_menu('support-forums'); // Remove the support forums link
$wp_admin_bar->remove_menu('feedback'); // Remove the feedback link
}
}
add_action('wp_before_admin_bar_render', 'remove_admin_bar_links');

Source

Remove Posts menu for Editor, Author, and Contributor

// Remove Posts menu for Editor, Author, and Contributor
if (! current_user_can('manage_options')) {
add_action('admin_menu', 'notadmin_remove_menus', 999);
function notadmin_remove_menus() {
remove_menu_page('edit.php'); // Posts
remove_menu_page('tools.php'); // Tools
remove_submenu_page('edit.php?post_type=custom_post_type', 'post-new.php?post_type=custom_post_type'); // Custom Post Type
}
}

Source

Add new dashboard widget

// Add new dashboard widget
function new_dash_widget_function() { ?>
<p>Some text or Html.</p>
<?php }
function add_dashboard_widgets() {
wp_add_dashboard_widget('new_dash_widget', 'Your_Widget_name', 'new_dash_widget_function');
}
add_action('wp_dashboard_setup', 'add_dashboard_widgets');

Source

Trim AJAX Calendar Future POSTS to be valid

// Trim AJAX Calendar Future POSTS to be valid
add_filter('get_calendar', 'customize_calendar_output');
function customize_calendar_output($calendar_output) {
$calendar_output = str_replace('<table id="wp-calendar" summary="' . esc_attr__('Calendar') . '">', '<table id="wp-calendar">', $calendar_output);
return $calendar_output;
}

Source

Trim posts with status future like post status publish

// Trim posts with status future like post status publish
function custom_archive_query($query) {
if ( $query->is_archive() )
{
$query->set('post_status', array('future', 'publish'));
}
return $query;
}
add_filter('pre_get_posts', 'custom_archive_query');

Source


* Remember to backup your template files and/or database before performing any edits. Take care!!! :)

David

Graphic and website designer, currently maintaining web servers for existing clients and building websites for new clients. In my free time, I learn about Linux, SSH, and WP-CLI and I’m preparing this blog.

Related Posts

25+ Free Fonts for Your Next Design Project

25+ Free Fonts for Your Next Design Project

Looking for some fresh and creative fonts to spice up your design projects in 2023? You’re in luck! In this blog post, we’ve curated a list of 25 free fonts that you can download and use right away. These fonts are suitable for various purposes such as logos,...

Adobe Photoshop Shortcuts – Infographic

Adobe Photoshop Shortcuts – Infographic

Thanks to Amanda from the Consumer Base that she sent us an ultimate Adobe Photoshop shortcuts cheat sheet. If you are a beginner or want to brush up your knowledge on Adobe Photoshop shortcuts then you are at the right place. Have look at it and let us your thoughts...

20 WordPress Tutorial channels on Youtube

20 WordPress Tutorial channels on Youtube

This post will introduce you a useful list of the 20+ best currently active WordPress tutorial related channels on Youtube. These channels are mostly focused on Tutorials and Guides for Developers and Designers that want to learn all about WordPress. Check them out...

12 AI Tools for Graphic and Website Designers

12 AI Tools for Graphic and Website Designers

Artificial intelligence (AI) is transforming the way designers work, from ideation to execution. AI tools can help designers automate tedious tasks, generate new content, and enhance their creativity. In this blog post, I will introduce AI tools that you can use to...

How to Pair Fonts – Tips and Tools for Perfect Font Combinations

How to Pair Fonts – Tips and Tools for Perfect Font Combinations

Font pairing is the art of choosing two or more fonts that work well together and create a harmonious design. Font pairing can make or break your website, logo, poster, or any other project that involves typography. But how do you pair fonts like a pro? In this...

The Ultimate Guide to Color Palettes

The Ultimate Guide to Color Palettes

Color palettes are collections of colors that work well together and create a harmonious visual effect. They are essential for any design and art project, as they help to convey the mood, message, and style of your work. But how do you choose the right color palette...

Show/Hide Comments (0 comments)
L

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social followers

Articles

Visits

Pin It on Pinterest

Shares