Support

Account

Home Forums General Issues Trying to Add ACF Custom Field to Page Source Meta at the top For SEO

Solved

Trying to Add ACF Custom Field to Page Source Meta at the top For SEO

  • I thought this would be easy to figure out, but I’m stumped.

    I properly created three custom fields with ACF for a Google Shopping feed (GTIN, MPN, Brand). These work great for my xml feed generator for Google Shopping.

    However, I’d like these custom ACF fields to also show up in the source code on the single product page only (for woocommerce). These don’t have to appear visually on the page necessarily, just in the source code at the top when I view source code for the page.

    For example something like:

    <meta property=”GTIN” content=”123456789112″ />

    Something like this.

    Is this possible? I’m happy to update the functions.php file of my child theme to do this as it’s only three custom ACF fields I created, but I can’t figure this out for the life of me.

    Any help is appreciated, thanks.

  • Are the fields saved on the product or somewhere else like an options page?

  • Hi John, the custom fields are setup to only show on the product pages.

    They are included as data entry fields on the product entry area in the dashboard.
    (Attached are the fields I created)

    These work great when creating an XML feed for Google Shopping.

    I would just like these to appear in the header source code for greater SEO.

    Any help is appreciated, thanks!

  • I would create an action in your functions.php file.

    
    add_action('wp_head', 'product_details_meta');
    function product_details_meta() {
      // get the queried object
      $queried_object = get_queried_object();
      // see if it's a post
      if (!is_a($queried_object, 'WP_POst')) {
        // not a post
        return;
      }
      // set $post_id
      $post_id = $queried_object->ID;
      // check post type
      if (get_post_type($post_id) != 'product') {
        // not a product
        return;
      }
      // use $post_id to get ACF field values
      $value = get_field('your_field_name', $post_id);
      // your code to output meta tags here
    }
    
  • Hi John,

    Thank you so much for your detailed reply.
    Since this type of coding exceeds my skill level, (translation: I’m kind of clueless) in the code above where commented out “// your code to output meta tags here”

    What would the sample code look like if, for example, my field was called “gtin” and I would like it to display the following in the source code:

    <meta property=”GTIN” content=”123456789112″ />

    If I could see an example of the code needed on that line, I could cut/paste and configure the rest for the other fields. Thank you for your time and help here!

  • 
    if (get_field('gtin', $post_id)) {
      ?><meta property="GTIN" content="<?php the_field(gtin', $post_id);?>" /><?php 
    }
    
  • Hi John,

    Thanks so much for your time on this. I replaced the “// your code to output meta tags here” with the code above, and the following code was my block of text added to the functions.php of my child theme:

    add_action('wp_head', 'product_details_meta');
    function product_details_meta() {
      // get the queried object
      $queried_object = get_queried_object();
      // see if it's a post
      if (!is_a($queried_object, 'WP_POst')) {
        // not a post
        return;
      }
      // set $post_id
      $post_id = $queried_object->ID;
      // check post type
      if (get_post_type($post_id) != 'product') {
        // not a product
        return;
      }
      // use $post_id to get ACF field values
      $value = get_field('gtin', $post_id);
      if (get_field('gtin', $post_id)) {
      ?>
      <meta property="GTIN" content="<?php the_field(gtin', $post_id);?>" />
      <?php 
    }
    

    Sadly, it threw up a critical error on all pages of my site when I included this.

    If you happen to see anything glaring that I mistakenly did with the code above, please let me know. Once again, thanks for your time and your help is greatly appreciated. Thanks again.

  • 
    add_action('wp_head', 'product_details_meta');
    function product_details_meta() {
      // get the queried object
      $queried_object = get_queried_object();
      // see if it's a post
      if (!is_a($queried_object, 'WP_POst')) {
        // not a post
        return;
      }
      // set $post_id
      $post_id = $queried_object->ID;
      // check post type
      if (get_post_type($post_id) != 'product') {
        // not a product
        return;
      }
      // use $post_id to get ACF field values
      if (get_field('gtin', $post_id)) {
        ?><meta property="GTIN" content="<?php the_field('gtin', $post_id); ?>" /><?php 
      }
    }
    
  • John, you’re awesome! That worked like a charm and was exactly was I was looking for.

    I sincerely appreciate you time, your detailed replies and your help with this.
    Thanks so much!

  • Very nice, John I didn’t think to use wp_head, I was so focused on header.php.

    Very nice indeed! Thank you for this!

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.