Archives: Snippets | Page 13

Show Post Thumbnails in RSS Feed

If you want to display the post featured image in the RSS feed (essential if you use RSS for Mailchimp for example), add the following code to your functions.php file:

// Put post thumbnails into rss feed
function savvy_feed_post_thumbnail($content) {
    global $post;
    if(has_post_thumbnail($post->ID)) {
        $content = '' . $content;
    }
    return $content;
}
add_filter('the_excerpt_rss', 'savvy_feed_post_thumbnail');
add_filter('the_content_feed', 'savvy_feed_post_thumbnail');

Change Payment Icons on WooCommerce Checkout

Here is an example of how to change the default PayPal icon on the WooCommerce checkout page. Live example:

<?php
/**
 * Add custom gateway icons
 * 
 * @param  string $icon       Image HTML.
 * @param  string $gateway_id Gateway ID.
 * 
 * @return string
 */
function custom_wc_gateway_icons( $icon, $gateway_id ) {
	// Example for PayPal:
	if ( 'paypal' == $gateway_id ) {
		$icon = '<img src="' . WC_HTTPS::force_https_url( 'http://your-site.com/image.png' ) . '" alt="' . __( 'PayPal' ) . '" />'
	}
	return $icon;
}
add_filter( 'woocommerce_gateway_icon', 'custom_wc_gateway_icons', 10, 2 );

For a complete WooCommerce setup guide, see Create a Virtual Store with WooCommerce.

Change Number of Items on WooCommerce Orders Page

To change the number of items on the WooCommerce orders page (on the front end), use the following code where X is the maximum number of items to display (functions.php):

function custom_my_account_orders( $args ) {
    $args['posts_per_page'] = X;
    return $args;
}
add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_account_orders', 10, 1 );

More on managing and handling orders in WooCommerce sites in the post Managing orders in WooCommerce.

Show WooCommerce Checkout When Cart Is Empty

By default, there is an automatic redirect from the WooCommerce checkout page to the (empty) cart page when there are no products in the cart.

Try accessing the checkout page (link in footer) at this sample store and you will see the redirect.

There are uncommon situations where you may want to disable this redirect and show the user the checkout page anyway. You can do this by adding the following code to your functions.php file:

add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' );
add_filter( 'woocommerce_checkout_update_order_review_expired', '__return_false' );

Disable Yoast SEO Schema and Structured Data

Yoast SEO adds a JSON-LD schema graph to every page, built from your plugin settings. To disable Yoast schema output completely, return false on the wpseo_json_ld_output filter. Recent Yoast versions also have a settings toggle that does the same thing.

You’d do this when a theme, a dedicated schema plugin or hand-written JSON-LD already covers your structured data, and Yoast’s graph creates duplicates.

If you only want to drop the breadcrumb markup, see remove Yoast breadcrumbs schema. To change the trail itself, see add a link to Yoast SEO breadcrumbs.

The Code

Add this line to your child theme’s functions.php file or to a custom plugin:

add_filter( 'wpseo_json_ld_output', '__return_false' );

How It Works

Yoast applies wpseo_json_ld_output right before it prints the <script type="application/ld+json" class="yoast-schema-graph"> block. If the filter returns false or an empty array, Yoast prints nothing.

That removes the entire graph: Organization or Person, WebSite, WebPage, Article, BreadcrumbList, and the schema from Yoast’s FAQ and How-to blocks. Titles, meta descriptions and Open Graph tags are separate outputs and stay in place.

A named function that returns an empty array works too, since Yoast treats false and [] the same way. __return_false is just shorter.

To disable schema only on certain pages, return false conditionally and pass the original value through everywhere else:

add_filter( 'wpseo_json_ld_output', function( $data ) {
	if ( is_page( 'contact' ) ) {
		return false;
	}
	return $data;
} );

Disable Yoast Schema Without Code

Yoast SEO 26.8 added a Schema settings page. Go to Yoast SEO > Settings > Advanced > Schema and switch off Enable Schema Framework. On the front end, that adds the same __return_false filter shown above.

Yoast strongly recommends keeping the toggle on. Only turn it off when another tool fully replaces the graph, because without structured data the way your pages appear in search results can change.

If the toggle shows as off and switching it on gives the message “Yoast Schema Framework can’t be enabled”, some code already returns false or an empty array on wpseo_json_ld_output.

It may be your theme, a plugin or an old snippet. To find it, search your wp-content folder:

grep -rn "wpseo_json_ld_output" wp-content/

Remove that filter, or make it conditional, and the toggle works again.

Remove Only Part of the Schema

Many “Yoast broke my schema” problems are really duplicates: another plugin also outputs an Article or BreadcrumbList. You can drop just the overlapping piece and keep the rest of the graph.

Every piece has a wpseo_schema_needs_<identifier> filter. The identifiers are article, webpage, main_image, breadcrumb, website, organization, person, author, faq and howto.

// Keep Yoast's graph but drop its Article piece.
add_filter( 'wpseo_schema_needs_article', '__return_false' );

Other pieces may still reference the one you removed by @id. Check the output in the Rich Results Test, and for breadcrumbs use the full snippet on the breadcrumbs page linked above.

For more ways to adjust Yoast’s output, see Yoast SEO filters and the Yoast Schema API docs.

FAQs

Common questions about disabling Yoast SEO schema:

Does disabling Yoast schema affect my SEO titles and meta descriptions?
No. The wpseo_json_ld_output filter and the Schema toggle only remove the JSON-LD block. Titles, meta descriptions, canonicals, robots and social tags are output separately and keep working.
Do I need to uninstall Yoast SEO to get rid of its schema?
No, the filter or toggle is enough. If you do uninstall the plugin, note that deleting it leaves its data (the wp_yoast_ tables, options and post meta) in the database, so reinstalling restores your settings.
Is the Wincher integration related to Yoast schema?
No. Wincher is a keyphrase rank tracker and adds nothing to your structured data. To turn it off, go to Yoast SEO > Integrations and switch off the Wincher toggle.
I added the filter but the schema is still there. What's wrong?
Usually a page cache is serving the old HTML, so purge it and view the source again. If JSON-LD is still there, check its class: anything without yoast-schema-graph comes from another plugin or the theme.

Display Relative Dates in WordPress

Instead of displaying the exact date your post was published, you can display it relatively – “6 months ago” or “3 weeks ago”. Use the following code to do this:

# For posts & pages #
<?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?>

# For comments #
<?php echo human_time_diff(get_comment_time('U'), current_time('timestamp')) . ' ago'; ?>

For another useful date/time display, see Display Estimated Post Reading Time.

Display Total Post Count for a Custom Post Type

This short snippet lets you display the total number of posts for a specific Custom Post Type. Add the code where you want to display the post count and change POST-TYPE-NAME to the name of the post type whose count you want to show.

<?php
     // Get total number of posts in POST-TYPE-NAME
     $count_posts = wp_count_posts('POST-TYPE-NAME');
     $total_posts = $count_posts->publish;
     echo $total_posts;
?>

Change the Default Excerpt Length in WordPress

The following filter lets you set the excerpt length that WordPress displays when calling the the_excerpt function. In this example we set the excerpt length to 20 words (the default is 55):

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

The filter only affects excerpts that WordPress generates automatically from the post content. A manual excerpt entered in the editor is shown in full. The priority of 999 makes sure your value wins over a theme or plugin that sets its own length.

This uses the excerpt_length filter. Learn more in WordPress Hooks Explained.

Include Custom Post Types in WordPress Search Results

Custom Post Types (CPTs) are not included in WordPress search results by default. The following snippet lets you choose which custom post types to add to WordPress search results. Add this code to your theme’s functions.php file and change the post type names as needed:

function custom_post_type_search( $query ) {
     if ($query->is_search) {
          $query->set('post_type', array( 'post', 'myCustomPostType1', 'myCustomPostType2', 'myCustomPostType3'));
     }
     return $query;
}
add_filter( 'pre_get_posts', 'custom_post_type_search' );

If you want more advanced search options, take a look at the post “Improving search with Relevanssi“.