Skip to content

Instantly share code, notes, and snippets.

@zackkatz
Last active August 29, 2015 14:16
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 zackkatz/305032e4823043810384 to your computer and use it in GitHub Desktop.
Save zackkatz/305032e4823043810384 to your computer and use it in GitHub Desktop.
/**
* Replace the link text with a PDF image.
*
* Only for an anchor link to a PDF file in field output.
*
* Place this code (after <?php) at the bottom of your theme's functions.php file to enable it.
*
* @param string $html Existing HTML output
* @param array $args Arguments passed to the function
*/
function modify_gravityview_field_output( $html, $args ) {
$return = $html;
// Bail if not an anchor with a PDF.
preg_match( '/<a(.+?)>.+?(pdf)<\/a>/i', $return, $matches );
if ( empty( $matches ) )
return $return;
// How to replace the link text with an image:
// First, upload an image to your Media Library if necessary.
// Option 1.
// Edit the image and find the 'attachment_id' number in the Permalink.
// Enter that number in the parentheses here:
$image_url = '<img src="' . wp_get_attachment_url( 7 ) . '" />';
// Option 2.
// Use the name of file if you know its location in your uploads directory:
// $image_url = '<img src="' . content_url( 'uploads/pdficon_large.png' ) . '" />';
// Note: Linking to an external image is not recommended.
// Official Adobe stuff: http://www.adobe.com/legal/permissions/icons-web-logos.html#weblogos
// Replace the link text with the image URL.
$return = preg_replace( '/<a(.+?)>.+?<\/a>/i', "<a$1>" . $image_url . "</a>", $return );
return $return;
}
add_filter( 'gravityview_field_output', 'modify_gravityview_field_output', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment