Advertisement
idotherest34

pastebin for post title manipulation

Oct 23rd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. function set_interview_title( $value, $post_id ) {
  2.  
  3.  
  4. $label = 'INTERVIEW';
  5.  
  6. $date = date("Ymd");
  7. $date =  date("Ymd", strtotime($date));
  8.  
  9. $uniqueid_length = 8;
  10. $uniqueid = crypt(uniqid(rand(),1));
  11. $uniqueid = strip_tags(stripslashes($uniqueid));
  12. $uniqueid = str_replace(".","",$uniqueid);
  13. $uniqueid = strrev(str_replace("/","",$uniqueid));
  14. $uniqueid = substr($uniqueid,0,$uniqueid_length);
  15. $uniqueid = strtoupper($uniqueid);
  16. if($value['post_type'] == 'interviews') {
  17.   if( ( 'publish' == $_POST['post_status'] ) && ( 'publish' == $_POST['original_post_status'] ) ) {
  18. return;
  19.   }
  20. if( ( 'publish' == $_POST['post_status'] ) && ( 'publish' != $_POST['original_post_status'] ) ) {
  21. $title = $label . ' - ' . $uniqueid . '-' . $date;
  22. $post_slug = sanitize_title_with_dashes ($title,'','save');
  23. $post_slugsan = sanitize_title($post_slug);
  24. $value['post_title'] = $title;
  25. $value['post_name'] = $post_slugsan;
  26.   }
  27.   if( ( 'draft' == $_POST['post_status'] ) || ( 'pending' == $_POST['post_status'] ) ) {
  28.       $title = $label . ' - ' . $uniqueid . '-' . $date;
  29. $post_slug = sanitize_title_with_dashes ($title,'','save');
  30. $post_slugsan = sanitize_title($post_slug);
  31. $value['post_title'] = $title;
  32. $value['post_name'] = $post_slugsan;
  33. }
  34. } return $value;
  35. }
  36. add_filter( 'wp_insert_post_data' , 'set_interview_title' , '10', 2 );
  37.  
  38. Thanks
  39.  
  40. auto-update
  41. shareedit
  42. edited Aug 16 '15 at 19:19
  43. asked Aug 16 '15 at 19:13
  44.  
  45. Fanding P. Njie
  46. 33
  47. add a comment
  48. 1 Answer
  49. active oldest votes
  50. up vote
  51. 0
  52. down vote
  53. accepted
  54. Something like this should do the trick using JS to set the title on initial load of a new post. You will not need to differentiate between publish/draft etc. and you can let WordPress sanitize.
  55.  
  56. function set_post_title( $post ) {
  57.     global $current_user;
  58.  
  59.     if( get_post_type() != 'interviews' || $post->post_status != 'auto-draft' )
  60.         return;
  61.  
  62.     $uniqueid_length = 8;
  63.     $uniqueid = crypt(uniqid(rand(),1));
  64.     $uniqueid = strip_tags(stripslashes($uniqueid));
  65.     $uniqueid = str_replace(".","",$uniqueid);
  66.     $uniqueid = strrev(str_replace("/","",$uniqueid));
  67.     $uniqueid = substr($uniqueid,0,$uniqueid_length);
  68.     $uniqueid = strtoupper($uniqueid);
  69.  
  70.     $title = 'INTERVIEW: ' . $current_user->first_name . ' ' . $current_user->last_name . ' - ' . $uniqueid . ' - ' . date( 'Ymd' );
  71.  
  72.     ?>
  73.     <script type="text/javascript">
  74.     jQuery(document).ready(function($) {
  75.         $("#title").val("<?php echo $title; ?>");
  76.         $("#title").prop("readonly", true); // Don't allow author/editor to adjust the title
  77.     });
  78.     </script>
  79.     <?php
  80. } // set_post_title
  81. add_action( 'edit_form_after_title', 'set_post_title' ); // Set the post title for Custom posts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement