在博客主題里添加前臺(tái)刪除文章及評(píng)論功能

字號(hào):


    一般情況下我們都是登錄后臺(tái)進(jìn)行文章及評(píng)論管理,如能前臺(tái)實(shí)現(xiàn)管理文章,特別是前臺(tái)刪除垃圾留言,是不是很方便。實(shí)現(xiàn)方法很簡(jiǎn)單,把下面兩段代碼加到主題模板的適當(dāng)位置即可。
    前臺(tái)刪除文章
    原代碼來(lái)自:http://wp-snippets.com/delete-post-from-front-end/
    運(yùn)行原代碼后,發(fā)現(xiàn)點(diǎn)擊“Delete post”只會(huì)轉(zhuǎn)到一個(gè)警告頁(yè)面,并且不會(huì)刪除日志,稍微改動(dòng)了一下。
    修改后的:
    <?php
    $url = get_bloginfo('url');
    if (current_user_can('edit_post', $post->ID)){
    echo '<a href="';
    echo wp_nonce_url("$url/wp-admin/post.php?action=delete&post=$id", 'delete-post_'. $post->ID);
    echo '">Delete post</a>';
    }
    ?>
    前臺(tái)刪除評(píng)論
    原代碼來(lái)自:http://www.ninthday.net/2011/08/magement-comments-front.html
    原代碼需手動(dòng)修改博客URL地址,并需要確認(rèn)后方可刪除評(píng)論,修改后的:
    <?php
    if (is_user_logged_in() ) {
    $url = get_bloginfo('url');
    echo '<a id="delete-'. $comment->comment_ID .'" href="'. wp_nonce_url("$url/wp-admin/comment.php?action=deletecomment&p=". $comment->comment_post_ID . '&c='. $comment->comment_ID, 'delete-comment_'. $comment->comment_ID). '"">Delete</a>';
    }
    ?>
    HotNews主題將上述代碼加到functions.php模版:
    <span>
    這句下面適當(dāng)位置。
    這段代碼我只在HotNews主題中試過(guò),評(píng)論模版采用標(biāo)準(zhǔn)評(píng)論函數(shù)的主題未嘗試過(guò)。
    感覺(jué)第一段前臺(tái)刪除文章的代碼用處不是很大,而前臺(tái)刪除評(píng)論功能卻很實(shí)用,看到一個(gè)垃圾刪一個(gè),爽!