0

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.

  • 1
    You are echoing the meta tags only for page with ID equal to 6. if ( !is_page( 6 ) return That means: it not is page with ID 6, then return. – cybmeta Jan 12 '15 at 12:49
  • The form thank you page is ID 6 so that is correct. Like I said, I believe it loads it once but for some reason it goes twice and second time makes the variables empty – user995317 Jan 12 '15 at 13:34
  • The variables $name and $item are not valid parameters of wp_head; if they are added by gravity form plugin you should ask to gravity form support. – cybmeta Jan 12 '15 at 13:36
  • No, trouble is not getting the data form. I get them. But inserting them into to head is problem. – user995317 Jan 12 '15 at 13:38
  • When you execute insert_og_in_head in wp_head, $name and $item are null because they are not valid parameters for wp_head callbak. If you execute insert_og_in_head in after_submission (callback of gform_after_submission) the meta are not printed in the head (or maybe yes, I don't know how gform_after_submission works). As we don't know how or when gform_after_submission works, we can not help you. You should ask to gravity form support or try another way to get form data in wp_head, maybe using $_POST or $_GET inside wp_head callback depending on the form method. – cybmeta Jan 12 '15 at 13:58
-1

Well You can also try by adding action inside.

function after_submission( $entry, $form ) {

    $name = $entry[2];
    $item = $entry[5];

    insert_og_in_head( $name, $item );
    add_action( "wp_head", "insert_og_in_head" );

}
add_action( "gform_after_submission", "after_submission", 10, 2 );

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"/>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.