Add Featured Images to RSS Feeds

Extend your site's RSS feeds by including featured images in the feed.

Use Snippet
php
<?php
/**
 * 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' );
WPCode Team
600+

Disable Login Screen Language Switcher

Hide the Language Switcher on the default WordPress login screen.

Use Snippet
php
<?php
add_filter( 'login_display_language_dropdown', '__return_false' );
WPCode Team
500+

Remove Dashboard Welcome Panel

Hide the Welcome Panel on the WordPress dashboard for all users.

Use Snippet
php
<?php
add_action(
	'admin_init',
	function () {
		remove_action( 'welcome_panel', 'wp_welcome_panel' );
	}
);
WPCode Team
500+

Display the Last Updated Date

Add the last updated date & time to your posts in the frontend.

Use Snippet
php
<?php
$u_time          = get_the_time( 'U' );
$u_modified_time = get_the_modified_time( 'U' );
// Only display modified date if 24hrs have passed since the post was published.
if ( $u_modified_time >= $u_time + 86400 ) {
	$updated_date = get_the_modified_time( 'F jS, Y' );
	$updated_time = get_the_modified_time( 'h:i a' );
	$updated = '<p class="last-updated">';
	$updated .= sprintf(
	// Translators: Placeholders get replaced with the date and time when the post was modified.
		esc_html__( 'Last updated on %1$s at %2$s' ),
		$updated_date,
		$updated_time
	);
	$updated .= '</p>';
	echo wp_kses_post( $updated );
}
WPCode Team
500+

Disable Search

Completely disable search on your WordPress website.

Use Snippet
php
<?php
// Prevent search queries.
add_action(
	'parse_query',
	function ( $query, $error = true ) {
		if ( is_search() && ! is_admin() ) {
			$query->is_search       = false;
			$query->query_vars['s'] = false;
			$query->query['s']      = false;
			if ( true === $error ) {
				$query->is_404 = true;
			}
		}
	},
	15,
	2
);
// Remove the Search Widget.
add_action(
	'widgets_init',
	function () {
		unregister_widget( 'WP_Widget_Search' );
	}
);
// Remove the search form.
add_filter( 'get_search_form', '__return_empty_string', 999 );
// Remove the core search block.
add_action(
	'init',
	function () {
		if ( ! function_exists( 'unregister_block_type' ) || ! class_exists( 'WP_Block_Type_Registry' ) ) {
			return;
		}
		$block = 'core/search';
		if ( WP_Block_Type_Registry::get_instance()->is_registered( $block ) ) {
			unregister_block_type( $block );
		}
	}
);
// Remove admin bar menu search box.
add_action(
	'admin_bar_menu',
	function ( $wp_admin_bar ) {
		$wp_admin_bar->remove_menu( 'search' );
	},
	11
);
WPCode Team
400+

Add the Page Slug to Body Class

Add the page slug to the body class for better styling.

Use Snippet
php
<?php
function wpcode_snippet_add_slug_body_class( $classes ) {
	global $post;
	if ( isset( $post ) ) {
		$classes[] = $post->post_type . '-' . $post->post_name;
	}
	return $classes;
}
add_filter( 'body_class', 'wpcode_snippet_add_slug_body_class' );
WPCode Team
300+

Automatically Link Featured Images to Posts

Wrap featured images in your theme in links to posts.

Use Snippet
php
<?php
/**
 * Wrap the thumbnail in a link to the post.
 * Only use this if your theme doesn't already wrap thumbnails in a link.
 *
 * @param string $html The thumbnail HTML to wrap in an anchor.
 * @param int    $post_id The post ID.
 * @param int    $post_image_id The image id.
 *
 * @return string
 */
function wpcode_snippet_autolink_featured_images( $html, $post_id, $post_image_id ) {
	$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
	return $html;
}
add_filter( 'post_thumbnail_html', 'wpcode_snippet_autolink_featured_images', 20, 3 );
WPCode Team
300+

Add an Edit Post Link to Archives

Make it easier to edit posts when viewing archives. Or on single pages. If you...

Use Snippet
php
<?php
edit_post_link( __( '{Edit}' ) );
WPCode Team
300+

Disable Login by Email

Force your users to login only using their username.

Use Snippet
php
<?php
remove_filter( 'authenticate', 'wp_authenticate_email_password', 20 );
WPCode Team
200+

Change Excerpt Length

Update the length of the Excerpts on your website using this snippet.

Use Snippet
php
<?php
add_filter(
	'excerpt_length',
	function ( $length ) {
		// Number of words to display in the excerpt.
		return 40;
	},
	500
);
WPCode Team
200+

Completely Disable Comments

Disable comments for all post types, in the admin and the frontend.

Use Snippet
php
<?php
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('init', function () {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
});
WPCode Team
200+

Change Read More Text for Excerpts

Customize the "Read More" text that shows up after excerpts.

Use Snippet
php
<?php
function wpcode_snippets_change_read_more( $read_more, $read_more_text ) {
	// Edit the line below to add your own "Read More" text.
	$custom_text = 'Read the whole post';
	$read_more = str_replace( $read_more_text, $custom_text, $read_more );
	return $read_more;
}
add_filter( 'the_content_more_link', 'wpcode_snippets_change_read_more', 15, 2 );
WPCode Team
200+

Hide ‘Screen Options’ Tab

Remove the Screen Options menu at the top of admin pages.

Use Snippet
php
<?php
// Hide admin 'Screen Options' tab
add_filter('screen_options_show_screen', '__return_false');
WPCode Team
100+

Set a Minimum Word Count for Posts

Force your authors to write posts that have a minimum length.

Use Snippet
php
<?php
/**
 * Prevent publishing posts under a minimum number of words.
 *
 * @param int     $post_id The id of the post.
 * @param WP_Post $post The post object.
 *
 * @return void
 */
function wpcode_snippet_publish_min_words( $post_id, $post ) {
	// Edit the line below to set the desired minimum number of words.
	$word_count = 100;
	if ( str_word_count( $post->post_content ) < $word_count ) {
		wp_die(
			sprintf(
				// Translators: placeholder adds the minimum number of words.
				esc_html__( 'The post content is below the minimum word count. Your post needs to have at least %d words to be published.' ),
				absint( $word_count )
			)
		);
	}
}
add_action( 'publish_post', 'wpcode_snippet_publish_min_words', 9, 2 );
WPCode Team
90+

Exclude Specific Categories from RSS Feed

Prevent posts in certain categories from showing up in your RSS Feeds.

Use Snippet
php
<?php
/**
 * Exclude a category or multiple categories from the feeds.
 * If you want to exclude multiple categories, use a comma-separated list: "-15, -5, -6".
 * Make sure to prefix the category id(s) with a minus "-".
 *
 * @param WP_Query $query The query.
 */
function wpcode_snippets_exclude_feed_category( $query ) {
	if ( $query->is_feed ) {
		// Replace 15 with the desired category id you want to exclude.
		$query->set( 'cat', '-15' );
	}
}
add_action( 'pre_get_posts', 'wpcode_snippets_exclude_feed_category' );
WPCode Team
90+
1 2 3 4