Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save titodevera/db32045cb68e03ad8bd6dd2a50a084f7 to your computer and use it in GitHub Desktop.
Save titodevera/db32045cb68e03ad8bd6dd2a50a084f7 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();
@Obteohub
Copy link

Hi, @titodevera thanks for the solution. This code seems not to function well. The editor can only take plain text formats. It does not take links, images, blocks, etc. I have modified the code to the format below but the only problem I'm facing is, 'the content is not visible in the backend after adding text to the second description. I see an empty field in the second description meta box. check code below.

/**
* 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 ){
		?>
		<div class="form-field">
			<label for="tag-secondary-description"><?php echo self::$field_name;?></label>
			 <?php
  $settings = array(
     'textarea_name' => 'Secondary description',
     'quicktags' => array( 'buttons' => 'em,strong,link' ),
     'tinymce' => array(
        'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
        'theme_advanced_buttons2' => '',
     ),
     'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
  );

  wp_editor( '', 'seconddesc', $settings );
  ?>	
		</div>
		<?php
	}

	/**
	 * 		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 );
		?>
		<tr class="form-field">
			<th scope="row"><label for="secondary-description"><?php echo self::$field_name;?></label></th>
			<td>
							<?php
  $settings = array(
     'textarea_name' => 'secondary-description',
     'quicktags' => array( 'buttons' => 'em,strong,link' ),
     'tinymce' => array(
        'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
        'theme_advanced_buttons2' => '',
     ),
     'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
  );

  wp_editor( '', 'seconddesc', $settings );
  ?>
				<p class="secondary-description"><?php echo self::$field_desc;?></p>
			</td>
		</tr>
		<?php
	
	}

	/**
	 * 		Saves the secondary description as term meta
	 */
	public function save_desc_add( $term_id, $tt_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 );
			
			?>
			<div class="secondary-description"><?php echo $sec_desc;?></div>
			<?php
			
		}
	}

}
new Custom_PWB_Secondary_Description();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment