Answer the question
In order to leave comments, you need to log in
How to add a WordPress function that changes [caption] to figure when post is saved?
Hello!
I wrote a regular expression that turns the [caption] shortcode into a figure tag:
$content = preg_replace( "/(\[caption.*?(id=\"\s*(.*?)\s*\").*?(align=\"\s*(.*?)\s*\").*?\].*?<a.*?href=\"\s*(.*?)\s*\".*?>.*?<img\s?(.*?)\s?alt=\"\s*(.*?)\s*\"\s*(.*?)\s?\/?\s?><\s*\/\s*a\s*>\s*(.*?)\s*\[\/caption\])/i", "<figure id=\"$3\" class=\"wp-caption $5\">\r\n\t<a href=\"$6\">\r\n\t\t<img $7 alt=\"$8\" $9>\r\n\t</a>\r\n\t<figcaption class=\"wp-caption-text\">$10</figcaption>\r\n</figure>", $content );
function my_filter_function_name( $content ) {
// Process content here
return $content;
}
add_filter( 'content_save_pre', 'my_filter_function_name', 10, 1 );
Answer the question
In order to leave comments, you need to log in
It is not clear why you are here such a regular.
Try like this:
function my_filter_function_name( $content ) {
return str_replace( array( '[caption', '[/caption]' ), array( '[figure', '[/figure]' ), $content );
}
add_filter( 'content_save_pre', 'my_filter_function_name', 10, 1 );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question