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
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;
}
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
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;
}
My issue is resolved now. Thank you!