2013-04-08 42 views
1

我需要一個函數,它將獲取正在保存的帖子的內容,計算包括標點符號和空格在內的字符並返回一個數字。wordpress插件函數將返回正在保存的帖子的字符數

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

我將通過使用add_action('save_post', 'char_count');運行函數,我不知道如何在我的函數中獲取正在保存的帖子的帖子內容,以便我可以運行計數腳本。

回答

0

get_post()通過傳遞職位ID獲取您想要的職位。返回一個對象,其中的內容是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); 
}