0

I have a code that is driving me very crazy and frustrated, I wonder if there will be any help for a newbie like me. There where solutions for something like it before but they are not enough.

I want to be able to auto generate custom post titles with the author first name, last name, a random number and current date. The problem is, it works only when I press publish. But not when I save as draft without publishing finally. I want to be able to generate on draft and when publish it will still maintain the generated title instead of updating each time.

my code is here:


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

0

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
  • Thanks for your attempt. I have tried your suggested solution nut it still gives me the same result. When I publish and then change status to draft and publish again, it doesn't carry through the initial unique number assigned on the inception. So I need to have the unique number assigned from the beginning to the end of the post's life. – Wally Aug 17 '15 at 12:35
  • OK, I adjusted the code so that it only runs if it is a new post... if( get_post_type() != 'interviews' || $post->post_status != 'auto-draft' ) return; Should be ok now but will obviously only apply to brand new posts – Mike Aug 17 '15 at 12:54
  • Hi Mike, you're a magician. that worked like an Nubian charm. You're the Boss. Thanks. – Wally Aug 17 '15 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.