Created
August 6, 2021 11:00
-
-
Save harkor/718fb2a2b153a0db09f3b2b0d29c40ad to your computer and use it in GitHub Desktop.
Fix 404 error WPML - Slug with %custom_taxonomy% in rewrite slug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* To make it work you need : | |
* 1 - Set a translation in WPML for the custom post type | |
* 2 - flush permalinks | |
*/ | |
// Example custom post type snippet | |
add_action('init', function(){ | |
// Register Custom Post type | |
register_post_type('craft', [ | |
// ... | |
'rewrite' => array( | |
'slug' => 'craft/%craft_category%', | |
'with_front' => true, | |
), | |
// ... | |
]); | |
// Register custom Taxonomy | |
register_taxonomy( | |
'craft_category', // Taxonomy slug | |
'craft', // Post Type | |
array( | |
'label' => 'Craft Taxonomies', | |
'hierarchical' => true, | |
'show_ui' => true, | |
) | |
); | |
fixCategorySlugForLanguages('craft/%craft_category%', 'craft', 'craft_category'); | |
}, 'content_type')); | |
// Example: fixCategorySlugForLanguages('craft/%craft_category%', 'craft', 'craft_category); | |
function fixCategorySlugForLanguages($slug, $post_type, $category){ // | |
$WPMLDefaultLanguage = apply_filters( 'wpml_default_language', null ); // Get default language (not need to fix this one) | |
$WPMLLanguages = apply_filters( 'wpml_active_languages', null); // Get All active languages from WPML settings | |
foreach($WPMLLanguages as $languageCode => $language): | |
// Skip default language | |
if($languageCode == $WPMLDefaultLanguage): | |
continue; | |
endif; | |
// Get translated post Slug | |
$postSlug = apply_filters( 'wpml_translate_single_string', $slug, 'WordPress', 'URL slug: ' . $post_type, $languageCode ); | |
// Find base translated slug | |
$postSlug = str_replace('/%'. $category .'%', '', $postSlug); | |
// Fix category WPML translations (maybe you need to adapt for your case) | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/attachment/([^/]+)/?$', 'index.php?attachment=$matches[1]'); | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/attachment/([^/]+)/trackback/?$', 'index.php?attachment=$matches[1]&tb=1'); | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?attachment=$matches[1]&feed=$matches[2]'); | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?attachment=$matches[1]&feed=$matches[2]'); | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&cpage=$matches[2]'); | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/attachment/([^/]+)/embed/?$', 'index.php?attachment=$matches[1]&embed=true'); | |
add_rewrite_rule($postSlug.'/([^/]+)/([^/]+)/embed/?$', 'index.php?'. $category .'=$matches[1]&'. $post_type .'=$matches[2]&embed=true'); | |
add_rewrite_rule($postSlug.'/([^/]+)/([^/]+)/trackback/?$', 'index.php?'. $category .'=$matches[1]&'. $post_type .'=$matches[2]&tb=1'); | |
add_rewrite_rule($postSlug.'/([^/]+)/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?'. $category .'=$matches[1]&'. $post_type .'=$matches[2]&paged=$matches[3]'); | |
add_rewrite_rule($postSlug.'/([^/]+)/([^/]+)/comment-page-([0-9]{1,})/?$', 'index.php?'. $category .'=$matches[1]&'. $post_type .'=$matches[2]&cpage=$matches[3]'); | |
add_rewrite_rule($postSlug.'/([^/]+)/([^/]+)(?:/([0-9]+))?/?$', 'index.php?'. $category .'=$matches[1]&'. $post_type .'=$matches[2]&page=$matches[3]'); | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/([^/]+)/?$', 'index.php?attachment=$matches[1]'); | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/([^/]+)/trackback/?$', 'index.php?attachment=$matches[1]&tb=1'); | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?attachment=$matches[1]&feed=$matches[2]'); | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?attachment=$matches[1]&feed=$matches[2]'); | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$', 'index.php?attachment=$matches[1]&cpage=$matches[2]'); | |
add_rewrite_rule($postSlug.'/[^/]+/[^/]+/([^/]+)/embed/?$', 'index.php?attachment=$matches[1]&embed=true'); | |
add_rewrite_rule($postSlug.'/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?'. $category .'=$matches[1]&paged=$matches[2]'); | |
add_rewrite_rule($postSlug.'/([^/]+)/comment-page-([0-9]{1,})/?$', 'index.php?'. $category .'=$matches[1]&cpage=$matches[2]'); | |
add_rewrite_rule($postSlug.'/([^/]+)/?$', 'index.php?'. $category .'=$matches[1]'); | |
endforeach; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment