wordpress 文章发布或修改时自动从内容截取设置文章标题

我的需求是,发布文章时,如果标题没有设置,则从内容里截取

一开始,网上找到一个插件,于是我把核心复制了出来,自定义修改了一下,但是在wordpress app上发布文章没有效果,

https://cn.wordpress.org/plugins/autotitle-for-wordpress/

/**
 * 自动标题
 * @return void
 */
function auto_title()
{

    global $_POST;

    if (!empty($_POST)) {

            if ($_POST['post_title'] == '') $_POST['post_title'] = wp_trim_words($_POST['content'], 16, '');

    }


}

add_action('init', 'auto_title', 10);

下面这个在wordpress app上也有效果

/**
 * @param $post
 * @return void
 */
function auto_title($post)
{

    if (!empty($post)) {

        if ($post['post_title'] == '') $post['post_title'] = wp_trim_words($post['post_content'], 16, '');

        return $post;
    }

}

add_filter('wp_insert_post_data', 'auto_title', 10);

参考:https://blog.csdn.net/getyouwant/article/details/81775809

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注




Enter Captcha Here :