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

  • A+
所属分类:Wordpress

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进行了是否为空的判断,需要设置两个钩子动作的优先级

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: