Skip to content

Instantly share code, notes, and snippets.

@kierzniak
Last active March 8, 2024 15:19
Show Gist options
  • Save kierzniak/0d0b763eb0471b8603f7a2e574eea6a9 to your computer and use it in GitHub Desktop.
Save kierzniak/0d0b763eb0471b8603f7a2e574eea6a9 to your computer and use it in GitHub Desktop.
Remove accents from url slug before WordPress search for a post
<?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