Static code is often lighter and more secure than plugins, delivering the same functionality without the extra bloat. Below are some of my most commonly used code snippets for WordPress sites. I update this list often so don’t be surprised if the next time you visit, I’ll have 100s more code snippets for you to choose from – all free, just copy/paste.
Manage This Page dropdown
Allows admins to delete pages/posts from the frontend.
function add_delete_dropdown_toolbar($wp_admin_bar) {
// Ensure only admins (or users with 'manage_options' capability) can see this
if (current_user_can('manage_options') && is_singular()) {
$post_id = get_the_ID();
$delete_link = get_delete_post_link($post_id);
// Add the dropdown parent
$wp_admin_bar->add_node(array(
'id' => 'manage-page',
'title' => __('Manage This Page'),
));
// Add the delete option
$wp_admin_bar->add_node(array(
'id' => 'delete-page',
'parent' => 'manage-page',
'title' => __('Delete This Page'),
'href' => $delete_link,
));
}
}
add_action('admin_bar_menu', 'add_delete_dropdown_toolbar', 100);
Display Random Posts
Show random posts anywhere on your site.
$number_of_posts = 5; // Change this to the number of random posts you want to display.
$title = 'You might also like:'; // This is the title that will be displayed above the list of posts.
$current_post_id = get_the_ID();
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => $number_of_posts,
'post__not_in' => array( $current_post_id ),
);
$random_posts = new WP_Query( $args );
if ( $random_posts->have_posts() ) {
echo '<h3>' . esc_html( $title ) . '</h3>';
echo '<ul>';
while ( $random_posts->have_posts() ) {
$random_posts->the_post();
echo '<li><a href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a></li>';
}
echo '</ul>';
}
wp_reset_postdata();
Allow Contributors to Upload Images
Allow users with the Contributor role to upload files in the admin.
add_filter( 'user_has_cap', function ( $allcaps, $caps, $args, $user ) {
if ( in_array( 'contributor', $user->roles ) && empty( $caps['upload_files'] ) ) {
$allcaps['upload_files'] = true;
}
return $allcaps;
}, 10, 4 );
Disable Post Formats
Disable the Post Formats feature for all posts.
add_action( 'after_setup_theme', function() {
remove_theme_support( 'post-formats' );
}, 11 );
Remove WordPress Version Number
Hide the WordPress version number from your site’s frontend and feeds.
add_filter('the_generator', '__return_empty_string');
Disable Gutenberg Editor
Switch back to the Classic Editor by disabling the Block Editor.
add_filter('gutenberg_can_edit_post', '__return_false', 5);
add_filter('use_block_editor_for_post', '__return_false', 5);
Disable Widget Blocks
Use the classic interface instead of Blocks to manage Widgets.
add_filter( 'use_widgets_block_editor', '__return_false' );
Force new tab to open for external links
function add_target_blank_to_external_links() {
?>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
var links = document.querySelectorAll('a[href^="http"]:not([href*="' + location.hostname + '"])');
links.forEach(function(link) {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
});
});
</script>
<?php
}
add_action('wp_footer', 'add_target_blank_to_external_links');
Hide Login Errors in WordPress
Improve safety by hiding the specific login error information.
add_filter(
'login_errors',
function ( $error ) {
// Edit the line below to customize the message.
return 'Something is wrong!';
}
);
Disable XML-RPC
On sites running WordPress 3.5+, disable XML-RPC completely.
add_filter( 'xmlrpc_enabled', '__return_false' );
Disable WordPress REST API
Easily disable the WP REST API on your website with this snippet.
add_filter(
'rest_authentication_errors',
function ( $access ) {
return new WP_Error(
'rest_disabled',
__( 'The WordPress REST API has been disabled.' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
);
Disable Emojis
Disable Emoji’s in WordPress to improve your site’s performance
/**
* Disable the emojis in WordPress.
*/
add_action( 'init', function () {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
// Remove from TinyMCE.
add_filter( 'tiny_mce_plugins', function ( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
} );
// Remove from dns-prefetch.
add_filter( 'wp_resource_hints', function ( $urls, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
$urls = array_diff( $urls, array( $emoji_svg_url ) );
}
return $urls;
}, 10, 2 );
} );
Completely Disable Comments
Disable comments for all post types, in the admin and the frontend.
add_action('admin_init', function () {
// Redirect any user trying to access comments page
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_safe_redirect(admin_url());
exit;
}
// Remove comments metabox from dashboard
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
// Disable support for comments and trackbacks in post types
foreach (get_post_types() as $post_type) {
if (post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
});
// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
// Remove comments page in menu
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
});
// Remove comments links from admin bar
add_action('admin_bar_menu', function () {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}, 0);
Remove WordPress Version Number
Hide the WordPress version number from your site’s frontend and feeds.
add_filter('the_generator', '__return_empty_string');
Disable Automatic Updates Emails
Stop getting emails about automatic updates on your WordPress site.
// Disable auto-update emails.
add_filter( 'auto_core_update_send_email', '__return_false' );
// Disable auto-update emails for plugins.
add_filter( 'auto_plugin_update_send_email', '__return_false' );
// Disable auto-update emails for themes.
add_filter( 'auto_theme_update_send_email', '__return_false' );
Disable XML-RPC
On sites running WordPress 3.5+, disable XML-RPC completely.
add_filter( 'xmlrpc_enabled', '__return_false' );
Disable The WP Admin Bar
Hide the WordPress Admin Bar for all users in the frontend.
add_filter( 'show_admin_bar', '__return_false' );
Disable Automatic Updates
Use this snippet to completely disable automatic updates on your website.
// Disable core auto-updates
add_filter( 'auto_update_core', '__return_false' );
// Disable auto-updates for plugins.
add_filter( 'auto_update_plugin', '__return_false' );
// Disable auto-updates for themes.
add_filter( 'auto_update_theme', '__return_false' );
Enable Shortcode Execution in Text Widgets
Extend the default Text Widget with shortcode execution.
add_filter( 'widget_text', 'do_shortcode' );
Disable WordPress REST API
Easily disable the WP REST API on your website with this snippet.
add_filter(
'rest_authentication_errors',
function ( $access ) {
return new WP_Error(
'rest_disabled',
__( 'The WordPress REST API has been disabled.' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
);
Disable RSS Feeds
Turn off the WordPress RSS Feeds for your website with 1 click.
/**
* Display a custom message instead of the RSS Feeds.
*
* @return void
*/
function wpcode_snippet_disable_feed() {
wp_die(
sprintf(
// Translators: Placeholders for the homepage link.
esc_html__( 'No feed available, please visit our %1$shomepage%2$s!' ),
' <a href="' . esc_url( home_url( '/' ) ) . '">',
'</a>'
)
);
}
// Replace all feeds with the message above.
add_action( 'do_feed_rdf', 'wpcode_snippet_disable_feed', 1 );
add_action( 'do_feed_rss', 'wpcode_snippet_disable_feed', 1 );
add_action( 'do_feed_rss2', 'wpcode_snippet_disable_feed', 1 );
add_action( 'do_feed_atom', 'wpcode_snippet_disable_feed', 1 );
add_action( 'do_feed_rss2_comments', 'wpcode_snippet_disable_feed', 1 );
add_action( 'do_feed_atom_comments', 'wpcode_snippet_disable_feed', 1 );
// Remove links to feed from the header.
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );
Add Featured Images to RSS Feeds
Extend your site’s RSS feeds by including featured images in the feed.
/**
* Add the post thumbnail, if available, before the content in feeds.
*
* @param string $content The post content.
*
* @return string
*/
function wpcode_snippet_rss_post_thumbnail( $content ) {
global $post;
if ( has_post_thumbnail( $post->ID ) ) {
$content = '<p>' . get_the_post_thumbnail( $post->ID ) . '</p>' . $content;
}
return $content;
}
add_filter( 'the_excerpt_rss', 'wpcode_snippet_rss_post_thumbnail' );
add_filter( 'the_content_feed', 'wpcode_snippet_rss_post_thumbnail' );
Remove Dashboard Welcome Panel
Hide the Welcome Panel on the WordPress dashboard for all users.
add_action(
'admin_init',
function () {
remove_action( 'welcome_panel', 'wp_welcome_panel' );
}
);
Disable Posts Post Type
This snippet hides the default Posts post type from the WordPress admin
// Remove side menu
add_action( 'admin_menu', function () {
remove_menu_page( 'edit.php' );
} );
// Remove +New post in top Admin Menu Bar
add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'new-post' );
}, 999 );
// Remove Quick Draft Dashboard Widget
add_action( 'wp_dashboard_setup', function () {
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
}, 999 );