Advertisement
itsmereal

Advanced Custom Field on Front-end

Apr 3rd, 2016
5,317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.15 KB | None | 0 0
  1. // =====================================================================================
  2. // UPDATE: A better method for both frontend submission and editing is available here
  3. // https://itsmereal.com/frontend-post-submission-edit-advanced-custom-fields/
  4. // =====================================================================================
  5.  
  6.  
  7. // ======================================================================================
  8. // Codes to put on your page template header
  9. // Remember, the above piece of code needs to be placed between <?php and get_header();
  10. // ======================================================================================
  11.  
  12. **
  13.  * Add required acf_form_head() function to head of page
  14.  * @uses Advanced Custom Fields Pro
  15.  */
  16. add_action( 'get_header', 'tsm_do_acf_form_head', 1 );
  17. function tsm_do_acf_form_head() {
  18.     // Bail if not logged in or not able to post
  19.     if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  20.         return;
  21.     }
  22.     acf_form_head();
  23. }
  24.  
  25. /**
  26.  * Deregister the admin styles outputted when using acf_form
  27.  */
  28. add_action( 'wp_print_styles', 'tsm_deregister_admin_styles', 999 );
  29. function tsm_deregister_admin_styles() {
  30.     // Bail if not logged in or not able to post
  31.     if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  32.         return;
  33.     }
  34.     wp_deregister_style( 'wp-admin' );
  35. }
  36.  
  37. // ======================================================================================
  38. // Codes to put on your content body area
  39. // ======================================================================================
  40.  
  41.  
  42. // Bail if not logged in or able to post
  43.     if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) {
  44.         echo '<p>You must be a registered author to post.</p>';
  45.         return;
  46.     }
  47.  
  48.     $new_post = array(
  49.         'post_id'            => 'new', // Create a new post
  50.         // PUT IN YOUR OWN FIELD GROUP ID(s)
  51.         'field_groups'       => array(7,58), // Create post field group ID(s)
  52.         'form'               => true,
  53.         'return'             => '%post_url%', // Redirect to new post url
  54.         'html_before_fields' => '',
  55.         'html_after_fields'  => '',
  56.         'submit_value'       => 'Submit Post',
  57.         'updated_message'    => 'Saved!'
  58.     );
  59.     acf_form( $new_post );
  60.  
  61.  
  62. // ======================================================================================
  63. // Codes to put on your functions.php file
  64. // ======================================================================================
  65.  
  66.  
  67. /**
  68.  * Back-end creation of new candidate post
  69.  * @uses Advanced Custom Fields Pro
  70.  */
  71. add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
  72. function tsm_do_pre_save_post( $post_id ) {
  73.  
  74.     // Bail if not logged in or not able to post
  75.     if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  76.         return;
  77.     }
  78.  
  79.     // check if this is to be a new post
  80.     if( $post_id != 'new' ) {
  81.         return $post_id;
  82.     }
  83.  
  84.     // Create a new post
  85.     $post = array(
  86.         'post_type'     => 'post', // Your post type ( post, page, custom post type )
  87.         'post_status'   => 'publish', // (publish, draft, private, etc.)
  88.         'post_title'    => wp_strip_all_tags($_POST['acf']['field_54dfc93e35ec4']), // Post Title ACF field key
  89.         'post_content'  => $_POST['acf']['field_54dfc94e35ec5'], // Post Content ACF field key
  90.     );
  91.  
  92.     // insert the post
  93.     $post_id = wp_insert_post( $post );
  94.  
  95.     // Save the fields to the post
  96.     do_action( 'acf/save_post' , $post_id );
  97.  
  98.     return $post_id;
  99.  
  100. }
  101.  
  102. /**
  103.  * Save ACF image field to post Featured Image
  104.  * @uses Advanced Custom Fields Pro
  105.  */
  106. add_action( 'acf/save_post', 'tsm_save_image_field_to_featured_image', 10 );
  107. function tsm_save_image_field_to_featured_image( $post_id ) {
  108.  
  109.     // Bail if not logged in or not able to post
  110.     if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  111.         return;
  112.     }
  113.  
  114.     // Bail early if no ACF data
  115.     if( empty($_POST['acf']) ) {
  116.         return;
  117.     }
  118.  
  119.     // ACF image field key
  120.     $image = $_POST['acf']['field_54dfcd4278d63'];
  121.  
  122.     // Bail if image field is empty
  123.     if ( empty($image) ) {
  124.         return;
  125.     }
  126.  
  127.     // Add the value which is the image ID to the _thumbnail_id meta data for the current post
  128.     add_post_meta( $post_id, '_thumbnail_id', $image );
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement