Make WordPress Core

Opened 3 weeks ago

Last modified 3 weeks ago

#63334 new defect (bug)

`switch_to_locale()` overwrites custom object types for core taxonomies

Reported by: chouby's profile Chouby Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7
Component: I18N Keywords:
Focuses: Cc:

Description

It's frequent that core taxonomies are registered for custom post types. For example in Jetpack: https://plugins.trac.wordpress.org/browser/jetpack/tags/14.5/jetpack_vendor/automattic/jetpack-classic-theme-helper/src/class-featured-content.php#L157

However after switch_to_locale() is called, the custom post type is unregistered for the taxonomy.

This is demonstrated by the following test:

<?php
register_post_type( 'cpt' );
register_taxonomy_for_object_type( 'category', 'cpt' );

$this->assertEqualsCanonicalizing( array( 'post', 'cpt' ), get_taxonomy( 'category' )->object_type ); // Passes.

switch_to_locale( 'de_DE' );
$this->assertEqualsCanonicalizing( array( 'post', 'cpt' ), get_taxonomy( 'category' )->object_type ); // Fails.

This is due to create_initial_taxonomies() directly hooked to change_locale without extra logic. This results in the core taxonomy to be fully re-initialized without its custom object types. See https://core.trac.wordpress.org/browser/tags/6.8/src/wp-includes/default-filters.php#L653

Change History (1)

#1 @swissspidy
3 weeks ago

Thanks for your report!

For context:

IIRC we added the hooks there to fix post type and taxonomy labels when changing locales. It's a workaround for issues such as #38643 and #41305.

Note: See TracTickets for help on using tickets.