Skip to content

Instantly share code, notes, and snippets.

@rafaehlers
Last active August 30, 2017 19:57
Show Gist options
  • Save rafaehlers/242d4be253f8f9ae3b313c067b017b26 to your computer and use it in GitHub Desktop.
Save rafaehlers/242d4be253f8f9ae3b313c067b017b26 to your computer and use it in GitHub Desktop.
Specify an entry image or a default image to the og:image Open Graph tag.
<?php
add_action( 'wpseo_opengraph', 'my_gv_wpseo_opengraph_image', 10, 1);
function my_gv_wpseo_opengraph_image( $img ) {
if( ! function_exists('gravityview_is_single_entry') || ! function_exists( 'gravityview_get_entry') ) {
return $img;
}
// get single entry id
$entry_id = gravityview_is_single_entry();
if( empty( $entry_id ) ) {
return $img;
}
$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 '30':
// Customise to your own FIELD ID
$img_field = '1';
break;
}
if( isset( $entry[ $img_field ] ) && !empty( $entry[ $img_field ])) {
$img = $entry[ $img_field ];
}else{
//specify here a default image
$img = 'http://yourwebsite.com/wp-content/uploads/2017/07/Image2.jpg';
}
$GLOBALS['wpseo_og']->image_output( $img );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment