• 1.
    <?php
  • 2.
    /**
  • 3.
     * Template Name: Create Post2
  • 4.
     *
  • 5.
     * @author Mike Hemberger
  • 6.
     * @link http://thestizmedia.com/front-end-posting-with-acf-pro/
  • 7.
     * @uses Advanced Custom Fields Pro
  • 8.
     */
  • 9.
     
  • 10.
    /**
  • 11.
     * Add required acf_form_head() function to head of page
  • 12.
     * @uses Advanced Custom Fields Pro
  • 13.
     */
  • 14.
    add_action( 'get_header', 'tsm_do_acf_form_head', 1 );
  • 15.
    function tsm_do_acf_form_head() {
  • 16.
    	// Bail if not logged in or not able to post
  • 17.
    	if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  • 18.
    		return;
  • 19.
    	}
  • 20.
    	acf_form_head();
  • 21.
    }
  • 22.
     
  • 23.
    /**
  • 24.
     * Deregister the admin styles outputted when using acf_form
  • 25.
     */
  • 26.
    add_action( 'wp_print_styles', 'tsm_deregister_admin_styles', 999 );
  • 27.
    function tsm_deregister_admin_styles() {
  • 28.
    	// Bail if not logged in or not able to post
  • 29.
    	if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  • 30.
    		return;
  • 31.
    	}
  • 32.
    	wp_deregister_style( 'wp-admin' );
  • 33.
    }
  • 34.
     
  • 35.
     
  • 36.
    /**
  • 37.
     * Add ACF form for front end posting
  • 38.
     * @uses Advanced Custom Fields Pro
  • 39.
     */
  • 40.
    add_action( 'genesis_entry_content', 'tsm_do_create_post_form' );
  • 41.
    function tsm_do_create_post_form() {
  • 42.
     
  • 43.
    	// Bail if not logged in or able to post
  • 44.
    	if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) {
  • 45.
    		echo '<p>You must be a registered author to post.</p>';
  • 46.
    		return;
  • 47.
    	}
  • 48.
     
  • 49.
    	$new_post = array(
  • 50.
    		'post_id'            => 'new', // Create a new post
  • 51.
    		// PUT IN YOUR OWN FIELD GROUP ID(s)
  • 52.
    		'field_groups'       => array(group_xxxxxxxxx), // Create post field group ID(s)
  • 53.
    		'form'               => true,
  • 54.
    		'return'             => '%post_url%', // Redirect to new post url
  • 55.
    		'html_before_fields' => '',
  • 56.
    		'html_after_fields'  => '',
  • 57.
    		'submit_value'       => 'Submit Post',
  • 58.
    		'updated_message'    => 'Saved!'
  • 59.
    	);
  • 60.
    	acf_form( $new_post );
  • 61.
     
  • 62.
    }
  • 63.
     
  • 64.
    /**
  • 65.
     * Back-end creation of new candidate post
  • 66.
     * @uses Advanced Custom Fields Pro
  • 67.
     */
  • 68.
    add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
  • 69.
    function tsm_do_pre_save_post( $post_id ) {
  • 70.
     
  • 71.
    	// Bail if not logged in or not able to post
  • 72.
    	if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  • 73.
    		return;
  • 74.
    	}
  • 75.
     
  • 76.
    	// check if this is to be a new post
  • 77.
    	if( $post_id != 'new' ) {
  • 78.
    		return $post_id;
  • 79.
    	}
  • 80.
     
  • 81.
    	// Create a new post
  • 82.
    	$post = array(
  • 83.
    		'post_type'     => 'post', // Your post type ( post, page, custom post type )
  • 84.
    		'post_status'   => 'publish', // (publish, draft, private, etc.)
  • 85.
    		'post_title'    => wp_strip_all_tags($_POST['acf']['field_xxxxxxxx']), // Post Title ACF field key
  • 86.
     
  • 87.
    	);
  • 88.
     
  • 89.
    	// insert the post
  • 90.
    	$post_id = wp_insert_post( $post );
  • 91.
     
  • 92.
    	// Save the fields to the post
  • 93.
    	do_action( 'acf/save_post' , $post_id );
  • 94.
     
  • 95.
    	return $post_id;
  • 96.
     
  • 97.
    }
  • 98.
     
  • 99.
    /**
  • 100.
     * Save ACF image field to post Featured Image
  • 101.
     * @uses Advanced Custom Fields Pro
  • 102.
     */
  • 103.
    add_action( 'acf/save_post', 'tsm_save_image_field_to_featured_image', 10 );
  • 104.
    function tsm_save_image_field_to_featured_image( $post_id ) {
  • 105.
     
  • 106.
    	// Bail if not logged in or not able to post
  • 107.
    	if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  • 108.
    		return;
  • 109.
    	}
  • 110.
     
  • 111.
    	// Bail early if no ACF data
  • 112.
    	if( empty($_POST['acf']) ) {
  • 113.
    		return;
  • 114.
    	}
  • 115.
     
  • 116.
    	// ACF image field key
  • 117.
    	$image = $_POST['acf']['field_54dfcd4278d63'];
  • 118.
     
  • 119.
    	// Bail if image field is empty
  • 120.
    	if ( empty($image) ) {
  • 121.
    		return;
  • 122.
    	}
  • 123.
     
  • 124.
    	// Add the value which is the image ID to the _thumbnail_id meta data for the current post
  • 125.
    	add_post_meta( $post_id, '_thumbnail_id', $image );
  • 126.
     
  • 127.
    }
  • 128.
     
  • 129.
    // acf/update_value/name={$field_name} - filter for a specific field based on it's key
  • 130.
     
  • 131.
     
  • 132.
    //* Run the Genesis loop
  • 133.
    genesis();
<?php /** * Template Name: Create Post2 * * @author Mike Hemberger * @link http://thestizmedia.com/front-end-posting-with-acf-pro/ * @uses Advanced Custom Fields Pro */ /** * Add required acf_form_head() function to head of page * @uses Advanced Custom Fields Pro */ add_action( 'get_header', 'tsm_do_acf_form_head', 1 ); function tsm_do_acf_form_head() { // Bail if not logged in or not able to post if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) { return; } acf_form_head(); } /** * Deregister the admin styles outputted when using acf_form */ add_action( 'wp_print_styles', 'tsm_deregister_admin_styles', 999 ); function tsm_deregister_admin_styles() { // Bail if not logged in or not able to post if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) { return; } 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() { // Bail if not logged in or able to post if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) { echo '<p>You must be a registered author to post.</p>'; return; } $new_post = array( 'post_id' => 'new', // Create a new post // PUT IN YOUR OWN FIELD GROUP ID(s) 'field_groups' => array(group_xxxxxxxxx), // Create post field group ID(s) 'form' => true, 'return' => '%post_url%', // Redirect to new post url 'html_before_fields' => '', 'html_after_fields' => '', 'submit_value' => 'Submit Post', 'updated_message' => 'Saved!' ); acf_form( $new_post ); } /** * Back-end creation of new candidate post * @uses Advanced Custom Fields Pro */ add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' ); function tsm_do_pre_save_post( $post_id ) { // Bail if not logged in or not able to post 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; } // Create a new post $post = array( 'post_type' => 'post', // Your post type ( post, page, custom post type ) 'post_status' => 'publish', // (publish, draft, private, etc.) 'post_title' => wp_strip_all_tags($_POST['acf']['field_xxxxxxxx']), // Post Title ACF field key ); // insert the post $post_id = wp_insert_post( $post ); // Save the fields to the post do_action( 'acf/save_post' , $post_id ); return $post_id; } /** * Save ACF image field to post Featured Image * @uses Advanced Custom Fields Pro */ 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 ); } // acf/update_value/name={$field_name} - filter for a specific field based on it's key //* Run the Genesis loop genesis();

Copy Code to Clipboard Raw

Sign-in or Create a Free Account.

Snippi uses Twitter or Facebook to register and create accounts. Registration will enable you to keep track of your snippets. We never spam your timeline or wall. Ever.

Sign-in with Facebook Sign-in with Twitter