Adding Meta Tags to your WordPress site using ACF

The following is an example of how to add meta tags to Pages using the custom fields plugin ACF.

  1. Create a custom field called ‘Pages’ and created 2 text fields called ‘Description’ and ‘Keywords’.
  2. Ensure that you have the rules set to the pages post type.
  3. Populate the appropriate page you would like to test.
  4. Now add the following code to your functions.php page so that the new meta tag fields are dropped into the header.
     //meta tag function
    
     function _set_meta_tag()
     {
    
     //global $nome;
     //global $descr;
     //global $file;
     //global $path_meta;
    
     $output .= '<meta property="author" content="https://plus.google.com/104573429015560904949" />';
    
     if(get_field('description')){
     $output .= '<meta property="description" content="'.get_field('description').'" />';
     }else {
     $output .= '<meta property="description" content="'.get_bloginfo('description').'" />';
     }
    
     if(get_field('keywords')){
     $output .= '<meta property="keywords" content="'.get_field('keywords').'" />';
     }else {
     $output .= '<meta property="keywords" content="'.get_bloginfo('name').'" />';
     }
     
    
     echo $output;
    
     }
    
     add_action('wp_head', '_set_meta_tag');
  5. Now you should be able to see the meta tags displaying on all pages you have set them up for.