Sanitize Title for Java

Today, I’m gonna show you a WordPress sanitize_title function. I working on a Spring JAVA project and I need sometimes WordPress functions. Because I love WordPress and maybe you know I’m a WordPress Developer. Eh nevermind…

 * Sanitize Title like WordPress
 * author Themesama (@theme_sama)
public static String sanitizeTitle(String str) {
  //Replace special charters with another one
  String from = "İıŞşÜüĞğÇçÖö ";
  String to = "iissuuggccoo-";
  //Make lower case
  str = str.toLowerCase();
  char[] out = null;
  for (int i = 0, len = str.length(); i < len; i++) {
    char c = str.charAt(i);
    int p = from.indexOf(c);

    if (p >= 0) {
      if (out == null) {
        out = str.toCharArray();
      out[i] = to.charAt(p);
  //Check changes
  if(out != null) {
    str = new String(out);
  //Scope of character
  str = str.replaceAll("[^a-z0-9-]", "");
  return str;

Method of use

System.out.println(yourClass.sanitizeTitle('This Long Title is what My Post or Page might be'));
//It should return a formatted value, the output would be this: this-long-title-is-what-my-post-or-page-might-be

I hope you can help me improve for this method 😉 Maybe we can add some methods for escape injections for next time.

Highlight main menu item for custom post types

Sometimes, we are using custom post types on WordPress and if a visitor looking single pages we need to highlight this item on main menu with a extra class names. This is important for UX Design. Here is an example for portfolio custom post type with a filter.

 * Add extra class names on main menu item for custom post type
 * @author Themesama(theme_sama)
if( !function_exists('add_extra_class_to_wp_nav_menu') ) {
  function add_extra_class_to_wp_nav_menu($classes , $item) {
    if (!empty( $item->title ) && $item->title == 'Portfolio' && is_singular('portfolio') ) {
      $classes[] = 'current-menu-parent';

    return $classes;

add_filter('nav_menu_css_class', 'add_extra_class_to_wp_nav_menu', 10, 2);

I’m checking with item title. You can check with item id or another things in $item object.

More details and example for nav_menu_css_class filter…

Search form not working after activating Polylang

Probably, you are using custom search form. If you have searchform.php inside your activated theme or if you wrote yourself a search form for WordPress, you need to remove it!

Just use get_search_form() function and modify your search form via a filter.

 * Get Slupy Search Form
 * @since 1.1.6
if( !function_exists( 'get_slupy_search_form' ) ) {

function get_slupy_search_form( $html ) {
  $html = '<form class="search" method="get" action="'.esc_url( home_url('/') ).'" role="search">
                    <input type="search" class="live-search" name="s" placeholder="'.esc_attr__( 'What are you looking for?', 'slupy' ).'">
                    '.( defined( 'ICL_LANGUAGE_CODE' ) ? '<input type="hidden" name="lang" value="'.esc_attr( ICL_LANGUAGE_CODE ).'"/>' : '' ).'
                    <button type="submit" role="button" class="search-submit">'.__( 'Search', 'slupy' ).'</button>
  return $html;

//filter for search form
add_filter( 'get_search_form', 'get_slupy_search_form' );

I’m using same lines with my own searchform.php file but this is working like magic 🙂


A belated idea…

“Tailors can’t sew up their robes”

A proverb

Today, I’m blogging! This project was something I always wanted to do. I want to say HELLO WORLD! before starting any project or any programming languages. You will found here a lot of solutions and details for web development and languages. I will try to explain as best enjoyable and understandable. Keep looking and follow me 😉


Premium Themes and Web Development