Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate a post title and slug based on the content
<?php
/**
* Generate a post title and slug from the content
*/
function dk_generate_title_and_slug( $data ) {
if( 'post' == $data['post_type'] && empty( $data['post_title'] ) ) {
// Quick way to strip media (could do something like use media type to inform title)
$title = wp_trim_excerpt( $data['post_content'] );
// Limit generated excerpt to 10 words instead of 55 by wp_trim_excerpt (could go with character length)
$title = wp_trim_words( $title, 10, '' );
// Set the title and slug
$data['post_title'] = $title;
$data['post_name'] = sanitize_title( $title );
}
return $data;
}
add_filter( 'wp_insert_post_data', 'dk_generate_title_and_slug' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.