Last active
October 25, 2017 01:08
-
-
Save ziemekpr0/c9c981045623eed57d04115bc185f363 to your computer and use it in GitHub Desktop.
WordPress snippet - its a simple plugin which instead of sending e-mail, saves given post data as a post content. In this stage it is not dynamic and require some preparations in order to make it work.:)
This file contains 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: Post Form | |
* Description: Umożliwia dodanie wysłanego formularza jako postu w kokpicie do przeglądu. | |
* Version: 1.0 | |
* Author: ziemekpr0 | |
* Author Email: ziemekpr0@gmail.com | |
* Author URI: http://wpadmin.pl | |
* License: GPLv2 or later | |
*/ | |
/* Security check - block direct access */ | |
if (!defined('ABSPATH')) exit('No direct script access allowed'); | |
/* Musli Social Links */ | |
class Post_Form | |
{ | |
public function __construct() | |
{ | |
// Do not send me those data again in e-mail | |
add_filter('wpcf7_skip_mail', array($this, 'skip_mail'), 10, 2); | |
// Send me notification | |
// Get CF7 submissions | |
add_action('wpcf7_before_send_mail', array($this, 'save_submission_as_post')); | |
} | |
public function skip_mail($skip_mail, $contact_form) | |
{ | |
if($contact_form->id == 235) | |
return true; | |
else | |
return $skip_mail; | |
} | |
// ------------------------------------------------------------------- | |
public function save_submission_as_post() | |
{ | |
$cf7_instance = WPCF7_Submission::get_instance(); | |
$submission = $cf7_instance->get_posted_data(); | |
/* | |
print_r($submission); | |
>> OUTPUT: | |
Array | |
( | |
[_wpcf7] => 1960 | |
[_wpcf7_version] => 4.4.2 | |
[_wpcf7_locale] => pl_PL | |
[_wpcf7_unit_tag] => wpcf7-f1960-p1961-o1 | |
[your-name] => Andrzej | |
[your-email] => dasdsa@aa.pl | |
[your-subject] => Jakiś temat | |
[your-message] => Treść wpisu | |
[_wpcf7_is_ajax_call] => 1 | |
)*/ | |
/** | |
* Optional: Create new users account | |
*/ | |
// Let's check if the given e-mail exists | |
// if so in $is_email_exists we will get that users ID, else | |
// we have to do some other stuff | |
$email = $submission['your-email']; | |
$is_email_exists = email_exists($email); | |
if($is_email_exists === FALSE) | |
{ | |
// create a unique username | |
if(username_exists($submission['your-name'])) | |
{ | |
$x = 2; | |
while( username_exists($submission['your-name'] . $x) ) | |
{ | |
$x++; | |
} | |
$user_name = $submission['your-name'] . $x; | |
} | |
else | |
{ | |
$user_name = $submission['your-name']; | |
} | |
// lastly create a new password for that user | |
$random_password = wp_generate_password(); | |
// finally create new user | |
$user_id = wp_create_user( | |
$user_name, | |
$random_password, | |
$submission['your-email'] | |
); | |
} | |
else | |
{ | |
$user_id = $is_email_exists; | |
} | |
/** | |
* Create and save post data | |
*/ | |
$post_data = array( | |
'post_type' => 'post', | |
'post_status' => 'draft', | |
'post_title' => $submission['your-subject'], | |
'post_content' => $submission['your-message'], | |
'post_category' => array(13), | |
'post_author' => $user_id //remove if you do not want to create user accounts | |
); | |
wp_insert_post($post_data); | |
} | |
// [contact-form-7 id="1960" title="Dodaj wpis"] | |
// $skip_mail = apply_filters( 'wpcf7_skip_mail', $skip_mail, $contact_form ); | |
} | |
new Post_Form; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. I just needed a chunk from the above code.