Wordpress, WooCommerce code to Add OG tags in Category Archive page.
<?php
//Product Cat creation page
function text_domain_taxonomy_add_new_meta_field() {
?>
<div class="form-field">
<label for="term_meta[wh_meta_title]"><?php _e('Meta Title', 'text_domain'); ?></label>
<input type="text" name="term_meta[wh_meta_title]" id="term_meta[wh_meta_title]">
<p class="description"><?php _e('Enter a meta title, <= 60 character', 'text_domain'); ?></p>
</div>
<div class="form-field">
<label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'text_domain'); ?></label>
<textarea name="term_meta[wh_meta_desc]" id="term_meta[wh_meta_desc]"></textarea>
<p class="description"><?php _e('Enter a meta description, <= 160 character', 'text_domain'); ?></p>
</div>
<?php
}
add_action('product_cat_add_form_fields', 'text_domain_taxonomy_add_new_meta_field', 10, 2);
//Product Cat Edit page
function text_domain_taxonomy_edit_meta_field($term) {
//getting term ID
$term_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option("taxonomy_" . $term_id);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[wh_meta_title]"><?php _e('Meta Title', 'text_domain'); ?></label></th>
<td>
<input type="text" name="term_meta[wh_meta_title]" id="term_meta[wh_meta_title]" value="<?php echo esc_attr($term_meta['wh_meta_title']) ? esc_attr($term_meta['wh_meta_title']) : ''; ?>">
<p class="description"><?php _e('Enter a meta title, <= 60 character', 'text_domain'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'text_domain'); ?></label></th>
<td>
<textarea name="term_meta[wh_meta_desc]" id="term_meta[wh_meta_desc]"><?php echo esc_attr($term_meta['wh_meta_desc']) ? esc_attr($term_meta['wh_meta_title']) : ''; ?></textarea>
<p class="description"><?php _e('Enter a meta description', 'text_domain'); ?></p>
</td>
</tr>
<?php
}
add_action('product_cat_edit_form_fields', 'text_domain_taxonomy_edit_meta_field', 10, 2);
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta($term_id) {
if (isset($_POST['term_meta'])) {
$term_meta = get_option("taxonomy_" . $term_id);
$cat_keys = array_keys($_POST['term_meta']);
foreach ($cat_keys as $key) {
if (isset($_POST['term_meta'][$key])) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option("taxonomy_" . $term_id, $term_meta);
}
}
add_action('edited_product_cat', 'save_taxonomy_custom_meta', 10, 2);
add_action('create_product_cat', 'save_taxonomy_custom_meta', 10, 2);
////////////////////////////////////////////////////////////////
///Adding Open Graph Meta Tags To WordPress Head function
////////////////////////////////////////////////////////////////
function doctype_opengraph($output) {
return $output . '
xmlns:og="http://opengraphprotocol.org/schema/"
xmlns:fb="http://www.facebook.com/2008/fbml"';
}
add_filter('language_attributes', 'doctype_opengraph');
function wh_opengraph() {
global $post;
//for product cat archive page only
if (is_product_category()) {
$term = get_queried_object();
$metaArray = get_option('taxonomy_' . $term->term_id);
$productCatMetaTitle = $metaArray['wh_meta_title'];
$productCatMetaDesc = $metaArray['wh_meta_desc'];
?>
<meta property="og:type" content="object" />
<meta property="og:title" content="<?php echo $productCatMetaTitle; ?> | <?php echo get_bloginfo(); ?>" />
<meta property="og:description" content="<?php echo $productCatMetaDesc; ?>" />
<meta property="og:site_name" content="<?php echo get_bloginfo(); ?>" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:description" content="<?php echo $productCatMetaDesc; ?>" />
<meta name="twitter:title" content="<?php echo $productCatMetaTitle; ?> | <?php echo get_bloginfo(); ?>" />
<!--<meta name="description" content="<?php // echo $productCatMetaDesc; ?>">-->
<?php
} else {
return;
}
}
add_action('wp_head', 'wh_opengraph', 5);