Skip Navigation

[Gelöst] Need code to auto generate nice title

This support ticket is created vor 7 Jahren, 4 Monaten. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Dieses Thema enthält 5 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Brad Tipper vor 7 Jahren, 4 Monaten.

Assistiert von: Shane.

Author
Artikel
#539020
2017-06-20 22_11_35-Edit Enquiry ‹ Price My Ride — WordPress.png

I am trying to: get my posts to have generated titles, rather than "CRED Auto Draft" or whatever it says.

I found this code which works somewhat:

add_filter('title_save_pre', 'my_title');
function my_title($title) {
    $title = date('YmdHis');
    return $title;
}

EXCEPT it also renames all of my forms with the same structure when I update them.

I would also like to make the slug of saved posts a unique random number- how would I go about that?

Cheers in anticipation of your help.

#539139

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Brad,

Thank you for contacting our support forum.

Could you try the hook in this link below as well ?
https://wordpress.stackexchange.com/questions/105926/rewriting-post-slug-before-post-save

Please let me know if this helps.

Thanks,
Shane

#539333

Sorry, as well as what I already have? I don't see that that is going to stop the renaming of my CRED forms etc?

Also, it doesn't appear to give the posts a unique random number as a slug either?

Third point which I forgot to mention: this needs to happen only to specific post types (ie. those created with toolset)

#539867

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Brad,

The posts on the frontend using CRED are they created using a post title? or is the post title generated after the post is saved.

Please let me know.

Thanks,
Shane

#539910

Hi Shane,

The post title appears to be generated after the post is saved. There's no logical field to form the post title- although I guess the post date and one of the fields would be an acceptable title/slug.

How would I implement that?

Cheers,
Brad

#540083

Well, I ended up just getting it done myself anyway. Here's my code for anyone else who may need it:

//Better titles for forms
// =============================================================================
add_action("cred_save_data", "my_save_data_action",10,2);
function my_save_data_action($post_id, $form_data) {   
    // Enquiry form
    if ( $form_data["id"]==71 || $form_data["id"]==410 ) {
        if (isset($_POST["wpcf-vehicle-make"]) && isset($_POST["wpcf-vehicle-model"]) && isset($_POST["wpcf-year"]) ){
            //Get the regular post date
            $date_format = get_option('date_format');
            $date = get_the_date($date_format, $id);
            //Build the title & slug
            $title = $date." ".$_POST["wpcf-vehicle-make"]." ".$_POST["wpcf-vehicle-model"]." ".$_POST["wpcf-year"];
            $slug = sanitize_title($title);
            // Update the post into the database
            $my_post = array(
                'ID' => $post_id,
                'post_title' => $title,
                'post_name' => $slug
            );
            wp_update_post( $my_post ); 
        } 
    }
}