首页 » Wordpress »

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

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

本文标签:

相关文章推荐

转载请保留,转载自:老牛博客
原文地址 http://xiaohost.com/2419.html

发表评论