Hello, I have a few bits of advice for you. First and most important is to use the PHP function error_log. If you're not familiar with error_log, take a few minutes to research it and it will save you hours of time. You can use the error_log function to write out to a server log file as code is executing, and it's an invaluable tool for PHP development. You can write out a simple string or value directly using
...if the data you want to log is an array or object $foo, you should use print_r with error_log like so:
error_log(print_r($foo, true));
If you're not familiar with turning on server logs, I can show you how to turn them on temporarily in most common wp-config.php setups. Go in your wp-config.php file and look for
define('WP_DEBUG', false);
Change it to:
define('WP_DEBUG', true);
Then add these lines, just after the WP_DEBUG line:
define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt');
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);
Update wp-config.php on your server. Now you can use the error_log function to create and write to an error_log.txt file in the root directory of your site - the same directory that holds wp-config.php in most setups. Once you have that log up and running, use error_log() in your code to write to the logs as code is executed. When you're done, revert the changes you made in wp-config.php and you can delete the log file. I've added two error_log statements to your code below, which should help you see what's going on and where the problems lie:
//Calculate a publishing date from a starting date .
add_action('cred_save_data', 'calculate_publishing_date',10,2);
function calculate_publishing_date($post_id, $form_data) {
if ($form_data["id"]==3260)
{
error_log('Form 3260 has been submitted. The POST superglobal contains: ');
error_log(print_r($_POST, true));
if(isset($_POST['start_date'])){ // ['start_date'] comes from a datepicker custom field in the form
$start_date = new DateTime(date('Y-m-d', $_POST['start_date']));
}
if(isset($_POST['year-to-publish'])){ // ['year-to-publish'] comes from a dropdown menu custom field in the form
$number_of_years = $_POST['year-to-publish'];
}
$publishing_date = $start_date + $number_of_years * 365 * 24 * 60 * 60; // calculate the publishing date (here I'm on thin ice)
update_post_meta($post_id, 'wpcf-publishing-date', $publishing_date); // 'wpcf-publishing-date' is the new date to store in a custom field.
}
}
The two log statements should tell you when this Form has been submitted, and then give you a breakdown of all the information captured in the $_POST superglobal. Beyond that, some additional advice specific to your code here:
1. A Types custom datepicker field will use the 'wpcf-' prefix. So if the field slug in wp-admin is start_date, the field key in $_POST will be wpcf-start_date.
2. A Types datepicker field will be passed in the $_POST superglobal as an array of information, not a single value. To access the Unix timestamp of the datepicker field selection, you can do something like this in PHP:
$timestamp = $_POST['wpcf-start_date']['timestamp'];
3. Check out this stackoverflow post that discusses using strtotime to add time to a timestamp:
https://stackoverflow.com/questions/5172849/php-adding-years-to-a-timestamp
$newTimestamp = strtotime('+1 year', $timestamp);
No need for using DateTimes or any other conversions here, just timestamp manipulation.
Let me know if you get stuck.