Created
June 24, 2017 16:22
-
-
Save ikushlianski/f653ac0991abf8c343d777636280a499 to your computer and use it in GitHub Desktop.
Contact form that creates a wordpress post (translatable with Polylang)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Example Contact Form | |
Plugin URI: http://example.com | |
Description: Simple non-bloated WordPress Contact Form | |
Version: 1.0 | |
Author: Agbonghama Collins | |
Author URI: http://w3guy.com | |
Text Domain: cform | |
*/ | |
// POLYLANG STRINGS FOR THIS THEME (add the next line to the functions.php of the current theme) | |
// pll_register_string('Header Title', 'Your Name (required)'); | |
function html_form_code() { | |
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">'; | |
echo '<p>'; | |
echo pll__('Your Name (required)') . '<br />'; | |
echo '<input type="text" name="cf-name" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />'; | |
echo '</p>'; | |
echo '<p>'; | |
echo 'Your Email (required) <br />'; | |
echo '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />'; | |
echo '</p>'; | |
echo '<p>'; | |
echo 'Название заказа (required) <br />'; | |
echo '<input type="text" name="cf-subject" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" size="40" />'; | |
echo '</p>'; | |
echo '<p>'; | |
echo 'Your Message (required) <br />'; | |
echo '<textarea rows="10" cols="35" name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>'; | |
echo '</p>'; | |
echo '<p><input type="submit" name="cf-submitted" value="Send"/></p>'; | |
echo '</form>'; | |
} | |
function my_custom_redirect() { | |
if ( !empty( $_POST['cf-submitted']) && !empty( $_POST['cf-name']) && !empty( $_POST['cf-email']) && !empty( $_POST['cf-subject'] ) ) { | |
$newPost = wp_insert_post(array( | |
'post_title' => wp_strip_all_tags($_POST["cf-subject"]), | |
'post_status' => 'draft', | |
'post_category' => [50], | |
'post_content' => $_POST["cf-name"] . ' сделал заказ <b>'. htmlspecialchars($_POST["cf-subject"]) . '</b> и прислал следующее сообщение: <p><i>' . $_POST["cf-message"] . '</i></p><p> Ответить ему Вы можете по электронной почте <b>' . $_POST['cf-email'] . '</b></p>' | |
)); | |
if (function_exists(pll_set_post_language)) { | |
pll_set_post_language($newPost, 'en'); | |
} | |
wp_redirect( get_permalink( 49 ) ); | |
exit; | |
} | |
} | |
add_action ('wp_loaded', 'my_custom_redirect'); | |
// if the submit button is clicked, send the email | |
// if ( isset( $_POST['cf-submitted'] ) ) { | |
// wp_redirect( get_permalink( 211 ) ); exit; | |
// // sanitize form values | |
// $name = sanitize_text_field( $_POST["cf-name"] ); | |
// $email = sanitize_email( $_POST["cf-email"] ); | |
// $subject = sanitize_text_field( $_POST["cf-subject"] ); | |
// $message = esc_textarea( $_POST["cf-message"] ); | |
// | |
// // get the blog administrator's email address | |
// $to = get_option( 'admin_email' ); | |
// | |
// $headers = "From: $name <$email>" . "\r\n"; | |
// | |
// // If email has been process for sending, redirect to another page | |
// if ( wp_mail( $to, $subject, $message, $headers ) ) { | |
// echo '<div>'; | |
// echo '<p>Thanks for contacting me, expect a response soon.</p>'; | |
// echo '</div>'; | |
// } else { | |
// echo 'An unexpected error occurred'; | |
// } | |
// } | |
function cf_shortcode() { | |
ob_start(); | |
my_custom_redirect(); | |
html_form_code(); | |
return ob_get_clean(); | |
} | |
add_shortcode( 'sitepoint_contact_form', 'cf_shortcode' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment