-1

Please guys

I need help on how to remove special characters in POST TITLE -

Certain SYMBOLS LIKE < > { }

Please Note: I am a beginner coder, kindly make the solution very simple for me to understand

2
  • What exactly do you mean by special characters - emojis, accents / other diacritics, certain symbols, anything that's not A-Z ?
    – Rup
    Commented Dec 18, 2020 at 8:42
  • @Rup Yes, certain symbols like < > { }
    – Theophilus
    Commented Dec 18, 2020 at 9:38

2 Answers 2

1

You can use WordPress function wp_strip_all_tags()

$args = array(
    'post_type' => 'post',
    'post_title'    => wp_strip_all_tags( $string ),
  );

Or you can also use preg_replace() :

preg_replace('/[^A-Za-z0-9\-]/', '', $string);
0

You can use function:

sanitize_title( string $title, string $fallback_title = '', string $context = 'save' );

More info: https://developer.wordpress.org/reference/functions/sanitize_title/

3
  • That's for generating slugs though, not page titles. As one of the comments says "Despite the name of this function, the returned value is intended to be suitable for use in a URL, not as a human-readable title."
    – Rup
    Commented Dec 18, 2020 at 10:10
  • you can use filter sanitize_title & do preg_replace( '/[^A-Za-z0-9\-]/', '', $title); Commented Dec 18, 2020 at 10:14
  • I am a beginner coder, Please, make it more explanatory for me. Where should I add this snippet code?
    – Theophilus
    Commented Dec 18, 2020 at 10:24

Your Answer

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

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