Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function set_interview_title( $value, $post_id ) {
- $label = 'INTERVIEW';
- $date = date("Ymd");
- $date = date("Ymd", strtotime($date));
- $uniqueid_length = 8;
- $uniqueid = crypt(uniqid(rand(),1));
- $uniqueid = strip_tags(stripslashes($uniqueid));
- $uniqueid = str_replace(".","",$uniqueid);
- $uniqueid = strrev(str_replace("/","",$uniqueid));
- $uniqueid = substr($uniqueid,0,$uniqueid_length);
- $uniqueid = strtoupper($uniqueid);
- if($value['post_type'] == 'interviews') {
- if( ( 'publish' == $_POST['post_status'] ) && ( 'publish' == $_POST['original_post_status'] ) ) {
- return;
- }
- if( ( 'publish' == $_POST['post_status'] ) && ( 'publish' != $_POST['original_post_status'] ) ) {
- $title = $label . ' - ' . $uniqueid . '-' . $date;
- $post_slug = sanitize_title_with_dashes ($title,'','save');
- $post_slugsan = sanitize_title($post_slug);
- $value['post_title'] = $title;
- $value['post_name'] = $post_slugsan;
- }
- if( ( 'draft' == $_POST['post_status'] ) || ( 'pending' == $_POST['post_status'] ) ) {
- $title = $label . ' - ' . $uniqueid . '-' . $date;
- $post_slug = sanitize_title_with_dashes ($title,'','save');
- $post_slugsan = sanitize_title($post_slug);
- $value['post_title'] = $title;
- $value['post_name'] = $post_slugsan;
- }
- } return $value;
- }
- add_filter( 'wp_insert_post_data' , 'set_interview_title' , '10', 2 );
- Thanks
- auto-update
- shareedit
- edited Aug 16 '15 at 19:19
- asked Aug 16 '15 at 19:13
- Fanding P. Njie
- 33
- add a comment
- 1 Answer
- active oldest votes
- up vote
- 0
- down vote
- accepted
- 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.
- function set_post_title( $post ) {
- global $current_user;
- if( get_post_type() != 'interviews' || $post->post_status != 'auto-draft' )
- return;
- $uniqueid_length = 8;
- $uniqueid = crypt(uniqid(rand(),1));
- $uniqueid = strip_tags(stripslashes($uniqueid));
- $uniqueid = str_replace(".","",$uniqueid);
- $uniqueid = strrev(str_replace("/","",$uniqueid));
- $uniqueid = substr($uniqueid,0,$uniqueid_length);
- $uniqueid = strtoupper($uniqueid);
- $title = 'INTERVIEW: ' . $current_user->first_name . ' ' . $current_user->last_name . ' - ' . $uniqueid . ' - ' . date( 'Ymd' );
- ?>
- <script type="text/javascript">
- jQuery(document).ready(function($) {
- $("#title").val("<?php echo $title; ?>");
- $("#title").prop("readonly", true); // Don't allow author/editor to adjust the title
- });
- </script>
- <?php
- } // set_post_title
- 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