Created
July 14, 2022 12:10
-
-
Save pixeldevsio/200b2081af21ba8de6dfc8ee4db1df6d to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Update Single Post Content images to include image credit | |
* | |
* @param string $content | |
* @return string | |
*/ | |
function _codeable_addCreditToImage(string $content){ | |
global $post; | |
// Get all images from post content | |
preg_match_all("/<img(.*?)class=('|\")(.*?)('|\")(.*?)src=('|\")(.*?)('|\")(.*?)>/i", $content, $images); | |
// check if images exist. If not, exit. | |
if(!is_null($images)){ | |
$i = 0; | |
// Loop through images | |
foreach ($images as $key) { | |
// Get the class and match just the image ID. | |
$class = $images[3][$i]; | |
preg_match('/(\d+)[^-]*$/', $class, $pictureID); | |
// Set the image ID and check for Credit Line Post Meta | |
$imageID = $pictureID[0]; | |
$credit_line = get_post_meta($imageID, 'sanfrancisco_credit_line', true ); | |
// Do we have Credit Line? | |
if($credit_line){ | |
// Format the image with a credit line | |
$replacement = "<figure class='has-credit-line'> | |
<img ".$images[1][$i]." class=".$images[2][$i].$images[3][$i].$images[4][$i].$images[5][$i]." src=".$images[6][$i].$images[7][$i].$images[8][$i].$images[9][$i]."> | |
<div class='sf-entry-flags'> | |
<span class='sf-entry-flag sf-entry-flag-creditline'>" . get_post_meta($imageID , 'sanfrancisco_credit_line', true ) . "</span> | |
</div> | |
</figure>"; | |
// replace the content with the new images | |
$content = str_replace($images[0][$i], $replacement, $content); | |
} | |
$i++; | |
} | |
} | |
return $content; | |
} | |
add_filter('the_content', '_codeable_addCreditToImage', 9999); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment