Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davekellam/7afb9b9767c64dc6c3d9052ffb2ffe7b to your computer and use it in GitHub Desktop.
Save davekellam/7afb9b9767c64dc6c3d9052ffb2ffe7b to your computer and use it in GitHub Desktop.
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