WordPress Developer Note: str_contains is Polyfilled Since WP 5.9

One of the nice functions introduced in PHP 8.1 is str_contains. Also introduced is str_starts_with. These functions are easier to use for certain use-cases than strrpos.

If you’re a developer for WordPress you’re probably thinking that it would be nice to use these functions but you have no way to guarantee that your plugin/theme code is going to be running PHP 8.0.

WordPress though, has your back.

Starting in WordPress 5.9 the WordPress theme created a polyfill for the str_contains function inside of WordPress.

So, if you want, you can go ahead and use that nice new PHP function with your WordPress code – as long as your customer(s) are running at least WordPress 5.9.

Sweet, huh?

Of course, you could probably have created the polyfill yourself and include it in your plugins/themes – the underlying code is pretty simple:

if ( ! function_exists( 'str_contains' ) ) {
function str_contains( $haystack, $needle ) {
return ( '' === $needle || false !== strpos( $haystack, $needle ) );

Still, it’s pretty nice to know that WordPress core developers are constantly thinking about these kinds of backwards compatibility and developer usability things – it’s why developers and users continue flocking to WordPress.

Was This Article Useful? Or do you have questions or comments about it (or our products & services)? We'd love to hear from you!

Please enter your name.
Please enter a message.
You must accept the Terms and Conditions.
Please check the captcha to verify you are not a robot.

Automatic Notification Of New Articles

Sign up to get automatic notifications of new articles.  This is a different list than our standard list - you only get new articles once a week (usually on Mondays).  No other emails will be sent unless you sign up for our general list as well.

Posted in ,