Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Obteohub/3d34ae53c907d52cf859b49b39247abe to your computer and use it in GitHub Desktop.
Save Obteohub/3d34ae53c907d52cf859b49b39247abe to your computer and use it in GitHub Desktop.
Adds secondary description field for brands taxonomy (Perfect WooCommerce Brands)
<?php
/**
* Adds a secondary description field to brands
*/
class Custom_PWB_Secondary_Description{
static $field_name = 'Secondary description';
static $field_desc = 'Here is the secondary description';
function __construct(){
add_action( 'pwb-brand_add_form_fields', array( $this, 'secondary_desc_add' ), 9, 1 );
add_action( 'pwb-brand_edit_form_fields', array( $this, 'secondary_desc_edit' ), 9, 2 );
add_action( 'edited_pwb-brand', array( $this, 'save_desc_add' ), 10, 2 );
add_action( 'create_pwb-brand', array( $this, 'save_desc_add' ), 10, 2 );
add_action( 'woocommerce_after_main_content', array( $this, 'add_desc_to_brands_page' ) );
}
/**
* Adds the extra field to 'edit-tags.php' page
*/
public function secondary_desc_add( $taxonomy ){
ob_start();
?>
<div class="form-field term-secondary-description-wrap">
<label for="tag-secondary-description"><?php echo self::$field_name;?></label>
<textarea name="secondary-description" id="tag-secondary-description" rows="5" cols="40"></textarea>
<p><?php echo self::$field_desc;?></p>
</div>
<?php
echo ob_get_clean();
}
/**
* Adds the extra field to 'term.php' page
*/
public function secondary_desc_edit( $brand, $taxonomy ){
$sec_desc = get_term_meta( $brand->term_id, 'secondary-description', true );
ob_start();
?>
<tr class="form-field term-secondary-description-wrap">
<th scope="row"><label for="secondary-description"><?php echo self::$field_name;?></label></th>
<td>
<textarea name="secondary-description" id="secondary-description" rows="5" cols="50" class="large-text"><?php echo $sec_desc; ?></textarea>
<p class="secondary-description"><?php echo self::$field_desc;?></p>
</td>
</tr>
<?php
echo ob_get_clean();
}
/**
* Saves the secondary description as term meta
*/
public function save_desc_add( $term_id, $taxonomy ){
if( isset( $_POST['secondary-description'] ) )
update_term_meta( $term_id, 'secondary-description', $_POST['secondary-description'] );
}
/**
* Adds the secondary description content after the brand archive page loop
*/
public function add_desc_to_brands_page(){
if( is_tax( 'pwb-brand' ) ){
$brand = get_queried_object();
$sec_desc = get_term_meta( $brand->term_id, 'secondary-description', true );
ob_start();
?>
<div class="secondary-description"><?php echo $sec_desc;?></div>
<?php
echo ob_get_clean();
}
}
}
new Custom_PWB_Secondary_Description();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment