Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. function biwp_contributor_name($post_id) {
  2. $corporate = get_field('biwp_corporate', $post_id ); //get Corporate checkbox value
  3. if ($corporate) { //if Corporate is checked, use biwp_cname (Corportate Name)
  4. $name = get_field('biwp_cname', $post_id );
  5. } else { //if Corporate is unchecked, use biwp_gnames (Given Names) and biwp_snames (Surnames)
  6. $gnames = get_field('biwp_gnames', $post_id );
  7. $snames = get_field('biwp_snames', $post_id );
  8. $name = $gnames .' '. $snames;
  9. }
  10. return $name;
  11. }
  12.  
  13. add_filter('acf/update_value', 'biwp_fix_contributor_title', 10, 3);
  14.  
  15. function biwp_fix_contributor_title( $value, $post_id, $field ) {
  16. if (get_post_type($post_id) == 'contributor') {
  17.  
  18. $name = biwp_contributor_name($post_id);
  19. $new_title = $name;
  20. $new_slug = sanitize_title( $new_title );
  21.  
  22. // update post
  23. $biwp_contributor = array(
  24. 'ID' => $post_id,
  25. 'post_title' => $new_title,
  26. 'post_name' => $new_slug,
  27. );
  28.  
  29. if ( ! wp_is_post_revision( $post_id ) ){
  30. // unhook this function so it doesn't loop infinitely
  31. remove_action('save_post', 'biwp_fix_contributor_title');
  32. // update the post, which calls save_post again
  33. wp_update_post( $biwp_contributor );
  34. // re-hook this function
  35. add_action('save_post', 'biwp_fix_contributor_title');
  36. }
  37.  
  38. }
  39.  
  40. return $value;
  41. }
  42.  
  43. add_action('save_post', 'biwp_fix_contributor_title', 12);
  44.  
  45. function biwp_fix_contributor_title ($post_id) {
  46. if ( $post_id == null || empty($_POST) )
  47. return;
  48.  
  49. if ( !isset( $_POST['post_type'] ) || $_POST['post_type']!='contributor' )
  50. return;
  51.  
  52. if ( wp_is_post_revision( $post_id ) )
  53. $post_id = wp_is_post_revision( $post_id );
  54.  
  55. global $post;
  56. if ( empty( $post ) )
  57. $post = get_post($post_id);
  58.  
  59. if ($_POST['biwp_corporate']!='') {
  60. global $wpdb;
  61. $name = biwp_contributor_name($post_id);
  62. $where = array( 'ID' => $post_id );
  63. $wpdb->update( $wpdb->posts, array( 'post_title' => $name ), $where );
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement