I'm using gravity forms and on thank you page I would like to be able to add some of the fields into to head OG meta tags. So far I have this:
After Gravity forms submission we get the needed fields
function after_submission( $entry, $form ) {
$name = $entry[2];
$item = $entry[5];
insert_og_in_head( $name, $item );
}
add_action( "gform_after_submission", "after_submission", 10, 2 );
Write the metatags to head
function insert_og_in_head( $name = NULL, $item = NULL) {
global $post;
if ( !is_page( 6 ) ) //if it is not a post or a page
return;
echo '<meta property="og:title" content="' . $name . ' lorem opossum dolor"/>';
echo '<meta property="og:description" content="' . $item . ' lorem." />';
echo '<meta property="og:type" content="website"/>';
}
add_action( "wp_head", "insert_og_in_head", 5 );
It gets the needed fields from forms correctly, it gets to function where it supposed to write them, but still on my head the field parts are empty. What I'm doing wrong? I somehow feel it goes twice to insert_og_in_head function, first time writes correctly but on second time it overrides with empty.
if ( !is_page( 6 ) return
That means: it not is page with ID 6, then return.$name
and$item
are not valid parameters ofwp_head
; if they are added by gravity form plugin you should ask to gravity form support.insert_og_in_head
inwp_head
,$name
and$item
arenull
because they are not valid parameters forwp_head
callbak. If you executeinsert_og_in_head
inafter_submission
(callback ofgform_after_submission
) the meta are not printed in the head (or maybe yes, I don't know howgform_after_submission
works). As we don't know how or whengform_after_submission
works, we can not help you. You should ask to gravity form support or try another way to get form data inwp_head
, maybe using$_POST
or$_GET
insidewp_head
callback depending on the form method.