Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function my_pre_save_post( $post_id ) {
- if ( !empty( $_POST['acf']['field_57e3ed6c92ec1'] ) ) {
- // if profile name is entered
- $entered_title = $_POST['acf']['field_57e3ed6c92ea0'];
- $cleaned_title = preg_replace( '/[^A-Za-z0-9\-\s]/', '', $entered_title ); // Removes special chars.
- $post_name = sanitize_title( $cleaned_title );
- update_field( 'field_57e3ed6c92ea0', $cleaned_title, $post_id ); // update acf field
- // check if profile image has changed
- $submitted_photo = $_POST['acf']['field_57e3ed6c9321c']; // array
- $stored_photo = get_field( 'sd_profile_photo', $post_id );
- if ( $submitted_photo == $stored_photo ) {
- // do nothing
- } else {
- $send_mail = true;
- add_post_meta( $submitted_photo, '_image_pending', true );
- }
- if ( $submitted_photo ) {
- // link image to post
- global $wpdb;
- $wpdb->update(
- $wpdb->posts,
- array(
- 'post_parent' => $post_id,
- ),
- array(
- 'ID' => $submitted_photo
- )
- );
- }
- // update post status + post title (if needed)
- global $wpdb;
- $wpdb->update(
- $wpdb->posts,
- array(
- 'post_status' => 'pending',
- 'post_title' => $cleaned_title,
- 'post_name' => $post_name
- ),
- array(
- 'ID' => $post_id
- )
- );
- // check if new images are added
- $submitted_gallery = $_POST['acf']['field_57e3ed6c93236']; // array
- $stored_gallery = get_field( 'sd_gallery', $post_id );
- $gallery_id_array = array();
- if ( $stored_gallery ) {
- foreach ( $stored_gallery as $gallery_item ) {
- $gallery_id_array[] = $gallery_item['ID'];
- }
- }
- if ( $submitted_gallery ) {
- foreach ( $submitted_gallery as $gallery ) {
- // link image to post
- $wpdb->update(
- $wpdb->posts,
- array(
- 'post_parent' => $post_id,
- ),
- array(
- 'ID' => $gallery
- )
- );
- if ( in_array( $gallery, $gallery_id_array ) ) {
- // if it exists in the array, do nothing
- } else {
- // if it doesn't exist, update post meta
- add_post_meta( $gallery, '_image_pending', true );
- }
- }
- }
- if ( $send_mail == true ) {
- $from_email = get_field( 'sd_support_email', 'option' );
- $to = get_option( 'admin_email' );
- $subject = 'Ad waiting approval';
- $message = 'An add is submitted and is <a href="#">awaiting approval</a>.';
- $headers[] = 'From: Site <' . $from_email . '>';
- $headers[] = 'Content-Type: text/html; charset=UTF-8';
- wp_mail( $to, $subject, $message, $headers );
- }
- clean_post_cache( $post_id );
- // Trigger all transition hooks in wordpress (in case other functions hook into those to do things.
- $old_status = $post->post_status;
- $post->post_status = 'pending';
- wp_transition_post_status( 'publish', $old_status, $post );
- }
- // return the same ID
- return $post_id;
- }
- add_filter( 'acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
Advertisement
Add Comment
Please, Sign In to add comment