Last active
August 29, 2015 14:21
-
-
Save raylinanthony/0c68f4929cce2f672296 to your computer and use it in GitHub Desktop.
Wrapper <img> to <figure> and <figcaption> for Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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