Skip to content

Instantly share code, notes, and snippets.

@everdaniel
Created October 23, 2013 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everdaniel/7127962 to your computer and use it in GitHub Desktop.
Save everdaniel/7127962 to your computer and use it in GitHub Desktop.
WordPress Image Caption Filter
<?php
add_filter( 'img_caption_shortcode', 'cleaner_caption', 10, 3 );
function cleaner_caption( $output, $attr, $content ) {
$output2 = null;
$output3 = null;
$pattern = 'http://';
/* We're not worried abut captions in feeds, so just return the output here. */
if ( is_feed() )
return $output;
/* Set up the default arguments. */
$defaults = array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
);
/* Merge the defaults with user input. */
$attr = shortcode_atts( $defaults, $attr );
/* Set up the attributes for the caption <div>. */
$attributes = ( !empty( $attr['id'] ) ? ' id="' . esc_attr( $attr['id'] ) . '"' : '' );
$attributes .= ' class="wp-caption ' . esc_attr( $attr['align'] ) . '"';
$attributes .= ' style="width: ' . esc_attr( $attr['width'] ) . 'px"';
/* Open the caption <div>. */
$output = '<div' . $attributes .'>';
/* Allow shortcodes for the content the caption was created for. */
$output .= do_shortcode( $content );
$id = explode("_", $attr['id']);
if ( get_field('photographer', $id['1']) )
{
$photographer_web = get_field('photographer_web', $id['1']);
$photographer = get_field('photographer', $id['1']);
if (strpos($photographer_web, $pattern) === false)
{
$photographer_web = 'http://'.$photographer_web;
}
$output2 = '<span>Photo by </span>';
$output2 .= '<a class="bmd_photographer" href="';
$output2 .= $photographer_web;
$output2 .= '" target="_blank">';
$output2 .= $photographer;
$output2 .= '</a>';
}
if (get_field('photo_courtesy', $id['1']))
{
$courtesy = get_field('photo_courtesy', $id['1']);
$courtesy_website = get_field('photo_courtesy_website', $id['1']);
$output3 = '<span class="bmd_photo_courtesy">, courtesy of ';
if ($courtesy_website)
{
if (strpos($courtesy_website, $pattern) === false)
{
$courtesy_website = 'http://'.$courtesy_website;
}
$output3 .= '<a class="bmd_photographer" href="';
$output3 .= $courtesy_website;
$output3 .= '" target="_blank">';
$output3 .= $courtesy;
$output3 .= '</a>';
}
else
{
$output3 .= $courtesy;
}
$output3 .= '</span>';
}
if ($output2)
{
$output .= $output2;
}
if ($output3)
{
$output .= $output3;
}
/* Close the caption </div>. */
$output .= '</div>';
/* Return the formatted, clean caption. */
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment