我已经创建了一个联系人表单(这里有你们的很多帮助),我希望能够通过一个短代码(用于边栏等)在页面和文本小部件中使用它。它在这两种情况下都能很好地工作,除了在提交表单时Illegal string offset
仅以侧栏小部件的形式。然而,页面表单工作正常。我试图做的是从表单中提取“name”字段,以便在表单成功提交时回显用户名。引发警告的代码行如下:
echo \'<p>Thank you for contacting us \'.$_POST[\'cf-name\'].\', a member of our team will be in touch with you shortly.</p>\';
我还尝试了:
echo \'<p>Thank you for contacting us \'.(isset($_POST[ \'cf-name\' ])).\', a member of our team will be in touch with you shortly.</p>\';
使用第二个示例可以删除警告,但在表单子提交上都不显示用户名,并且后一个选项在工作之前在页面联系人表单中显示1。有没有办法解决这个问题?
全部功能是:
function my_form_message()
{
global $errors;
if (is_wp_error($errors) && empty($errors->errors)) {
echo \'<section class="alertbox-success">\';
echo \'<div class="cf-success">\';
echo \'<p>Thank you for contacting us \'.$_POST[\'cf-name\'].\', a member of our team will be in touch with you shortly.</p>\';
echo \'</div>\';
echo \'</section>\';
//Empty $_POST because we already sent email
$_POST = \'\';
} else {
if (is_wp_error($errors) && !empty($errors->errors)) {
$error_messages = $errors->get_error_messages();
foreach ($error_messages as $k => $message) {
echo \'<section class="alertbox-error">\';
echo \'<div class="cf-error \'.$k.\'">\';
echo \'<p>\'.$message.\'</p>\';
echo \'</div>\';
echo \'</section>\';
}
}
}
}
不知道这是否有用。任何想法都将不胜感激。提前感谢
编辑:这是完整警告:
/Applications/MAMP/htdocs/centenary-framework/wp-content/themes/cent_framework/assets/inc/core/contact-form.php on line 113
Notice: Uninitialized string offset: 0 in /Applications/MAMP/htdocs/centenary-framework/wp-content/themes/cent_framework/assets/inc/core/contact-form.php on line 113
Thank you for contacting us , a member of our team will be in touch with you shortly.
编辑:
完整代码:
<?php
// Form markup
function cf_form_code()
{
?>
<div class="cf-contact-form">
<form action="<?php esc_url($_SERVER[\'REQUEST_URI\']);
?>" method="post">
<p>
<input type="text" name="cf-name" placeholder="Name (required)" pattern="[a-zA-Z0-9 ]+" value="<?php isset($_POST[\'cf-name\']) ? esc_attr($_POST[\'cf-name\']) : \'\';
?><?php if (isset($_POST[\'cf-name\'])) {
echo htmlentities($_POST[\'cf-name\']);
}
?>" size="40" />
</p>
<p>
<input type="email" name="cf-email" placeholder="Email (required)" value="<?php isset($_POST[\'cf-email\']) ? esc_attr($_POST[\'cf-email\']) : \'\';
?><?php if (isset($_POST[\'cf-email\'])) {
echo htmlentities($_POST[\'cf-email\']);
}
?>" size="40" />
</p>
<p>
<input type="tel" name="cf-tel" placeholder="Telephone" pattern="^\\s*\\(?(020[7,8]{1}\\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\\s*$" value="<?php isset($_POST[\'cf-tel\']) ? esc_attr($_POST[\'cf-tel\']) : \'\';
?><?php if (isset($_POST[\'cf-tel\'])) {
echo htmlentities($_POST[\'cf-tel\']);
}
?>" size="40" />
</p>
<p>
<textarea class="cf-message" rows="10" cols="35" name="cf-message" placeholder="Message (required)" onkeyup="adjust_textarea(this)"><?php isset($_POST[\'cf-message\']) ? esc_attr($_POST[\'cf-message\']) : \'\';
?><?php if (isset($_POST[\'cf-message\'])) {
echo htmlentities($_POST[\'cf-message\']);
}
?></textarea>
</p>
<p><input type="text" name="content" id="content" value="" class="hpot" /></p>
<p><input type="submit" name="cf-submitted" value="Send"/></p>
</form>
</div>
<?php
}
// Form validation
function cf_validate_form()
{
$errors = new WP_Error();
if (isset($_POST[ \'content\' ]) && $_POST[ \'content\' ] !== \'\') {
$errors->add(\'bot\', \'Sorry, this field should not be filled in. Robots only\');
}
if (isset($_POST[ \'cf-name\' ]) && $_POST[ \'cf-name\' ] == \'\') {
$errors->add(\'name_error\', \'Please fill in a valid name.\');
}
if (isset($_POST[ \'cf-email\' ]) && $_POST[ \'cf-email\' ] == \'\') {
$errors->add(\'email_error\', \'Please fill in a valid email.\');
}
if (isset($_POST[ \'cf-message\' ]) && $_POST[ \'cf-message\' ] == \'\') {
$errors->add(\'message_error\', \'Please fill in a valid message.\');
}
return $errors;
}
// Form delivery
function deliver_mail($args = array())
{
// This $default array is a way to initialize some default values that will be overwritten by our $args array.
// We could add more keys as we see fit and it\'s a nice way to see what parameter we are using in our function.
// It will only be overwritten with the values of our $args array if the keys are present in $args.
// This uses WP wp_parse_args() function.
$defaults = array(
\'name\' => \'\',
\'email\' => \'\',
\'tel\' => \'\',
\'message\' => \'\',
\'to\' => get_option(\'admin_email\'), // get the administrator\'s email address
);
$args = wp_parse_args($args, $defaults);
$headers = "From: {$args[\'name\']} <{$args[\'email\']}>"."\\r\\n";
// Send email returns true on success, false otherwise
if (wp_mail($args[\'to\'], $args[\'tel\'], $args[\'message\'], $headers)) {
return;
} else {
return false;
}
}
// Form sanitize
function cf_sanitize_field($input)
{
return trim(stripslashes(sanitize_text_field($input)));
}
// Form succsess message
function cf_form_message()
{
global $errors;
if (is_wp_error($errors) && empty($errors->errors)) {
echo \'<section class="alertbox-success">\';
echo \'<div class="cf-success">\';
echo \'<p>Thank you for contacting us \'.$_POST[\'cf-name\'].\', a member of our team will be in touch with you shortly.</p>\';
echo \'</div>\';
echo \'</section>\';
//Empty $_POST because we already sent email
$_POST = \'\';
} else {
if (is_wp_error($errors) && !empty($errors->errors)) {
$error_messages = $errors->get_error_messages();
foreach ($error_messages as $k => $message) {
echo \'<section class="alertbox-error">\';
echo \'<div class="cf-error \'.$k.\'">\';
echo \'<p>\'.$message.\'</p>\';
echo \'</div>\';
echo \'</section>\';
}
}
}
}
// Form shortcode
add_shortcode(\'contact_form\', \'cf_contact_form\');
function cf_contact_form()
{
ob_start();
cf_form_message();
cf_form_code();
return ob_get_clean();
}
// Error validation
add_action(\'init\', \'cf_cf_form\');
function cf_cf_form()
{
if (isset($_POST[\'cf-submitted\'])) {
global $errors;
$errors = cf_validate_form();
if (empty($errors->errors)) {
$args = array(
\'name\' => cf_sanitize_field($_POST[\'cf-name\']),
\'email\' => cf_sanitize_field($_POST[\'cf-email\']),
\'tel\' => cf_sanitize_field($_POST[\'cf-tel\']),
\'message\' => cf_sanitize_field($_POST[\'cf-message\']),
);
deliver_mail($args);
} else {
return $errors;
}
}
}