给Wordpress评论者信息栏加入新浪微博账号输入框

网站建设 · 2016-10-29

首先就是无论你采用哪种方法,都要在 functions.php 中加入如下代码:

    add_action( 'comment_post','save_comment_meta_data' );
    function save_comment_meta_data( $comment_id ) {
        add_comment_meta( $comment_id, 'sinawb', $_POST['sinawb'] );
        $expire = time() + 99999999;
        $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
        setcookie('bigfa_sinawb',$_POST['sinawb'],$expire,'/',$domain,false);
    }
    add_filter( 'get_comment_author_link',    'attach_twitter_to_author' );
    function attach_twitter_to_author( $author ) {
        $tw = get_comment_meta( get_comment_ID(), 'sinawb', true );
        if($tw)
            $author .= " / <a href='http://weibo.com/n/$tw' title='@$tw' target='_blank'>@$tw</a>";
        return $author;
    }

如果是自定义评论,则在 comments.php 相应位置:

如果是默认的comment_form,则下面的代码加到 functions.php 中:

add_filter( 'comment_form_defaults',    'change_comment_form_defaults');
function change_comment_form_defaults($default) {
    $commenter = wp_get_current_commenter();
    $default['fields']['url'] .= '<p class="comment-form-author"><label id="author_sinawb" for="sinawb">Weibo<input id="sinawb" type="text" tabindex="5" value="';
    if(isset($_COOKIE['bigfa_sinawb'])) $default['fields']['url'] .= $_COOKIE['bigfa_sinawb'];
    $default['fields']['url'] .='" name="sinawb"></label></p>';
    return $default;
}
Wordpress
Theme Jasmine by Kent Liao