Archives: Snippets | Page 13

Add a Link to Yoast SEO Breadcrumbs

The wpseo_breadcrumb_links filter lets you add, remove or reorder items in the Yoast SEO breadcrumbs trail. A common case is adding a link to the blog right after the Home link on single posts, so the trail reads Home › Blog › Post title.

To remove the breadcrumb structured data instead, see remove Yoast breadcrumbs schema. To turn off all of Yoast’s schema, see disable Yoast SEO schema.

The Code

Add this to your child theme’s functions.php file or to a custom plugin. Change the URL and text to match your blog page:

function yoast_seo_breadcrumb_append_link( $links ) {
    if ( ! is_singular( 'post' ) ) {
        return $links;
    }

    $breadcrumb[] = array(
        'url'  => home_url( '/blog/' ),
        'text' => 'Blog',
    );

    array_splice( $links, 1, 0, $breadcrumb );

    return $links;
}
add_filter( 'wpseo_breadcrumb_links', 'yoast_seo_breadcrumb_append_link' );

How It Works

Yoast passes the whole trail to wpseo_breadcrumb_links as an array, just before it renders it. Index 0 is the Home link and the last item is the current page.

Each item is an array with url and text. Yoast adds an extra key that tells you what the item is: id for posts and pages, term_id and taxonomy for terms, and ptarchive for post type archives.

array_splice( $links, 1, 0, $breadcrumb ) inserts the new item at position 1, right after Home, without removing anything. The third argument must be 0. Any other value, such as -2, silently deletes items from longer trails.

The is_singular( 'post' ) check limits the change to blog posts. Without it, the Blog link would also show up on the home page, on regular pages and on archives.

The same array feeds both the visible trail and Yoast’s BreadcrumbList schema, so your new item appears in the structured data too.

Check the Built-in Setting First

If the homepage is a static page and a Posts page is set under Settings > Reading, you may not need code at all.

Under Yoast SEO > Settings > Advanced > Breadcrumbs, Show blog page in breadcrumbs adds that page to the trail on single posts.

Use the filter when there’s no Posts page (for example, a blog index built as a custom page template), or when you need a link the settings don’t offer.

More wpseo_breadcrumb_links Examples

This version adds a hub page before the current item on a custom post type. A negative offset counts from the end, so -1 puts it right before the last crumb:

add_filter( 'wpseo_breadcrumb_links', function( $links ) {
    if ( ! is_singular( 'snippet' ) ) {
        return $links;
    }

    $new_crumb = array(
        array(
            'url'  => home_url( '/resources/' ),
            'text' => 'Resources',
        ),
    );

    array_splice( $links, -1, 0, $new_crumb );

    return $links;
} );

This one removes category crumbs from the trail:

add_filter( 'wpseo_breadcrumb_links', function( $links ) {
    foreach ( $links as $i => $link ) {
        if ( isset( $link['taxonomy'] ) && 'category' === $link['taxonomy'] ) {
            unset( $links[ $i ] );
        }
    }
    return array_values( $links );
} );

A few rules keep the filter safe:

  • Always return an array. Anything else is ignored, and Yoast logs a “doing it wrong” notice.
  • Give every item both url and text. If one is missing, Yoast drops the BreadcrumbList schema for that page.
  • Yoast prints text as-is, so wrap dynamic values like post titles in esc_html().

For a broader look at breadcrumb options, read how to add breadcrumbs to WordPress sites. The filter itself is defined in Yoast’s breadcrumbs generator source.

FAQs

Common questions about the wpseo_breadcrumb_links filter:

Does wpseo_breadcrumb_links change the breadcrumb schema too?
Yes. Yoast builds both the HTML trail and the BreadcrumbList JSON-LD from the filtered array. Filters like wpseo_breadcrumb_single_link and wpseo_breadcrumb_output only change the HTML.
Is there a Yoast breadcrumbs shortcode?
Yes, [wpseo_breadcrumb]. In PHP templates use yoast_breadcrumb( '<p id="breadcrumbs">', '</p>' ), which only prints when "Enable breadcrumbs for your theme" is on or the theme declares support for it. The filter applies to all of them.
Why is my new breadcrumb item not showing?
Check that your condition matches the page (for example is_singular( 'post' ) won't run on pages), that the function returns $links, and purge any page cache. Also confirm the trail on the page actually comes from Yoast and not from your theme or WooCommerce.

Increase Maximum Execution Time in WordPress

“Maximum execution time of 30 seconds exceeded” means a PHP script ran longer than the max_execution_time setting allows. In WordPress it usually hits during updates, imports and backups.

Below are the ways to increase max_execution_time and how to tell which one your server honors. For a quick fix of the error itself, see Fix Maximum Execution Time Exceeded in WordPress.

Check the Current Value First

Go to Tools > Site Health > Info and open the Server section.

  • PHP time limit is the current max_execution_time in seconds.
  • PHP SAPI tells you which method will work: apache2handler is Apache mod_php, fpm-fcgi is PHP-FPM, litespeed is LiteSpeed.

Recheck this screen after each change to confirm PHP picked up the new value.

Increase max_execution_time in WordPress

Pick the method that matches your PHP SAPI. The examples use 300 seconds, the limit WordPress core sets for itself during updates.

1. wp-config.php (works on any server)

Add this to wp-config.php, above the /* That's all, stop editing! */ line:

if ( function_exists( 'set_time_limit' ) ) {
	set_time_limit( 300 );
}

Keep the function_exists() check. Since PHP 8, a function blocked with disable_functions counts as undefined, so a bare call crashes the site on hosts that disable it.

2. .user.ini (PHP-FPM and CGI)

On PHP-FPM, the most common setup today, create a .user.ini file next to wp-config.php:

max_execution_time = 300
max_input_time = 300

PHP rereads this file every 5 minutes by default (user_ini.cache_ttl), so the change may not show immediately.

3. .htaccess (Apache mod_php or LiteSpeed)

Back up .htaccess first, then add this line:

php_value max_execution_time 300

This only works with Apache mod_php or LiteSpeed. On Apache with PHP-FPM, php_value is an unknown directive and the site returns a 500 error, so remove it and use .user.ini.

4. php.ini (VPS or dedicated server)

If you control the server, set it in php.ini and restart PHP-FPM or Apache. On shared hosting, use the PHP settings screen in your control panel.

max_execution_time = 300

How max_execution_time Works

The directive caps how many seconds a script may run (30 by default). Calling set_time_limit() changes the limit at runtime and restarts the counter from zero.

On Linux, standard PHP builds count only the script’s own execution time. Database queries and remote API calls don’t count, so a request can run past 30 seconds without this error.

max_input_time is the separate limit for parsing request input such as POST data and uploads. Its default of -1 means it uses max_execution_time. It can’t be changed with set_time_limit() or ini_set(), only in .user.ini, .htaccess or php.ini.

When Raising the PHP Limit Isn’t Enough

If long requests still die after raising the limit, another layer is cutting them off, usually with a 502, 504 or 524 error.

  • PHP-FPM’s request_terminate_timeout kills the worker after a set time. It’s off by default, but many hosts enable it.
  • Nginx’s fastcgi_read_timeout defaults to 60 seconds.
  • Cloudflare returns a 524 if the origin doesn’t respond within 125 seconds on non-Enterprise plans.

Your host has to raise these. For one-off heavy jobs, use WP-CLI instead: on the command line max_execution_time defaults to 0, meaning no limit.

WP-Cron runs as a web request, so web limits apply. For long scheduled tasks, disable WP-Cron and use a real server cron job that runs wp cron event run --due-now.

See also Optimizing WordPress configuration with wp-config.php and the directive reference in the PHP manual.

FAQs

Common questions about the PHP time limit in WordPress:

What is the default max_execution_time in WordPress?
WordPress doesn't set a global value. It inherits the PHP setting, which defaults to 30 seconds, though many hosts use 60 to 300. Core raises the limit temporarily with set_time_limit() during updates.
Why doesn't php_value max_execution_time in .htaccess work?
php_value is only understood by Apache mod_php and LiteSpeed. On PHP-FPM it causes a 500 error. Check PHP SAPI in Site Health and use .user.ini if it says fpm-fcgi.
How do I increase max_input_time in WordPress?
Set max_input_time = 300 in .user.ini or php.ini, or php_value max_input_time 300 in .htaccess on mod_php. It can't be changed from wp-config.php because PHP doesn't allow it at runtime.
Can I set max_execution_time to 0?
Yes, 0 removes the PHP limit, but a stuck script can then hold a PHP worker until a server timeout kills it. A finite value like 300 is safer on a live site.

Remove WooCommerce Breadcrumbs on Specific Pages

If you are looking for a way to remove breadcrumbs on sites using WooCommerce, you can do it like this:

/**
 * Remove the breadcrumbs
 */
function savvy_remove_shop_breadcrumbs() {
    if (is_shop()) {

        remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);
    }
}
add_action('init', 'savvy_remove_shop_breadcrumbs');

This code removes breadcrumbs on the main shop page but you can use any other condition for other page types.

Restrict File Upload Types in WordPress

The following example will restrict file uploads in the media library to JPG and GIF files only. If you want to also allow PNG files, add the line 'png' => 'image/png' to this code:

function savvy_restrict_mime($mimes) {
    $mimes = array(
        'jpg|jpeg|jpe' => 'image/jpeg',
        'gif' => 'image/gif',
    );
    return $mimes;
}
add_filter('upload_mimes','savvy_restrict_mime');

More on this topic in the post How to allow uploading additional file types in WordPress?.

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“.

Display Content for Mobile Only with wp_is_mobile

There is a function called wp_is_mobile that provides a condition to display content only when the user is viewing on mobile. The function returns true when the site is loaded on mobile. Use it like this:

<?php  if( wp_is_mobile()){  ?>
// mobile stuff goes here
<?php } else { ?>
// desktop stuff goes here
<?php  } ?>

Note: Make sure your caching mechanism caches separately for mobile and desktop. You can do this for example with the WP-Rocket plugin.

Add Class to the First Post in the WordPress Loop

Say you want to highlight the first post in the loop in some way. You can add a specific class to it and then style it with CSS. Do this in functions.php:

function wps_first_post_class( $classes ) {
    global $wp_query;
    if( 0 == $wp_query->current_post )
        $classes[] = 'first';
        return $classes;
}
add_filter( 'post_class', 'wps_first_post_class' );

For more on customizing the WordPress loop, see WordPress Template Hierarchy.

Add Tracking Code to WooCommerce Thank You Page

The following code lets you add tracking code to the WooCommerce Thank You page after a purchase. The $order variable contains all the relevant information for that order so you can send the details to the tracking service you use.

Pay attention to the comments in the code to understand where to add the tracking code and how to use this variable:

/**
 * Add custom tracking code to the thank-you page
 */

function my_custom_tracking( $order_id ) {

	// Lets grab the order
	$order = wc_get_order( $order_id );

	/**
	 * Put your tracking code here
	 * You can get the order total etc e.g. $order->get_total();
	 */
	 
	// This is the order total
	$order->get_total();
 
	// This is how to grab line items from the order 
	$line_items = $order->get_items();

	// This loops over line items
	foreach ( $line_items as $item ) {
  		// This will be a product
  		$product = $order->get_product_from_item( $item );
 
  		// This is the products SKU
		$sku = $product->get_sku();
		
		// This is the qty purchased
		$qty = $item['qty'];
		
		// Line item total cost including taxes and rounded
		$total = $order->get_line_total( $item, true, true );
		
		// Line item subtotal (before discounts)
		$subtotal = $order->get_line_subtotal( $item, true, true );
	}
}
add_action( 'woocommerce_thankyou', 'my_custom_tracking' );