我写了这个tid-bit代码,试图从我用联系表单7创建的时间表表单中获取数据,将数据保存为我想要的格式的csv,然后将其发送到指定的电子邮件中联系表格的“邮件”部分。不幸的是,我遇到了两个我似乎无法解决的问题。
似乎无法将CSV文档保存到我定义的目录中。或者根本无法保存。这是我的代码:
<?php
add_action( 'wpcf7_before_send_mail', 'add_form_as_attachment', 10, 1 );
function add_form_as_attachment(&$WPCF7_ContactForm) {
$current_user = wp_get_current_user();
$pay_rate = get_user_meta( $current_user->ID, 'pay-rate', true );
$list = array (
array( 'Employee Name:', $current_user->display_name),
array( 'Selected Week:', $pay_rate),
array( 'Weekday', 'Job Number', 'Labor Desc', 'Labor Code', 'Total Hours', 'Break Hours'),
array(
'Sunday',
$WPCF7_ContactForm->posted_data['job-number-sun'],
$WPCF7_ContactForm->posted_data['labor-desc-sun'],
$WPCF7_ContactForm->posted_data['labor-code-sun'],
$WPCF7_ContactForm->posted_data['total-hours-sun'],
$WPCF7_ContactForm->posted_data['break-hours-sun']
),array(
'Monday',
$WPCF7_ContactForm->posted_data['job-number-mon'],
$WPCF7_ContactForm->posted_data['labor-desc-mon'],
$WPCF7_ContactForm->posted_data['labor-code-mon'],
$WPCF7_ContactForm->posted_data['total-hours-mon'],
$WPCF7_ContactForm->posted_data['break-hours-mon']
),array(
'Tuesday',
$WPCF7_ContactForm->posted_data['job-number-tue'],
$WPCF7_ContactForm->posted_data['labor-desc-tue'],
$WPCF7_ContactForm->posted_data['labor-code-tue'],
$WPCF7_ContactForm->posted_data['total-hours-tue'],
$WPCF7_ContactForm->posted_data['break-hours-tue']
),array(
'Wednesday',
$WPCF7_ContactForm->posted_data['job-number-wed'],
$WPCF7_ContactForm->posted_data['labor-desc-wed'],
$WPCF7_ContactForm->posted_data['labor-code-wed'],
$WPCF7_ContactForm->posted_data['total-hours-wed'],
$WPCF7_ContactForm->posted_data['break-hours-wed']
),array(
'Thursday',
$WPCF7_ContactForm->posted_data['job-number-thu'],
$WPCF7_ContactForm->posted_data['labor-desc-thu'],
$WPCF7_ContactForm->posted_data['labor-code-thu'],
$WPCF7_ContactForm->posted_data['total-hours-thu'],
$WPCF7_ContactForm->posted_data['break-hours-thu']
),array(
'Friday',
$WPCF7_ContactForm->posted_data['job-number-fri'],
$WPCF7_ContactForm->posted_data['labor-desc-fri'],
$WPCF7_ContactForm->posted_data['labor-code-fri'],
$WPCF7_ContactForm->posted_data['total-hours-fri'],
$WPCF7_ContactForm->posted_data['break-hours-fri']
),array(
'Saturday',
$WPCF7_ContactForm->posted_data['job-number-sat'],
$WPCF7_ContactForm->posted_data['labor-desc-sat'],
$WPCF7_ContactForm->posted_data['labor-code-sat'],
$WPCF7_ContactForm->posted_data['total-hours-sat'],
$WPCF7_ContactForm->posted_data['break-hours-sat']
),array(
'Travel Mileage (Miles):',
$WPCF7_ContactForm->posted_data['mileage'],
'Travel Mileage (MPG):',
$WPCF7_ContactForm->posted_data['mpg']
),array(
'Remarks:',
$WPCF7_ContactForm->posted_data['remarks']
)
);
$debug_echo = 'Target Directory: ' . $target_directory . '\n\n';
$debug_echo .= 'WPCF7 Upload Directory: ' . wpcf7_upload_tmp_dir() . '\n\n';
$debug_echo .= 'Real Path: ' . realpath(dirname(__FILE__)) . '\n\n';
$debug_echo .= 'Target Directory Permissions: ' . substr(sprintf('%o', fileperms( $target_directory )), -4);
$target_directory = '/nas/content/live/lovettmechanic/wp-content/uploads/timesheets/timesheet.csv';
$fp = fopen( $target_directory , 'w');
debug_to_console( $debug_echo );
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}
function debug_to_console( $data ) {
$output = $data;
if ( is_array( $output ) )
$output = implode( ',', $output);
echo "<script>console.log( 'Debug Objects: " . $output . "' );</script>";
}
任何帮助都将受到赞赏。我的头靠在墙上试图弄清楚这一段时间。
== EDIT ==
我编辑了这个问题,以证明函数可能首先没有触发。或者至少echo
函数中的debug_to_console()
命令不是。然后它必须再次触发,因为如果出现语法错误,我将收到http 500错误。但是debug_to_console()
函数的行为并不像预期的那样。我认为这可能有助于找出问题。