Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Template Name: Test page wpis sam dodaj
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Twenty_Fifteen
  7.  *
  8.  */
  9.  
  10. get_header();
  11.  
  12. add_action( 'get_header', 'tsm_do_acf_form_head', 1 );
  13. function tsm_do_acf_form_head() {
  14.     // Bail if not logged in or not able to post
  15.     if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  16.         return;
  17.     }
  18.     acf_form_head();
  19. }
  20. /**
  21.  * Deregister the admin styles outputted when using acf_form
  22.  */
  23. add_action( 'wp_print_styles', 'tsm_deregister_admin_styles', 999 );
  24. function tsm_deregister_admin_styles() {
  25.     // Bail if not logged in or not able to post
  26.     if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  27.         return;
  28.     }
  29.     wp_deregister_style( 'wp-admin' );
  30. }
  31.  
  32.     // Bail if not logged in or able to post
  33.     if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) {
  34.         echo '<p>You must be a registered author to post.</p>';
  35.         return;
  36.     }
  37.     $new_post = array(
  38.         'post_id'            => 'new', // Create a new post
  39.         // PUT IN YOUR OWN FIELD GROUP ID(s)
  40.         'field_groups'       => array(42,39), // Create post field group ID(s)
  41.         'form'               => true,
  42.         'return'             => '%post_url%', // Redirect to new post url
  43.         'html_before_fields' => '',
  44.         'html_after_fields'  => '',
  45.         'submit_value'       => 'Submit Post',
  46.         'updated_message'    => 'Saved!'
  47.     );
  48.     acf_form( $new_post );
  49.  
  50. /**
  51.  * Back-end creation of new candidate post
  52.  * @uses Advanced Custom Fields Pro
  53.  */
  54. add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
  55. function tsm_do_pre_save_post( $post_id ) {
  56.     // Bail if not logged in or not able to post
  57.     if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  58.         return;
  59.     }
  60.     // check if this is to be a new post
  61.     if( $post_id != 'new' ) {
  62.         return $post_id;
  63.     }
  64.     // Create a new post
  65.     $post = array(
  66.         'post_type'     => 'post', // Your post type ( post, page, custom post type )
  67.         'post_status'   => 'publish', // (publish, draft, private, etc.)
  68.         'post_title'    => wp_strip_all_tags($_POST['acf']['field_54dfc93e35ec4']), // Post Title ACF field key
  69.         'post_content'  => $_POST['acf']['field_54dfc94e35ec5'], // Post Content ACF field key
  70.     );
  71.     // insert the post
  72.     $post_id = wp_insert_post( $post );
  73.     // Save the fields to the post
  74.     do_action( 'acf/save_post' , $post_id );
  75.     return $post_id;
  76. }
  77. /**
  78.  * Save ACF image field to post Featured Image
  79.  * @uses Advanced Custom Fields Pro
  80.  */
  81. add_action( 'acf/save_post', 'tsm_save_image_field_to_featured_image', 10 );
  82. function tsm_save_image_field_to_featured_image( $post_id ) {
  83.     // Bail if not logged in or not able to post
  84.     if ( ! ( is_user_logged_in() || current_user_can('publish_posts') ) ) {
  85.         return;
  86.     }
  87.     // Bail early if no ACF data
  88.     if( empty($_POST['acf']) ) {
  89.         return;
  90.     }
  91.     // ACF image field key
  92.     $image = $_POST['acf']['field_54dfcd4278d63'];
  93.     // Bail if image field is empty
  94.     if ( empty($image) ) {
  95.         return;
  96.     }
  97.     // Add the value which is the image ID to the _thumbnail_id meta data for the current post
  98.     add_post_meta( $post_id, '_thumbnail_id', $image );
  99. }
  100.  
  101.  get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement