GravityView: Add multiple og:image tags on the single entry view
This file contains 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 | |
/** | |
* 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