Skip to content

Instantly share code, notes, and snippets.

@raylinanthony
Last active August 29, 2015 14:21
Show Gist options
  • Save raylinanthony/0c68f4929cce2f672296 to your computer and use it in GitHub Desktop.
Save raylinanthony/0c68f4929cce2f672296 to your computer and use it in GitHub Desktop.
Wrapper <img> to <figure> and <figcaption> for Wordpress
<?php
/*
* @author Raylin Aquino <raylinaquino.com>
* @since 25-05-2015 (d m Y)
* Short script that wrapper all images (<img>) tags in a <figure> tag, with its alt attr. in a <figcaption> tag.
* The goal this... is provide the way correct of to show the images in a blog post.
*/
function figure_wrap_img($content) {
return preg_replace_callback(
'/(<figure.*?>.*?)?(\<img.*?(alt(\s+)?=(\s+)?[\"|\']([\w\-.*\s_\p{Han}\X\p{L}]*)[\"|\']).*?\/?>)/isum',
function ($match){
if(stripos($match[0],"<figure") === false){
$ptr = "\n<figure class='figure-wrap'>\n\t%s\n\t%s\r\n</figure>\n"; $figcap = "";
if(!empty($match[6])) $figcap = "<figcaption>$match[6]</figcaption>";
return sprintf($ptr,$match[0],$figcap);
}
return $match[0];
}
,$content);
}
add_filter( 'the_content', 'figure_wrap_img' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment