Skip to content

Instantly share code, notes, and snippets.

@JacquesJahnichen
Last active March 15, 2016 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacquesJahnichen/28d83ae8b5976545c088 to your computer and use it in GitHub Desktop.
Save JacquesJahnichen/28d83ae8b5976545c088 to your computer and use it in GitHub Desktop.
ACF Pro - Front end form to record post or custom post
<?php
/**
* Template Name: Event-form-front-post
* Description: Front end form to allow user to publish post or custom post
*/
/**
* Add default actions to the head of page
*/
add_action( 'get_header', 'tsm_do_logged_in_author_checks' );
function tsm_do_logged_in_author_checks() {
// Add required acf_form_head() function to head of page
add_action( 'get_header', 'tsm_do_acf_form_head', 1 );
// Deregister the admin styles outputted when using acf_form
add_action( 'wp_print_styles', 'tsm_deregister_admin_styles', 999 );
// Add the acf_form
add_action( 'genesis_entry_content', 'tsm_do_create_post_form' );
// Set default categories
add_filter( 'acf/load_value/key=field_54dfccb977a11', 'vivi_load_post_category');
}
// Add required acf_form_head() function to head of page
function tsm_do_acf_form_head() {
acf_form_head();
}
// Deregister the admin styles outputted when using acf_form
function tsm_deregister_admin_styles() {
wp_deregister_style( 'wp-admin' );
}
/**
* Add ACF form for front end posting
* @uses Advanced Custom Fields Pro
*/
add_action( 'genesis_entry_content', 'tsm_do_create_post_form' );
function tsm_do_create_post_form() {
// User have to be connected or have to be the rights to publish post
// Use Profile builder shortcode to display login form
if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) {
echo '<h5>You have to be connected to post an Event</h5>';
echo do_shortcode( '[wppb-login] ' );
return;
}
// Create Form with redirection to the new post
$new_post = array(
'post_id' => 'new', // Créer un nouveau post
'field_groups' => array('group_54dfc93792c38', 'group_56afa466906e1', 'group_56afa466906e', 'group_569a815466906e', 'group_56ab8b66906ecc', 'group_566906e906e1'), // List ACF Pro group ID(s), en bas de chaque groupe
'form' => true,
'return' => '%post_url%', // Redirect to the new post
'html_before_fields' => '',
'html_after_fields' => '',
'submit_value' => 'Poster',
'updated_message' => 'Saved!'
);
acf_form( $new_post );
}
// Set défault categorie
function vivi_load_post_category($field) {
$term_ids = array(38,799);
return $term_ids;
}
/**
* Create new post - Update/create fields
* @uses Advanced Custom Fields Pro
*/
add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
function tsm_do_pre_save_post( $post_id ) {
// New test to avoir checkout
if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
return;
}
// check if this is to be a new post
if( $post_id != 'new' ) {
return $post_id;
}
// Custom code to create title with categories (For SEO)
$post_title_city_category;
if ( !empty($_POST['acf']['field_54dfccb977a11']) ) {
$taxonomy = array('espresso_event_categories');
$args = array('include' => $_POST['acf']['field_5678cb977a11']);
$ee_terms = get_terms($taxonomy, $args);
foreach($ee_terms as $term){
$post_title_city_category .= $term->name . ' ';
}
}
// Concat categories with title
$post_title_city_category .= $_POST['acf']['field786c93e35ec4'];
// Create default Post
$post = array(
'post_type' => 'espresso_events', // Your post type ( post, page, custom post type )
'post_status' => 'publish', // (publish, draft, private, etc.)
'post_title' => wp_strip_all_tags($post_title_city_category), // Post Title ACF field key
'post_content' => stripslashes($_POST['acf']['field_5478654e35ec5']), // Post Content ACF field key
);
// Insert the default post
$post_id = wp_insert_post( $post );
// Save the fields to the post
do_action( 'acf/save_post' , $post_id );
// Update title (To have categories only in URL)
$post_update = get_post($post_id);
$post_update->post_title = stripslashes(wp_strip_all_tags($_POST['acf']['field_5434535ec4']));
wp_update_post($post_update);
//Custom functions to manage EE4 Venue (You can create others functions)
_form_event_venue($post_id);
//Custom functions to manage EE4 organizers (You can create others functions)
_form_event_people($post_id);
// Email to admin and user
_form_email_confirmation($post_id);
return $post_id;
}
// Function venue - to Insert or to ask the admin to create one
function _form_event_venue($post_id){
// Insert venues with function
if ( !empty($_POST['acf']['field_56a95678693b']) ) {
_insert_new_venue_to_event($post_id,$_POST['acf']['field_56afa47893b']);
}
// Email to Admin to create venues
if ( !empty($_POST['acf']['field_56487886a14']) ) {
// Picture
$imageurl = 'Pas d image';
if ( !empty($_POST['acf']['field_56af154686aee']) ) {
$imageurl = wp_get_attachment_url($_POST['acf']['field_56afa564786aee']);
}
$subject = 'New venue to create';
$body = '
<html>
<body>
<p>Un atelier a été créé sur yoursite. Il est nécessaire de créer la Venue</p>
<strong>Titre : </strong>'.$_POST['acf']['field_56af1566a14'].'<br />
<strong>Description : </strong> ' . $_POST['acf']['field_5eza4846686a81'].'<br />
<strong>Atelier : </strong>' . get_permalink($post_id) .'<br />
<strong>Image : </strong>' . $imageurl .'<br />
<br />
<strong>Ask by : </strong>' . wp_get_current_user()->first_name .' ' . wp_get_current_user()->last_name .'<br />
<strong>His email : </strong>' . wp_get_current_user()->user_email .'<br /><br />
</body>
</html>';
// send email
_send_yoursite_email(false, $subject, $body);
}
}
// Function venue - to Insert in your database
function _insert_new_venue_to_event($new_event_id,$new_venue_id){
global $wpdb;
$cols_n_values = array(
'EVT_ID'=>$new_event_id,
'VNU_ID'=>$new_venue_id,
'EVV_primary'=>true
);
$datatypes = array(
'%d',//EVT_ID
'%d',//VNU_ID
'%d',//EVT_primary
);
$success = $wpdb->insert($wpdb->prefix."esp_event_venue",$cols_n_values,$datatypes);
return $wpdb->insert_id;
}
// Function organizers - Use EE4 people admin add-on - Insert or ask the admin to create one
function _form_event_people($post_id){
// People event - Add people 1
if ( !empty($_POST['acf']['field_56a15s4ee868']) ) {
_insert_new_people_to_event($post_id,$_POST['acf']['field_56a15s4ee868'],'33');
}
// People event - Add people 2
if ( !empty($_POST['acf']['field_565afa56e8d5']) ) {
_insert_new_people_to_event($post_id,$_POST['acf']['field_565afa56e8d5'],'33');
}
// People event - Add organizer 1
if ( !empty($_POST['acf']['field_56afa46aee6cb01']) ) {
_insert_new_people_to_event($post_id,$_POST['acf']['field_56afa46aee6cb01'],'36');
}
// Email to create organizers
if ( !empty($_POST['acf']['field_56af6cb01fea1b']) ) {
// Picture
$imageurlpers = "Pas d'image";
if ( !empty($_POST['acf']['field_56cb03c1f1f']) ) {
$imageurlpers = wp_get_attachment_url($_POST['acf']['field_56cb03c1f1f']);
}
$subject = 'New people to create';
$body = '
<html>
<body>
<p>Un atelier a été créé sur yoursite. Il est nécessaire de créer la personne (People) et de l associer à cet événement</p>
<strong>Nom et prénom: </strong>'.$_POST['acf']['field_56af03c11b'].'<br />
<strong>Email : </strong>'.$_POST['acf']['field_5603c103c1286'].'<br />
<strong>Description : </strong> ' . $_POST['acf']['field_56af03c1aeea88'].'<br />
<strong>Atelier : </strong>' . get_permalink($post_id) .'<br />
<strong>Image : </strong>' . $imageurlpers .'<br /><br />
<strong>Ask by : </strong>' . wp_get_current_user()->first_name . ' ' . wp_get_current_user()->last_name .'<br />
<strong>His email : </strong>' . wp_get_current_user()->user_email .'<br /><br />
</body>
</html>';
// send email
_send_yoursite_email(false, $subject, $body);
}
}
// Email - Global email for the admin and for the user
function _form_email_confirmation($post_id){
// Email for Admin
$subject = 'Atelier posté | ' . wp_strip_all_tags($_POST['acf']['field_54dfc93e35ec4']);
$body = '
<html>
<body>
<p>Un atelier a été posté sur yoursite. </p>
<strong>Thème : </strong>' .$_POST['acf']['field_54dfc93e35ec4'].'<br />
<strong>Description : </strong> ' . $_POST['acf']['field_54dfc94e35ec5'].'<br />
<strong>Atelier : </strong>' . get_permalink($post_id) .'<br />
<strong>Téléphone : </strong>' . $_POST['acf']['field_56ab8bb5104b4']. '<br />
<strong>Informations complémentaires : </strong>' . $_POST['acf']['field_56ab8bb51058d']. '<br />
<strong>Informations tickets : </strong>' . $_POST['acf']['field_56ab8bb51036d']. '<br />
<br />
<strong>Ask by : </strong>' . wp_get_current_user()->first_name . ' ' . wp_get_current_user()->last_name .'<br />
<strong>His email : </strong>' . wp_get_current_user()->user_email .'<br /><br />
</body>
</html>';
// send email
_send_yoursite_email(false, $subject, $body);
// Email for the user
$user_email = wp_get_current_user()->user_email;
$to = $user_email;
$subject = 'Your new event | ' . wp_strip_all_tags($_POST['acf']['field_54dfc93e35ec4']);
$body = '
<html>
<body>
<p>Hello,</p>
<p>Your new event ! </p>
<ul>'. get_permalink($post_id) .'</ul>
</body>
</html>';
// send email
_send_yoursite_email($to, $subject, $body);
}
// Email template
function _send_yoursite_email($to, $subject, $body){
/* I personnaly use Post Mark tool to send clean email - But it possible to use the following code to improve
/* the strucutre
*/
$headers[] = "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers[] = "MIME-Version: 1.0\r\n";
$headers[] = 'From: Your site <contact@yoursite.com>';
// Default adresse to send email
if ($to == false ){
$to = 'contact@yoursite.com';
}
// Use wp function to send email
wp_mail($to, stripslashes($subject), stripslashes($body));
}
// Function to insert people - Use People Addon plugin
function _insert_new_people_to_event($new_event_id,$new_people_id,$new_people_type){
global $wpdb;
$cols_n_values = array(
'PER_ID'=>$new_people_id,
'OBJ_ID'=>$new_event_id,
'OBJ_type'=> 'Event', //Probleme sur Event qui ne se met pas sur la base et donc ne peut pas être retrouvé pour l'organisteur seule
'P2P_Order' => 0,
'PT_ID'=>$new_people_type //Formateur 33, organisateur 36
);
$datatypes = array(
'%d',//PER_ID
'%d',//OBJ_ID
'%s',//OBJ_type
'%d',//P2P_Order
'%d'//PT_ID
);
$success = $wpdb->insert($wpdb->prefix."esp_people_to_post",$cols_n_values,$datatypes);
return $wpdb->insert_id;
}
// Save ACF image field to post Featured Image
add_action( 'acf/save_post', 'tsm_save_image_field_to_featured_image', 10 );
function tsm_save_image_field_to_featured_image( $post_id ) {
// Bail if not logged in or not able to post
if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
return;
}
// Bail early if no ACF data
if( empty($_POST['acf']) ) {
return;
}
// ACF image field key
$image = $_POST['acf']['field_54dfcd4278d63'];
// Bail if image field is empty
if ( empty($image) ) {
return;
}
// Add the value which is the image ID to the _thumbnail_id meta data for the current post
add_post_meta( $post_id, '_thumbnail_id', $image );
}
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment