Advertisement
Beee

acf/save_post

Sep 19th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. // UPDATE: this code is working. Just FYI if you come across this link through https://support.advancedcustomfields.com/forums/topic/save-edited-post-as-pending-add-acf-field-title/
  2.  
  3. function change_post_status() {
  4.  
  5.     if ( isset($_GET['ad']) ) {
  6.         $post_id = $_GET['ad'];
  7.     } else {
  8.         $post_id = $post_id;
  9.     }
  10.  
  11.     $site_url = site_url();
  12.  
  13.     global $wpdb;
  14.     if ( ! $post = get_post( $post_id ) ) {
  15.         return;
  16.     }
  17.     if ( 'pending' == $post->post_status ) {
  18.         return;
  19.     } else { // is post != pending
  20.         $entered_title  = $_POST['acf']['field_57dec9df66a5b'];
  21.         $cleaned_title  = preg_replace('/[^A-Za-z0-9\-\s]/', '', $entered_title); // Removes special chars.
  22.  
  23.         // Update DB with pending status.
  24.         $wpdb->update( $wpdb->posts, array( 'post_status' => 'pending', 'post_title' => $cleaned_title ), array( 'ID' => $post_id ) );
  25.         update_field('field_57dec9df66a5b', $cleaned_title, $post_id);
  26.  
  27.         // send email
  28.         $to         = get_option('admin_email');
  29.         $subject    = 'Published ad is edited';
  30.         $headers[]  = 'From: DVP Portal <support@dvp.nl>';
  31.         $headers[]  = 'Content-Type: text/html; charset=UTF-8';
  32.         $message    = '<a href="'. $site_url .'/wp-admin/post.php?post='. $post_id .'&action=edit">Klik hier voor de ad</a>';
  33.         wp_mail( $to, $subject, $message, $headers );
  34.  
  35.         // clean the cache for the post
  36.         clean_post_cache( $post->ID );
  37.  
  38.         // Trigger all transition hooks in wordpress (in case other functions hook into those to do things.
  39.         $old_status         = $post->post_status;
  40.         $post->post_status  = 'pending';
  41.         wp_transition_post_status( 'publish', $old_status, $post );
  42.  
  43.     }
  44.  
  45. }
  46. add_action('acf/save_post', 'change_post_status', 20);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement