how to update permalink and title with the values of custom fileds dynamically ?

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

Put the below code in the theme’s functions.php file.

add_filter( 'wp_insert_post_data', 'wpse_121035', 50, 2 );
function wpse_121035( $data, $postarr ) {
//Check for the post statuses you want to avoid
if ( !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = sanitize_title( $data['post_title'] );
}
return $data;
}

自动将自定义字段的值填充到title和permalink,注意检查如果对title进行了是否为空的判断,需要设置两个钩子动作的优先级

Leave a Comment