1

I'm hoping to get some help in getting a checkbox in the Quick Edit of custom post type to work properly. It is intended to determine whether a product is featured on the front page of the site.

  • Post type: hub_product
  • Checkbox Field name: feature_meta_box_check
  • Checkbox checked values: on / off
  • Column name: feature_meta_box_check

At present, it 'works' as follows:

Click 'Quick Edit' when checkbox is checked:

  • Unchecking the checkbox then Update: State remains checked, database entry is 'on'.
  • Leaving the checkbox untouched just Update: State becomes unchecked, database entry is 'off'.

Click 'Quick Edit when checkbox is unchecked:

  • Checking the checkbox then Update: State becomes checked, database entry is 'on'.
  • Leaving the checkbox untouched just Update: State remains unchecked, database entry is 'off'.

Below is the PHP code that handles the creation of the column, the checkbox input element and saving to the database and then JS code that updates the checkbox value and checkbox state according to database entries upon clicking 'Quick Edit'.

In PHP, the current database value for the checkbox is held in a id of a div. The Javascript (which seems to work as expected) uses this to update the checkbox element upon clicking 'Quick Edit'. I suspect, (not being an expert) my problems relate to correctly determining the state of the checkbox in PHP upon clicking 'Update' in order to save the right data.

The class hide_column_value sets the div to 1px square with overflow hidden just so a capitalised version can be displayed. Two other fields are handled correctly, a text field and a dropdown list, the codes for them have been removed.

Can anyone see where I'm going wrong?

CODE PHP

add_filter( 'manage_posts_columns', 'manage_wp_posts_be_qe_manage_posts_columns', 10, 2 );
function manage_wp_posts_be_qe_manage_posts_columns( $columns, $post_type ) 
{
if ( $post_type == 'hub_product' ) {
    $columns[ 'feature_meta_box_check' ] = 'Front Page Feature';
}
return $columns;
}
add_action( 'manage_posts_custom_column', 'manage_wp_posts_be_qe_manage_posts_custom_column', 10, 2 );
function manage_wp_posts_be_qe_manage_posts_custom_column( $column_name, $post_id ) 
{
switch( $column_name ) 
{
case 'feature_meta_box_check':
        if ( !get_post_meta( $post_id, 'feature_meta_box_check', true ) ) 
        {
            $featured_value = 'off';
        }
        else
        {
            $featured_value = ( get_post_meta( $post_id, 'feature_meta_box_check', true ) == 'on' ) ? 'on' : 'off';
        }
        echo '<div class="hide_column_value" id="feature_meta_box_check-' . $post_id . '">' . $featured_value . '</div>' . ucfirst($featured_value);
break;
}
}

add_action( 'bulk_edit_custom_box', 'manage_wp_posts_be_qe_bulk_quick_edit_custom_box', 10, 2 );
add_action( 'quick_edit_custom_box', 'manage_wp_posts_be_qe_bulk_quick_edit_custom_box', 10, 2 );
function manage_wp_posts_be_qe_bulk_quick_edit_custom_box( $column_name, $post_type ) {

switch ( $post_type ) 
{

    case 'hub_product':
        switch( $column_name )
        {

            case 'feature_meta_box_check':
                ?>
                <fieldset class="inline-edit-col-right inline-edit-hub-product">
                  <div class="inline-edit-col inline-edit-<?php  echo $column_name ?>">
                    <label>
                      <span class="title">Feature on Front Page</span>
                      <input type="checkbox" name="feature_meta_box_check" value="">
                    </label>
                  </div>
                </fieldset>
                <?php
            break;
        }
    break;
}
}

add_action( 'save_post', 'manage_wp_posts_be_qe_save_post', 10, 2 );
function manage_wp_posts_be_qe_save_post( $post_id, $post ) {

// pointless if $_POST is empty (this happens on bulk edit)
if ( empty( $_POST ) )
    return $post_id;

// verify quick edit nonce
if ( isset( $_POST[ '_inline_edit' ] ) && ! wp_verify_nonce( $_POST[ '_inline_edit' ], 'inlineeditnonce' ) )
    return $post_id;

// don't save for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return $post_id;

// dont save for revisions
if ( isset( $post->post_type ) && $post->post_type == 'revision' )
    return $post_id;

switch( $post->post_type ) 
{
    case 'hub_product':
        $custom_fields = array( 'hub_product_url_box_text', 'feature_meta_box_check', 'design_meta_box_select' );
        foreach( $custom_fields as $field ) 
        {
            if ( array_key_exists( $field, $_POST ) )
            {
                if ( $field == 'feature_meta_box_check' )
                {
                    if ( !empty( $_POST['feature_meta_box_check'] ) )
                    {
                        update_post_meta( $post_id, $field, 'on' );
                    }
                    else
                    {
                        update_post_meta( $post_id, $field, 'off' );
                    }
                }
                else
                {
                    update_post_meta( $post_id, $field, $_POST[ $field ] );
                }
            }
        }
    break;
}
}

CODE JS

(function($) {

// we create a copy of the WP inline edit post function
var $wp_inline_edit = inlineEditPost.edit;

// and then we overwrite the function with our own code
inlineEditPost.edit = function( id ) {

    // "call" the original WP edit function
    // we don't want to leave WordPress hanging
    $wp_inline_edit.apply( this, arguments );

    // now we take care of our business

    // get the post ID
    var $post_id = 0;
    if ( typeof( id ) == 'object' )
        $post_id = parseInt( this.getId( id ) );

    if ( $post_id > 0 ) {

        // define the edit row
        var $edit_row = $( '#edit-' + $post_id );

        // get the feature meta box value
        var $feature_meta_box_value = $( '#feature_meta_box_check-' + $post_id ).text();
        // set the feature meta box value
        if ( $feature_meta_box_value == 'on' )
        {
            $edit_row.find( 'input[name="feature_meta_box_check"]' ).prop( 'checked', true );
        }
        else
        {
            $edit_row.find( 'input[name="feature_meta_box_check"]' ).val( $feature_meta_box_value );
            $edit_row.find( 'input[name="feature_meta_box_check"]' ).prop( 'checked', false );
        }
    }
};

})(jQuery);

Your Answer

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

Browse other questions tagged or ask your own question.