Skip Navigation

[Resolved] How to update Types default value for custom field programatically?

This thread is resolved. Here is a description of the problem and solution.

Problem:

I have a CPT called Member_Profile with a field called "expiration-date" which defaults to the following year when a new member signs up, since membership is good for one year. All memberships expire on 31-Dec-yyyy. This field hold only yyyy. I have to update this field manually in the Types Post Field Group every December. I am trying to write a PHP function or filter to do this automatically but not sure how to do it.

Solution:

Yes, it needs custom codes, for example, you can try the filter hook "wpt_field_options", like this:

https://toolset.com/forums/topic/how-to-update-types-default-value-for-custom-field-programatically/#post-1155214

Relevant Documentation:

This support ticket is created 5 years, 11 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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by jeffS-2 5 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#1155168
Screen Shot 2018-11-28 at 7.27.07 PM.png

Tell us what you are trying to do?
I have a CPT called Member_Profile with a field called "expiration-date" which defaults to the following year when a new member signs up, since membership is good for one year. All memberships expire on 31-Dec-yyyy. This field hold only yyyy. I have to update this field manually in the Types Post Field Group every December. I am trying to write a PHP function or filter to do this automatically but not sure how to do it. I am PHP-savvy.

Is there any documentation that you are following?
The Custom Fields documentation does not mention this for default values.

What is the link to your site?
hidden link but since my question is in the backend, this link will not help you.

Is there a way to use Conditional Display in Advanced text mode to accomplish this?
Or, perhaps I could use JQuery, but I don't know how to access the value in the default field.

Thanks!
Jeff

#1155214

Dear Jeff,

Yes, it needs custom codes, for example, you can try the filter hook "wpt_field_options", like this:

add_filter( 'wpt_field_options', 'dynamic_select_func', 10, 3);
 
function dynamic_select_func( $options, $title, $type ){        
    switch( $title ){        
        case 'Expiration date': 
			$this_year = date('Y');
			$options = array();
			for($i=0; $i<10; $i++){
				$options[] = array(
					'#value' => $this_year + $i, 
					'#title' => $this_year + $i,
				);
			}
			break;
    }
    return $options;
}
#1155215

Luo -

Thanks very much for this code. I was hoping for you to point me in the right direction, so thank you for the whole thing!
I can understand how this code works. However, I do not see how it can set the "default" radio button.

Thanks,
Jeff Safire

#1155229

Above codes won't be able to setup the "default" value, there isn't such a existed filter hook for it, it will use the first option as the default value by default.

So in your case, you can move next year as the fist option, for example, modify the PHP codes as below:

add_filter( 'wpt_field_options', 'dynamic_select', 10, 3);
 
function dynamic_select( $options, $title, $type ){        
    switch( $title ){        
        case 'Expiration date': 
			$this_year = date('Y');
			$options = array();
			$i = 1;
			$options = array(
				array( // next year
					'#value' => $this_year + $i, 
					'#title' => $this_year + $i,
				),
				array( // this year
					'#value' => $this_year, 
					'#title' => $this_year,
				),
			);
			for($i; $i<9; $i++){ // other years
				$options[] = array(
					'#value' => $this_year + $i, 
					'#title' => $this_year + $i,
				);
			}
			break;
    }
    return $options;
}
#1158195

My issue is resolved now. Thank you!