0

Im developing plugin which calculates child date of birth and so on. So what I need is to add opengraph tags with appropriate data when someone wants to share the page on facebook. What i want is that these tags are appended to head only if someone had actually pressed submit button on the form.

When I do it like this:

if($_GET['calc_submit_1']) {
   function add_open_graph_tags(){    
   echo '<meta property="og:url" content="'.$shortlink.'" />';
   echo '<meta property="og:title" content="Ich bin in der '.floor($week_of_pregnancy).'. Schwangerschaftswoche" />';
   echo '<meta property="og:description" content="Ich habe den myToys-Fruchtbarkeitsrechner benutzt und folgendes Ergebnis erhalten: '.$shortlink.'" />';
   echo '<meta property="og:image" content="http://mytoys.beta-preview.de/wp-content/uploads/2016/02/wallhaven-411.jpg" />';
  }     
  add_action('wp_head', 'add_open_graph_tags');
}

The tags are not added to wp_head hook. Is it possible to do it the way i want? Any help appreciated.

1
  • I guess you are executing that code in the wrong place. Can you provide the context/file where that code is executed?
    – cybmeta
    Feb 12 '16 at 14:57
0

Your function will only be called when there is something in$_GET['calc_submit_1']. Means, when you submit the form, your function is triggered. When you refresh the page, and $_GET['calc_submit_1'] is empty, it won't triggered. When FB checks for OG, FB will not submit your form and your function will not be triggered. You can use add_option to save something in to database on form submit, and check that, instead of $_GET['calc_submit_1'].

8
  • 1
    You are totallly right, but I think that the OP wants to generate a dinamic URL from the form (URL + query string generated by the form, note it is submitted with get method), so each user can share exactly what they have set in the form, I guess, it is not described in the quesiton.
    – cybmeta
    Feb 12 '16 at 15:16
  • @cybmeta you are right. He should describe his scenario. If it's a dynamic URL, and his query string is available in the URL, then it should work. Feb 12 '16 at 15:37
  • Yeah thats what i really want. After user submits form it gets the results (parameters in the url by get). And then social share icons appears - so the user should share exactly results with specific text (opengraph tags). Strange thing its not added to head as i wrote at start...
    – kroma
    Feb 13 '16 at 10:45
  • @kroma where did you put your codes? Feb 13 '16 at 14:35
  • @тнє Sufi function calculator_1($atts) { here i render calculator if($_GET['calc_submit_1']) { things from my first post } }
    – kroma
    Feb 14 '16 at 17:02

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.