Wordpress插件函数,返回被保存的文章的字符数


wordpress plugin function that will return the character count of the post being saved

我需要有一个函数,将得到保存的帖子的内容,计数字符,包括标点符号和空格,并返回一个数字。

我可以使用$text = mb_strlen($text, "UTF-8");,但我不知道如何调用当前正在保存或更新的帖子i的内容。

我将通过使用add_action('save_post', 'char_count');运行该函数,我不知道如何在我的函数中获得保存的帖子的帖子内容,因此我可以运行计数脚本。

get_post()通过传递post ID获得您想要的post。这将返回一个对象,其中内容在post_content中,然后您所要做的就是检查长度:

$post    = get_post('post_id'); 
$content = $post->post_content;
$length  = strlen($content);

如果你在保存时这样做,通常是:

add_action( 'save_post', 'post_save' );
function post_save( $post_id ) {
    $post    = get_post( $post_id ); 
    $content = $post->post_content;
    $content = apply_filters('the_content', $content);
    return strlen($content);
}