Tuesday, January 5, 2016

WordPress - add_tag_class() vs preg_replace()

If you got this error/warning:

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in functions.php on line 209

preg_replace with /e modifier was deprecated in version 5.5:

http://php.net/manual/en/migration55.deprecated.php

"The preg_replace() /e modifier is now deprecated. Instead, use the preg_replace_callback() function."

Looking the documentation:

http://php.net/manual/en/function.preg-replace-callback.php

Then change this:

$regex = "#(.*tag-link[-])(.*)(' title.*)#e";

To this:

$regex = "#(.*tag-link[-])(.*)(' title.*)#";

And change this:

$tagn[] = preg_replace($regex, "('$1$2 label tag-'.get_tag($2)->slug.'$3')", $tag );

To this:

$tagn[] = preg_replace_callback($regex,
    function ($matches) {
        return $matches[1].$matches[2] ." label tag-". get_tag($matches[2])->slug . $matches[3];
    }, $tag);

Cheers!