Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save titodevera/db32045cb68e03ad8bd6dd2a50a084f7 to your computer and use it in GitHub Desktop.

Select an option

Save titodevera/db32045cb68e03ad8bd6dd2a50a084f7 to your computer and use it in GitHub Desktop.

Revisions

  1. titodevera created this gist Feb 1, 2018.
    76 changes: 76 additions & 0 deletions class-custom-pwb-secondary-description.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    <?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();