Snippet to add Edit link to category archive page

WooCommerce and its Storefront theme display so-called category archive pages to list products in the category.

If the category includes a description, it shows at the top of the page along with the category’s name.

This php snippet adds an Edit link to those pages allowing a privileged user logged in to the Dashboard to update the category name and/or description. It’s for the convenience of the store manager.

function my_archive_description_editlink () {
  /* user roles exempt from hiding. */
  $exemptRoles = array ( 'administrator', 'manage_woocommerce');  
  global $current_user;
  $result = array_intersect ($current_user->roles, $exemptRoles);
  if (count($result) === 0 ) return;
 
  /* privileged users here. */
  if ( is_product_taxonomy() && 0 === absint( get_query_var( 'paged' ) ) ) { 
   $term = get_queried_object();
   if ( $term ) {
     $term = $term->term_id;
     $link = sprintf("/wp-admin/term.php?taxonomy=product_cat&tag_ID=%s", $term);
     printf ('<div class="edit-link"><a class="post-edit-link" href="%s">Edit</a></span>', $link);  
    }
  } 
}
   
add_action( 'woocommerce_archive_description', 'my_archive_description_editlink' , 15);

Leave a Comment