M
M
MrSusua2015-11-24 20:23:19
WordPress
MrSusua, 2015-11-24 20:23:19

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 );

Now I need to make it work when the record is saved. On the WordPress forum, I found a function that is responsible for this. How to combine them?
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

1 answer(s)
M
Maxim Martirosov, 2015-11-26
@MrSusua

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 question

Ask a Question

731 491 924 answers to any question