Last active
March 8, 2024 15:19
-
-
Save kierzniak/0d0b763eb0471b8603f7a2e574eea6a9 to your computer and use it in GitHub Desktop.
Remove accents from url slug before WordPress search for a post
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
<?php | |
/** | |
* Remove accents from url slug before WordPress search for a post | |
* | |
* When you access page with accents you will not find any posts because | |
* WordPress is removing accents from url before saving it to database. | |
* This function will remove accents from url before WordPress start looking | |
* for post. | |
* | |
* @param $query WP_Query WordPress query | |
* | |
* @return WP_Query | |
*/ | |
function motivast_remove_accents_from_url_slug( $query ) { | |
/** | |
* Parse query only for single page/post | |
*/ | |
if( $query->is_main_query() && $query->is_singular() ) { // is_singular works for any post type | |
$name = $query->get('name'); // Get slug from url | |
$name = urldecode( $name ); // Decode url to unicode | |
$name = remove_accents($name); // Remove accents | |
$query->set('name', $name); // Set new name to search posts by slug without accents | |
} | |
return $query; | |
} | |
add_filter('pre_get_posts', 'motivast_remove_accents_from_url_slug'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment