Archives: Snippets | Page 20

Change WooCommerce Product Image Zoom Behavior

If you want to change the zoom (magnification) behavior when hovering over a product on the WooCommerce product page, you can use the woocommerce_single_product_zoom_options filter:

add_filter( 'woocommerce_single_product_zoom_options', 'custom_single_product_zoom_options' );
function custom_single_product_zoom_options( $zoom_options ) {
    // Changing the magnification level:
    $zoom_options['magnify'] = 0.7;

    return $zoom_options;
}

Here are the parameters you can change:

$zoom_options = array (
    'url' => false,
    'callback' => false,
    'target' => false,
    'duration' => 120, // Transition in milli seconds (default is 120)
    'on' => 'mouseover', // other options: grab, click, toggle (default is mouseover)
    'touch' => true, // enables a touch fallback
    'onZoomIn' => false,
    'onZoomOut' => false,
    'magnify' => 1, // Zoom magnification: (default is 1  |  float number between 0 and 1)
);

For more WooCommerce product page customization, see Mastering WooCommerce Hooks.

Dismiss ACF Unsafe HTML Notice

The ACF plugin in version 6.2.5 and above displays a message like this: ACF now automatically escapes unsafe HTML when rendered by the_field or the ACF shortcode. If you know your field content is safe and want to dismiss this message, add the following code to your functions.php file:

function savvy_acf_remove_msg() {
	add_filter('acf/admin/prevent_escaped_html_notice', '__return_true');
	add_filter('acf/shortcode/allow_unsafe_html', '__return_true');
	add_filter('acf/the_field/allow_unsafe_html', '__return_true');
}
add_action('init', 'savvy_acf_remove_msg');

Change the Rank Math Metabox Display Priority

For Rank Math users, use the rank_math/metabox/priority filter to change the display order of the Rank Math metabox in the editor.

Setting the value to low will make the Rank Math metabox appear after other metaboxes, such as ACF fields.

/**
 * Change the Rank Math Metabox Priority
 *
 * @param array $priority Metabox Priority.
 */
add_filter( 'rank_math/metabox/priority', function( $priority ) {
	return 'low';
});

Change the Sitemap URL Base in Rank Math

The default Rank Math sitemap URL is https://example.com/sitemap_index.xml. The rank_math/sitemap/base_url filter adds a folder in front of it, so the index and every child sitemap move to a path like /search-sitemaps/sitemap_index.xml.

You might want this to keep sitemaps out of the site root, to avoid a clash with another tool that expects sitemap_index.xml, or to match the URL structure of a site you migrated to Rank Math.

To control what goes into the sitemap instead, see exclude a post type from the Rank Math sitemap.

The Code

Add the filter to your child theme’s functions.php file or to a custom plugin. This example moves the sitemaps under search-sitemaps/:

/**
 * Filter to change the Sitemap URL Base
 *
 * @param string $base New URL Base
 */
add_filter( 'rank_math/sitemap/base_url', function( $base ){
	return 'search-sitemaps/';
}, 10, 1 );

After saving the file, go to Settings > Permalinks and click Save Changes. That flushes the rewrite rules, and until you do it the new URL returns a 404.

How It Works

Rank Math builds every sitemap URL as home_url( $base . $file ). By default $base is an empty string, or index.php/ if your permalink structure includes index.php.

The filter is documented in the Rank Math filters and hooks reference.

Its value is used for the rewrite rules that serve the sitemaps and for the links Rank Math prints: the entries inside the sitemap index, the Sitemap: line in the virtual robots.txt, and the URL shown in the plugin settings.

Two details trip people up:

  • Keep the trailing slash. Rank Math appends the file name directly, so search-sitemaps without a slash gives you search-sitemapssitemap_index.xml.
  • Don’t add a leading slash. The same string goes into the rewrite rule, and WordPress matches rules against the path without a leading slash, so the sitemap would 404.

How to Find Your Rank Math Sitemap URL

The exact address, including any custom base, is shown at the top of Rank Math SEO > Sitemap Settings > General, next to “Your sitemap index can be found here”. Rank Math also redirects /sitemap.xml to /sitemap_index.xml at the site root.

If your site still uses plain permalinks (?p=123), there are no pretty sitemap URLs. The index is served at ?sitemap=1 instead.

To get the URL in PHP, use Rank Math’s template tag. Rank Math doesn’t register a sitemap object on rank_math(), so rank_math()->sitemap returns null.

if ( function_exists( 'rank_math_get_sitemap_url' ) ) {
	$sitemap_url = rank_math_get_sitemap_url(); // Includes the filtered base.
}

After You Change the Base

Moving the sitemap breaks any place that has the old URL stored. Go through this list:

  • Submit the new index URL in Google Search Console and remove the old one. Our guide to creating and submitting a sitemap walks through it.
  • If robots.txt has a hard-coded Sitemap: line, update it by hand. Rank Math skips adding its own line when one already exists.
  • On Nginx, Rank Math shows rewrite rules on the Sitemap Settings screen. They include the base, so copy the updated rules into your server config.
  • The built-in /sitemap.xml redirect still points to /sitemap_index.xml at the root, so don’t rely on it once the base changes.

FAQs

Common questions about the Rank Math sitemap URL:

What is the default Rank Math sitemap URL?
It's /sitemap_index.xml on your home URL, for example https://example.com/sitemap_index.xml. Each post type and taxonomy gets its own child sitemap, such as post-sitemap.xml and category-sitemap.xml.
Can I rename sitemap_index.xml itself?
Yes, with the rank_math/sitemap/index/slug filter. Return the new name without .xml, then save your permalinks. The base filter changes the folder, the slug filter changes the file name.
What does the rank_math/sitemap/index filter do?
It doesn't change the URL. It appends raw XML to the end of the sitemap index, so you can list extra <sitemap> entries, such as a sitemap generated by another plugin.
Why does my new sitemap URL return a 404?
The rewrite rules haven't been flushed, or the base is missing its trailing slash or has a leading one. Save Settings > Permalinks once, and on Nginx update the server rewrite rules as well.

Change the Author Base URL in Rank Math

Use the rank_math/author_base filter to change the URL base for author archives in Rank Math.

When the author base is set using this filter, the Author Base option configured by users under Rank Math SEO → Titles & Meta → Authors will have no effect.

In the example below, the author base is changed to writers (i.e., /writers/username/ instead of /author/username/).

/**
 * Allow developers to change the author base.
 *
 * @param string $base The author base.
 */
add_filter( 'rank_math/author_base', function( $base ) {
	return 'writers';
});

Exclude a Specific Post Type from the Sitemap in Rank Math

Rank Math lets you choose which post types go into the XML sitemap with the Include in Sitemap toggle under Rank Math SEO > Sitemap Settings > Post Types.

To exclude a post type from the Rank Math sitemap in code instead, use the rank_math/sitemap/exclude_post_type filter.

Code is the better choice when the decision depends on a condition, when a plugin or theme you ship registers the post type, or when you don’t want the setting switched back on from the dashboard.

To move the sitemap itself to a different address, see change the sitemap URL base in Rank Math.

The Code

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

/**
 * Filter decision if post type is excluded from the XML sitemap.
 *
 * @param bool   $exclude Default false.
 * @param string $type    Post type name.
 */
add_filter( 'rank_math/sitemap/exclude_post_type', function( $exclude, $type ){
	if ( 'snippet' === $type ) {
		return true;
	}
	return $exclude;
}, 10, 2 );

How It Works

Rank Math runs this filter for every post type that has Include in Sitemap turned on. It passes two arguments: $exclude (default false) and $type, the post type name.

Return true and that post type’s sitemap file (for example snippet-sitemap.xml) is no longer generated or listed in the sitemap index. For every other type, return $exclude unchanged so you don’t override other code that uses the same filter.

Replace 'snippet' with your post type’s name. That is the key passed to register_post_type(), not the label shown in the admin menu.

The filter can only remove post types. It can’t bring back a post type whose Include in Sitemap setting is off, because Rank Math checks that setting before it runs the filter.

To exclude several post types at once, compare against an array:

add_filter( 'rank_math/sitemap/exclude_post_type', function( $exclude, $type ) {
	$excluded = array( 'snippet', 'portfolio', 'testimonial' );

	return in_array( $type, $excluded, true ) ? true : $exclude;
}, 10, 2 );

Exclude a Single Page Instead

If you only want to remove one page or post from the sitemap, you don’t need to exclude the whole post type. Rank Math gives you three ways to do it:

  • Set the page’s Robots Meta to No Index in its Rank Math settings. Rank Math leaves noindex content out of the sitemap by default.
  • Add the ID to Sitemap Settings > General > Exclude Posts (comma-separated). It’s an advanced option, so switch Rank Math to Advanced Mode if you don’t see the field.
  • Use the rank_math/sitemap/entry filter and return false for that URL.

Here is the code version, which removes posts 42 and 108 from whatever sitemap they belong to:

add_filter( 'rank_math/sitemap/entry', function( $url, $type, $object ) {
	if ( 'post' === $type && in_array( (int) $object->ID, array( 42, 108 ), true ) ) {
		return false;
	}
	return $url;
}, 10, 3 );

$type is post for all post types (pages included), term for taxonomy terms and user for authors. Cast the ID to an integer: Rank Math reads these rows straight from the database, so the ID arrives as a string and a strict comparison would never match.

Both filters are listed in the Rank Math filters and hooks reference.

Check That It Worked

Rank Math caches generated sitemaps, so the old version can still show after you add the filter. If you have WP-CLI, this command clears the cache and rebuilds the sitemap:

wp rankmath sitemap generate

Then open your sitemap index and confirm the post type’s sitemap is gone. If a page cache or CDN sits in front of the site, purge it too, or you may be looking at a stored copy.

For the bigger picture on what belongs in a sitemap and how to submit it, read how to create and submit a sitemap for WordPress sites.

FAQs

Common questions about excluding content from the Rank Math sitemap:

Is there a rank_math/sitemap/post_types filter?
No. Rank Math has no filter by that name. Use rank_math/sitemap/exclude_post_type to remove a post type, or the Include in Sitemap toggle under Sitemap Settings > Post Types.
Does excluding a post type from the sitemap stop Google from indexing it?
No. The posts stay indexable, and Google can still find them through links. If you want them out of search results, set the post type to noindex under Rank Math SEO > Titles & Meta.
How do I exclude a taxonomy from the Rank Math sitemap?
Use rank_math/sitemap/exclude_taxonomy. It works the same way: it receives $exclude and the taxonomy name, and returning true removes that taxonomy's sitemap.
I added the filter but the post type is still in the sitemap. Why?
Usually it's the sitemap cache or a page cache. Run wp rankmath sitemap generate, purge your caching plugin or CDN, and check that the name in the code matches the registered post type key exactly.