Skip to content

Instantly share code, notes, and snippets.

@luistinygod
Last active May 11, 2016 14:35
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 luistinygod/fc20dceb05d0414f4ae1d82e4c7682ea to your computer and use it in GitHub Desktop.
Save luistinygod/fc20dceb05d0414f4ae1d82e4c7682ea to your computer and use it in GitHub Desktop.
GravityView: Add multiple og:image tags on the single entry view
<?php
/**
* Displays multiple "og:image" opengraph meta tags according to the single entry available gallery images
*/
function my_gv_add_opengraph_multi_image() {
if( ! function_exists('gravityview_is_single_entry') || ! function_exists( 'gravityview_get_entry') ) {
return;
}
// get single entry id
$entry_id = gravityview_is_single_entry();
if( empty( $entry_id ) ) {
return;
}
$entry = gravityview_get_entry( $entry_id );
$form_id = empty( $entry['form_id'] ) ? '' : $entry['form_id'];
switch ( $form_id ) {
// Customise to your own case FORM ID
case '3':
// Customise to your own FIELD ID
$img_field = '9';
break;
}
if( !empty( $img_field ) && isset( $entry[ $img_field ] ) ) {
$imgs = json_decode( $entry[ $img_field ] );
if( is_array( $imgs ) ) :
foreach( $imgs as $img ) : ?>
<meta property="og:image" content="<?php echo esc_url( $img ); ?>" />
<?php endforeach;
else: ?>
<meta property="og:image" content="<?php echo esc_url( $imgs ); ?>" />
<?php endif;
}
}
add_filter( 'wp_head', 'my_gv_add_opengraph_multi_image', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment