add_action( 'wpforms_post_submissions_process', 'set_post_category_from_dropdown', 10, 4 ); /* signature: ($post_id, $fields, $form_data, $entry_id) – docs :contentReference[oaicite:0]{index=0} */ function set_post_category_from_dropdown( $post_id, $fields, $form_data, $entry_id ) { if ( (int) $form_data['id'] !== 10540 ) { return; // wrong form – bail out } // ---- get the user’s choice (field ID 1) ---- if ( empty( $fields[1]['value_raw'] ) ) { return; } $choice = trim( $fields[1]['value_raw'] ); // ---- map label → term-ID ---- $map = [ 'Event' => 16, 'Position' => 17, ]; if ( empty( $map[ $choice ] ) ) { return; } $term_id = (int) $map[ $choice ]; // ---- merge with any existing cats, drop “Uncategorized” (ID 1) ---- $current = wp_get_object_terms( $post_id, 'category', [ 'fields' => 'ids' ] ); $new = array_unique( array_merge( array_diff( $current, [ 1 ] ), [ $term_id ] ) ); wp_set_object_terms( $post_id, $new, 'category', false ); // replace list } add_action( 'wpforms_post_submissions_process', 'wpf_attach_pdf_and_link', 20, 4 ); function wpf_attach_pdf_and_link( $post_id, $fields, $form_data, $entry_id ) { /* ───── Local config ───── */ $form_id = 10540; // your form’s ID $pdf_field_id = 4; // File-Upload field ID $debug_to = [ 'istefanou1@gmail.com', 'dmriley993@gmail.com' ]; $debug_on = false; // flip to true for mail debug $dest_rel = '/data/posts/'; // folder under site root /* helper */ $debug = function ( $subject, $data = '' ) use ( $debug_to, $debug_on ) { if ( ! $debug_on ) { return; } $body = ( is_array( $data ) || is_object( $data ) ) ? print_r( $data, true ) : (string) $data; foreach ( $debug_to as $addr ) { wp_mail( $addr, '[WPForms DBG] ' . $subject, $body ); } }; /* 1. right form? */ if ( (int) $form_data['id'] !== $form_id ) { return; } $debug( 'Hook fired – post_id', $post_id ); /* 2. did they upload a PDF? */ if ( empty( $fields[ $pdf_field_id ]['value_raw'] ) ) { $debug( 'No PDF uploaded' ); return; } $items = (array) $fields[ $pdf_field_id ]['value_raw']; $debug( 'Raw items', $items ); /* prepare destination paths */ $site_root = rtrim( ABSPATH, '/' ); $dest_dir = $site_root . $dest_rel; $dest_url = home_url( $dest_rel ); if ( ! is_dir( $dest_dir ) && ! wp_mkdir_p( $dest_dir ) ) { $debug( 'Could not create destination folder', $dest_dir ); return; } $link_block = []; foreach ( $items as $item ) { /* 3-a extract the URL no matter the format */ $src_url = is_array( $item ) ? ( $item['value'] ?? '' ) : $item; $src_url = esc_url_raw( $src_url ); if ( ! $src_url ) { $debug( 'Skipped – empty URL', $item ); continue; } /* 3-b translate URL → source path */ $filename = wp_basename( $src_url ); $src_path = str_replace( home_url(), $site_root, $src_url ); $dest_path = $dest_dir . $filename; $final_url = trailingslashit( $dest_url ) . $filename; /* 3-c move (rename) – fall back to copy if cross-partition */ if ( @rename( $src_path, $dest_path ) ) { $debug( 'Moved file', [ 'from' => $src_path, 'to' => $dest_path ] ); } elseif ( copy( $src_path, $dest_path ) ) { @unlink( $src_path ); $debug( 'Copied file (rename failed)', [ 'from' => $src_path, 'to' => $dest_path ] ); } else { $debug( 'Move/copy failed – giving up on this file', $src_path ); continue; } /* 3-d delete entry at media */ $attachment = get_posts( [ 'post_type' => 'attachment', 'meta_query' => [ [ 'key' => '_wp_attached_file', 'value' => $filename, 'compare' => 'LIKE', ], ], 'numberposts' => 1, ] ); if ( ! empty( $attachment ) ) { $attachment_id = $attachment[0]->ID; // Delete it wp_delete_attachment( $attachment_id, true ); } /* 3-e build the sentence with only “[PDF]” linked */ $link_block[] = sprintf( '
For further information, see the attached [PDF].
', esc_url( $final_url ) ); } if ( ! $link_block ) { $debug( 'Nothing to append' ); return; } /* 4. append to post content */ $current = get_post_field( 'post_content', $post_id ); $new = $current . "\n\n" . implode( "\n", $link_block ); $ret = wp_update_post( [ 'ID' => $post_id, 'post_content' => $new ], true ); $debug( 'Content append result', $ret ); }