I need to sanitize WordPress title with commas that is using commas to replace the spaces.

i used this but didn't worked for me

echo sanitize_title_with_commas("the_title");

What i mean is:

if this is the main title this is WordPress forum when I use the code it will become this,is,wordpress,forum

Thanks in advance.


1 Answer 1


Depending on how you are outputting the title, the answer is different. But there are two possibilities.

By Using wp_title();

If your theme is using the wp_title(); function in its header.php file, you can use the wp_title filter. However, this function is being deprecated since 4.4.

add_filter( 'wp_title', 'filter_the_title' );
function filter_the_title( $title ) {
    $title = str_replace( ' ', ',' , $title);
    return $title;

By Using add_theme_support( 'title-tag' );

Newer themes add this method to enable the title tag support. If this is your case, you can use the pre_get_document_title filter to do so.

add_filter( 'pre_get_document_title', 'filter_the_title' );
function filter_the_title( $title ) {
    $title = str_replace( ' ', ',' , $title);
    return $title;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.