0

I added a custom column to the manage categories admin page.

The code is generalized because is more specific in my case, but the meaning is the same

function mytheme_custom_column( $columns )
{
    $columns['my_column'] = 'My custom column';
    return $columns;
}
add_filter( 'manage_edit-category_columns' , 'mytheme_custom_column' );



function mytheme_custom_column_fill( $content, $column_name, $term_id )
{
    if ( 'my_column' == $column_name ) {
        // Get content using $term_id
        $content = mytheme_get_custom_field( $term_id );

        if( empty( $content ) )
        {
            // If column is empty, put a minus
            $content = '-';
        }
    }
    return $content;
}
add_filter( 'manage_category_custom_column', 'mytheme_custom_column_fill', 10, 3 );

Then a I added a new custom field in the category creation form and saved it in this way.

/*
* Saves category custom field on category create
*
*/
function mytheme_save_custom_field( $term_id ){
    if( isset( $_POST['my_custom_field'] ) ){
        mytheme_save_custom_field( $_POST['my_custom_field'], $term_id );
    }

}
add_action( 'created_category', 'mytheme_save_custom_field'); 

When I create the category, Wordpress sends the form with Ajax and adds automatically the new category row to the table.

The custom field should be showed in the table after creation, but always prints a '-', like "the custom field is empty", but when I reload the page, the field apperas correctly.

I think the problem is in this hook, that fires after the ajax response is sent, but not sure.

add_action( 'created_category', 'mytheme_save_custom_field'); 
  • problem is in your $content = mytheme_get_custom_field( $term_id ); this always showing empty content. Fix this line bugs will solve your issue. – Faysal Mahamud Oct 5 '17 at 21:50
  • I am giving you full code in the answer. – Faysal Mahamud Oct 5 '17 at 22:04
0

The code is fully functional and tested. Add this in functions.php and check. I am giving you an example using the checkbox in the add and edit for the category.

function mytheme_custom_column( $columns )
{
    $columns['my_column'] = 'My custom column';
    return $columns;
}
add_filter( 'manage_edit-category_columns' , 'mytheme_custom_column' );




function mytheme_custom_column_fill( $content, $column_name, $term_id )
{

    if ( 'my_column' == $column_name ) {
        // Get content using $term_id
        $content = get_term_meta( $term_id, 'show_category', true );
// be specific your content gets correct conent
        if( !empty( $content ) )
        {
            //$content = 'your custom field content here';
            return $content;
        }else{
          $content = '-';
          return $content;
        }
    }
}
add_filter( 'manage_category_custom_column', 'mytheme_custom_column_fill', 10, 3 );


// Add new term page
function mytheme_add_meta_fields( $taxonomy ) { ?>
    <div class="form-field term-group">
        <label for="show_category">
          <?php _e( 'Show Category', 'codilight-lite' ); ?> <input type="checkbox" id="show_category" name="show_category" value="yes" />
        </label>
    </div><?php
}
add_action( 'category_add_form_fields', 'mytheme_add_meta_fields', 10, 2 );

// Edit term page
function mytheme_edit_meta_fields( $term, $taxonomy ) {
    $show_category = get_term_meta( $term->term_id, 'show_category', true ); ?>

    <tr class="form-field term-group-wrap">
        <th scope="row">
            <label for="show_category"><?php _e( 'Show Category', 'codilight-lite' ); ?></label>
        </th>
        <td>
            <input type="checkbox" id="show_category" name="show_category" value="yes" <?php echo ( $show_category ) ? checked( $show_category, 'yes' ) : ''; ?>/>
        </td>
    </tr><?php
}
add_action( 'category_edit_form_fields', 'mytheme_edit_meta_fields', 10, 2 );

// Save custom meta
function mytheme_save_custom_field( $term_id, $tag_id ) {
    if ( isset( $_POST[ 'show_category' ] ) ) {
        update_term_meta( $term_id, 'show_category', 'yes' );
    } else {
        update_term_meta( $term_id, 'show_category', '' );
    }
}
add_action( 'created_category', 'mytheme_save_custom_field', 10, 2 );
add_action( 'edited_category', 'mytheme_save_custom_field', 10, 2 );
  • Thank you for the feedback, I'll try to check the mytheme_get_custom_field function – Claudio King Oct 7 '17 at 11:24
  • If you are failure let me know, Give here code I will give you appropriate code, and update my answer. – Faysal Mahamud Oct 8 '17 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.