Generate a post title and slug based on the content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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