0

A developer made a custom page for our wordpress site and I've added a custom field at the top titled Serial Number.

enter image description here

I want to add this field as a column to the admin page between Date Registered and Status. This field should say Yes if the radio button has been selected. Left blank if it has been unchecked.

enter image description here

This is the code that was created for that page:

// Add columns to the serials admin grid

function sha_wreg_columns_head( $defaults ) { global $sha_wreg_prefix; unset( $defaults['date'] ); $defaults[ $sha_wreg_prefix . 'system_name' ] = __('System Name', 'sha-wreg' ); $defaults[ $sha_wreg_prefix . 'generation_date'] = __('Date Generated', 'sha-wreg' ); $defaults[ $sha_wreg_prefix . 'register_date' ] = __('Date Registered', 'sha-wreg' ); $defaults[ $sha_wreg_prefix . 'status' ] = __('Status', 'sha-wreg' ); return $defaults; } add_filter('manage_' . $sha_wreg_module_slug . '_posts_columns', 'sha_wreg_columns_head');

// Make date columns sortable function sha_wreg_add_sortable_columns( $columns ) { global $sha_wreg_prefix;

$columns[ $sha_wreg_prefix . 'generation_date' ] = 'generation_date';
$columns[ $sha_wreg_prefix . 'register_date' ] = 'register_date';

return $columns;

} add_filter( 'manage_edit-' . $sha_wreg_module_slug . '_sortable_columns', 'sha_wreg_add_sortable_columns' );

// Add date sorting to query function sha_wreg_add_orderby_to_query( $query ) { global $sha_wreg_prefix;

if ( ! is_admin() )
    return;

$orderby = $query->get( 'orderby' );

if ( 'generation_date' == $orderby ) {
    $query->set( 'orderby', 'date' );
}

if ( 'register_date' == $orderby ) {
    $query->set( 'meta_key', $sha_wreg_prefix . 'register_date' );
    $query->set( 'orderby', 'meta_value_num' );
}

} add_action( 'pre_get_posts', 'sha_wreg_add_orderby_to_query' );

// Add columns to the PDFS admin grid function sha_wreg_pdfs_columns_head( $defaults ) { unset( $defaults['date'] ); $defaults['title'] = __('System', 'sha-wreg' ); $defaults['pdf'] = __('PDF User Manual', 'sha-wreg' ); return $defaults; } add_filter('manage_' . $sha_wreg_module_slug . '_pdfs_posts_columns', 'sha_wreg_pdfs_columns_head');

// Add column contents function sha_wreg_columns_content( $column_name, $post_ID ) { global $sha_wreg_prefix; $post_meta = get_post_meta( $post_ID );

if ($column_name == $sha_wreg_prefix . 'system_name' ) {
    if ( !empty( $post_meta[ $sha_wreg_prefix . 'system' ][0] ) ) {
        $system_data = unserialize( $post_meta[ $sha_wreg_prefix . 'system' ][0] );
        echo $system_data['name'];
    }
}

if ($column_name == $sha_wreg_prefix . 'register_date' ) {
    if ( !empty( $post_meta[ $sha_wreg_prefix . 'register_date' ][0] ) ) {
        echo date( 'd/m/Y', (int)$post_meta[ $sha_wreg_prefix . 'register_date' ][0] );
    }
}

if ($column_name == $sha_wreg_prefix . 'generation_date' ) {
    echo date( 'd/m/Y', strtotime( get_the_time('Y-m-d', $post_ID ) ) );
}

if ( $column_name == $sha_wreg_prefix . 'status' ) {
    if ( !empty( $post_meta[ $sha_wreg_prefix . 'status' ][0] ) ) {
        echo $post_meta[ $sha_wreg_prefix . 'status' ][0];
    }
}

if ( $column_name == 'pdf' ) {
    if ( !empty( $post_meta[ $sha_wreg_prefix . 'pdf_file' ][0] ) ) {
        echo $post_meta[ $sha_wreg_prefix . 'pdf_file' ][0];
    }
}

} add_action( 'manage_posts_custom_column', 'sha_wreg_columns_content', 10, 2);

// Add allocation fields to post type form function sha_wreg_add_allocation_metabox( $item_data ) {

global $sha_wreg_module_slug, $sha_wreg_prefix, $sha_wreg_plugin_dir;

// Get all existing serials
$args = array(
    'post_type'         => $sha_wreg_module_slug . '_pdfs',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
);

$systems = array(
    ''  => 'Select System'
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $systems[ get_the_ID() ] = trim( get_the_title() );
    }
}

$current_system = get_post_meta( $item_data->ID, $sha_wreg_prefix . 'system', true );

require_once $sha_wreg_plugin_dir . 'admin/templates/allocation_metabox.php';

}

// Add file field to PDFS to post type form function sha_wreg_add_file_metabox( $item_data ) {

global $sha_wreg_module_slug, $sha_wreg_prefix, $sha_wreg_plugin_dir;

$pdf_file = get_post_meta( $item_data->ID, $sha_wreg_prefix . 'pdf_file', true );

require_once $sha_wreg_plugin_dir . 'admin/templates/pdf_file_metabox.php';

}

// Add details to post type form function sha_wreg_add_details_metabox( $item_data ) {

global $sha_wreg_module_slug, $sha_wreg_prefix, $sha_wreg_plugin_dir;

$predefined_details = array(
    'tank_capacity'     => array(
        'name'          =>  __( 'System Tank Capacity', 'sha-wreg'),
        'value'         =>  ''
    ),
    'extra_opt1'        => array(
        'name'          =>  __( 'Optional Extra 1', 'sha-wreg'),
        'value'         =>  ''
    ),
    'extra_opt2'        => array(
        'name'          =>  __( 'Optional Extra 2', 'sha-wreg'),
        'value'         =>  ''
    ),
    'extra_opt3'        => array(
        'name'          =>  __( 'Optional Extra 3', 'sha-wreg'),
        'value'         =>  ''
    ),
    'date_manufactured' => array(
        'name'          =>  __( 'Date Manufactured', 'sha-wreg'),
        'value'         =>  ''
    ),
    'date_fitted'       => array(
        'name'          =>  __( 'Date Fitted', 'sha-wreg'),
        'value'         =>  ''
    ),
    'distributor'       => array(
        'name'          =>  __( 'Distributor', 'sha-wreg'),
        'value'         =>  ''
    ),
    'other_info'        => array(
        'name'          =>  __( 'Other Information', 'sha-wreg'),
        'value'         =>  ''
    )
);

$details = unserialize( get_post_meta( $item_data->ID, $sha_wreg_prefix . 'detail_data', true ) );
if ( count( $details ) > 0 ) {
    $details = array_merge( $predefined_details, $details );
} else {
    $details = $predefined_details;
}

$extra_details_keys = array();

foreach ( $details as $k => $detail ) {
    if ( !in_array( $k, array_keys( $predefined_details ) ) ) {
        $extra_details_keys[] = $k;
    }
}

require_once $sha_wreg_plugin_dir . 'admin/templates/details_metabox.php';

}

// Add checkboxes to post type form function sha_wreg_add_checkboxes_metabox( $item_data ) {

global $sha_wreg_module_slug, $sha_wreg_prefix, $sha_wreg_plugin_dir;

$checkboxes = unserialize( get_post_meta( $item_data->ID, $sha_wreg_prefix . 'checkbox_data', true ) );

require_once $sha_wreg_plugin_dir . 'admin/templates/checkboxes_metabox.php';

}

// Add user details to post type form function sha_wreg_add_user_metabox ( $item_data ) { global $sha_wreg_module_slug, $sha_wreg_prefix, $sha_wreg_plugin_dir;

$user_details = get_post_meta( $item_data->ID, $sha_wreg_prefix . 'user', true );
$key_mapping = array(
    'name' => 'User Name',
    'surname' => 'User Surname',
    'address' => 'User Address',
    'town' => 'User Town',
    'postcode' => 'User Postcode',
    'country' => 'User Coutry',
    'email' => 'User Email',
    'phone' => 'User phone',
    'id' => 'User ID'
);

require_once $sha_wreg_plugin_dir . 'admin/templates/user_metabox.php'; 

}

// Show meta boxes function sha_wreg_add_meta_fields() {

global $sha_wreg_module_slug;

add_meta_box(
    'sha-wreg-file-metbox',
    __( 'PDF file', 'sha-wreg' ),
    'sha_wreg_add_file_metabox',
    $sha_wreg_module_slug . '_pdfs',
    'normal',
    'high'
);

add_meta_box(
    'sha-wreg-checkboxes-metbox',
    __( 'System checkboxes', 'sha-wreg' ),
    'sha_wreg_add_checkboxes_metabox',
    $sha_wreg_module_slug . '_pdfs',
    'normal',
    'high'
);  


add_meta_box(
    'sha-wreg-allocation-metbox',
    __( 'System Allocation', 'sha-wreg' ),
    'sha_wreg_add_allocation_metabox',
    $sha_wreg_module_slug,
    'normal',
    'high'
);

add_meta_box(
    'sha-wreg-details-metbox',
    __( 'System details', 'sha-wreg' ),
    'sha_wreg_add_details_metabox',
    $sha_wreg_module_slug,
    'normal',
    'high'
);  

add_meta_box(
    'sha-wreg-user-metbox',
    __( 'User Data', 'sha-wreg' ),
    'sha_wreg_add_user_metabox',
    $sha_wreg_module_slug,
    'normal',
    'high'
);

} add_action( 'admin_init', 'sha_wreg_add_meta_fields' );

// Add enctype to form to allow download images add_action('post_edit_form_tag', 'sha_wreg_add_post_enctype');

function sha_wreg_add_post_enctype() { echo ' enctype="multipart/form-data"'; }

// Add Registered|Allocated quick filters add_filter( 'views_edit-' . $sha_wreg_module_slug, 'sha_wreg_add_quick_filters'); function sha_wreg_add_quick_filters( $views ){ global $sha_wreg_module_slug, $sha_wreg_prefix;

$registered = new WP_Query(
    array(
        'post_type'     => $sha_wreg_module_slug,
        'post_status'   => 'publish',
        'quick_filter'  => true,
        'meta_query'    => array(
            array(
                'key'       => $sha_wreg_prefix . 'status',
                'value'     => 'Registered',
                'compare'   => '='
            )
        )
    )
);
$registerd_total = $registered->found_posts;

$allocated = new WP_Query(
    array(
        'post_type'     => $sha_wreg_module_slug,
        'post_status'   => 'publish',
        'quick_filter'  => true,
        'meta_query'    => array(
            array(
                'key'       => $sha_wreg_prefix . 'status',
                'value'     => 'Allocated',
                'compare'   => '='
            )
        )
    )
);
$allocated_total = $allocated->found_posts;

// Remove Published and Mine quick filters
unset( $views['publish'] );
unset( $views['mine'] );
//unset( $views['trash'] );

$registered_css = ( $_GET['filter'] == 'registered' ) ? ' class="current"' : '';
$views['registered'] = sprintf(
    "<a href='edit.php?filter=registered&post_type=%s'%s>%s</a>",
    $sha_wreg_module_slug,
    $registered_css,
    sprintf(
        __( 'Registered (%d)', 'sha-wreg' ),
        $registerd_total
    )
);

$registered_css = ( $_GET['filter'] == 'allocated' ) ? ' class="current"' : '';
$views['allocated'] = sprintf(
    "<a href='edit.php?filter=allocated&post_type=%s'%s>%s</a>",
    $sha_wreg_module_slug,
    $registered_css,
    sprintf(
        __( 'Allocated (%d)', 'sha-wreg' ),
        $allocated_total
    )
);

return $views;

}

// Add filter var to query add_filter( 'query_vars', 'sha_wreg_query_vars' ); function sha_wreg_query_vars( $vars ) { $vars[] = 'filter';

return $vars;

}

// Add Registered|Allocated filters to query add_filter( 'request', 'sha_wreg_add_meta_query_to_quick_filter' ); function sha_wreg_add_meta_query_to_quick_filter( $vars ) { global $sha_wreg_prefix, $sha_wreg_module_slug;

// Skip, if not a correct post_type
if ( $vars['post_type'] != $sha_wreg_module_slug) {
    return $vars;
}

switch ( $vars['filter'] ) {
    case 'allocated':
        $vars = array_merge( $vars,
            array(
                'meta_query'    => array(
                    array(
                        'key'       => $sha_wreg_prefix . 'status',
                        'value'     => 'Allocated',
                        'compare'   => '='
                    )
                )
            )
        );
    break;

    case 'registered':
        $vars = array_merge( $vars,
            array(
                'meta_query'    => array(
                    array(
                        'key'       => $sha_wreg_prefix . 'status',
                        'value'     => 'Registered',
                        'compare'   => '='
                    )
                )
            )
        );
    break;
}

return $vars;

}

// Prevent add serial with the same number function sha_wreg_prevent_serial_duplication( $maybe_empty, $postarr ) { global $sha_wreg_module_slug; if ( ( $postarr['post_type'] == $sha_wreg_module_slug ) && ( !in_array( $postarr['post_status'], array( 'draft', 'auto-draft' ) ) ) && ( isset( $_POST['post_title'] ) ) ) {

    // Search for the card with same number and publish status
    $same_number = get_page_by_title( trim( $_POST['post_title'] ), ARRAY_A, $sha_wreg_module_slug );
    if ( isset( $same_number['post_status'] ) && ( $same_number['post_status'] == 'publish' ) && ( $_POST['post_ID'] != $same_number['ID'] ) ) {
        // Register notice
        $_SESSION[ $sha_wreg_module_slug ] = __( 'This serial is already exist', 'sha-wreg' );
        return true;
    }
}

return false;

} add_filter( 'wp_insert_post_empty_content', 'sha_wreg_prevent_serial_duplication', PHP_INT_MAX -1, 2 );

// Show admin notice function sha_wreg_replace_admin_messages( $messages ) { global $sha_wreg_module_slug;

$screen = get_current_screen();
if ( $screen->id == $sha_wreg_module_slug ) {
    if ( isset( $_SESSION[ $sha_wreg_module_slug ] ) ) {
        $messages['post'][6] = $_SESSION[ $sha_wreg_module_slug ];
        unset( $_SESSION[ $sha_wreg_module_slug ] );
    }
}
return $messages;

} add_filter( 'post_updated_messages', 'sha_wreg_replace_admin_messages', 10, 1 );

// Save/update meta fields add_action( 'save_post', 'sha_wreg_save_update', 10, 2 );

function sha_wreg_save_update( $item_id, $item_data ) { global $sha_wreg_module_slug, $sha_wreg_prefix, $sha_wreg_uploads_dir;

switch ( $item_data->post_type ) {

    case $sha_wreg_module_slug:
        // Update systems
        if ( isset( $_POST[ $sha_wreg_prefix . 'system' ] ) && !empty( $_POST[ $sha_wreg_prefix . 'system' ] ) ) {
            //Change status to allocating
            $system_data = get_post_meta( $item_id, $sha_wreg_prefix . 'system', true );
            $system_name = get_post( $_POST[ $sha_wreg_prefix . 'system' ] )->post_title;
            $system_object = array(
                'id'    => (int)$_POST[ $sha_wreg_prefix . 'system' ],
                'name'  => $system_name
            );

            if ( empty( $system_data ) ) {
                update_post_meta( $item_id, $sha_wreg_prefix . 'status', 'Allocated' );
            }
            update_post_meta( $item_id, $sha_wreg_prefix . 'system', $system_object );
        }

        //Remove empty rows
        foreach ( $_POST[ $sha_wreg_prefix . 'detail_data' ] as $k => $v ) {
            if ( empty( $v['name'] ) || ( empty( $v['name'] ) && empty( $v['value'] ) ) ) {
                unset( $_POST[ $sha_wreg_prefix . 'detail_data' ][ $k ] );
            }
        }

        // Update details
        $detail_data = get_post_meta( $item_id, $sha_wreg_prefix . 'detail_data', true );
        $detail_data = unserialize( $detail_data );
        if ( $detail_data !== $_POST[ $sha_wreg_prefix . 'detail_data' ] ) {
            update_post_meta( $item_id, $sha_wreg_prefix . 'detail_data', serialize( $_POST[ $sha_wreg_prefix . 'detail_data' ] ) );
        }

    break;

    case $sha_wreg_module_slug . '_pdfs':
        // Update PDF
        if ( isset( $_FILES[ $sha_wreg_prefix . 'pdf_file' ] ) && $_FILES[ $sha_wreg_prefix . 'pdf_file' ][ 'error' ] == 0 ) {
            $oldfile = get_post_meta( $item_id, $sha_wreg_prefix . 'pdf_file', true );
            $uploadfile = sprintf('%s/%s', $sha_wreg_uploads_dir,  $_FILES[ $sha_wreg_prefix . 'pdf_file' ]['name'] );

            //Copy PDF
            if ( move_uploaded_file( $_FILES[ $sha_wreg_prefix . 'pdf_file' ]['tmp_name'], $uploadfile ) ) {
                update_post_meta( $item_id, $sha_wreg_prefix . 'pdf_file', basename( $uploadfile ) );
            }

            //Remove old PDF
            if ( isset( $oldfile ) && !empty( $oldfile ) ) {
                if ( file_exists( sprintf( '%s/%s', $sha_wreg_uploads_dir, basename( $oldfile ) ) ) ) {
                    unlink( sprintf( '%s/%s', $sha_wreg_uploads_dir, basename( $oldfile ) ) );
                }
            }
        }

        // Update checkboxes
        $checkbox_data = get_post_meta( $item_id, $sha_wreg_prefix . 'checkbox_data', true );
        $checkbox_data = unserialize( $detail_data );
        if ( $checkbox_data !== $_POST[ $sha_wreg_prefix . 'checkbox_data' ] ) {
            update_post_meta( $item_id, $sha_wreg_prefix . 'checkbox_data', serialize( $_POST[ $sha_wreg_prefix . 'checkbox_data' ] ) );
        }

    break;
}

}

  • 1
    I can't even read that question. We cannot code for you here but help. If you want to DO something then you should TRY or Hire the one who can do. – Arsalan Mithani Oct 21 at 13:32
  • Please clean up the code you've provided and only include the part where you're trying to add the custom column, not where you're adding meta boxes. – WebElaine Oct 21 at 14:00

Your Answer

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