Skip Navigation

[Resolved] Altering the content of a custom field value in the admin posts screen

This support ticket is created 3 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Saul Baizman 3 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2272627

Hi there,

I have a custom post and on it a custom field we'll call Display. The Display field is a checkbox, and it holds a value of 1 when checked and 0 when not checked.

On the admin posts screen, the Display column appears and outputs either 1 or 0. What I would like to do is modify the output of the column. If the value of Display is 1, I want to output a checkmark icon. If the value of Display is 0, I want to output nothing.

I've read extensively about adding custom columns to custom post admin screens and populating their content and done this many times on many sites, but I'm having trouble modifying the existing value of a custom field. When I hook into manage_{$post_type}_posts_custom_column, I can only seem to print a second value in the column, but I can't replace the content. In other words, the output in the Display field is "1checkmark". The "1" is the value in the database, and "checkmark" represents the checkmark icon that I've coded.

Here's a snippet of the relevant code I'm using:

function car_resource_columns_output ( $column_name, $post_id ) {

	switch ( $column_name ) {
		case 'wpcf-display':

			$field_value = get_post_meta( $post_id , $column_name , true );
                        if ( $field_value == '1') {
                              printf ('<span class="dashicons dashicons-yes"></span>') ; // checkmark
                        }
            break;
      }
}
add_action ( 'manage_resource_posts_custom_column', 'car_resource_columns_output', 100, 2 ) ;

If "Display" is 1 is the database, the output in the admin posts screen is this:

1<span class="dashicons dashicons-yes"></span>

In Query Monitor, I can see that the manage_resource_posts_custom_column hook is being called twice: once by Toolset, and once by my code.

One solution might be to hide the "real" Display field and create my own field. But is there a proper way to just modify the field value?

Thank you!

Saul

#2273661

Hi Saul,

Thank you for contacting us and I'd be happy to assist.

To achieve this, I'll suggest the following steps:

1. From WP Admin -> Toolset -> Post Types open the edit screen for the "Resource" post type settings.

2. From the section "Post Fields to be shown as columns in Post Type listing in WordPress Admin", uncheck the option for the "Display" checkbox field, so that Toolset Types doesn't show its column in the admin posts screen.

3. In the last step, you can include the following code above your existing code from the message to programmatically add the column for this checkbox field:


add_filter('manage_resource_posts_columns', function($columns) {
	return array_merge($columns, ['wpcf-display' => __('Display', 'textdomain')]);
});

This column will not include the field's value and will only include the custom HTML.

regards,
Waqar

#2274085

Thanks, Waqar! This is what I thought had to be done. 🙂