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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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相应位置:

<label id="author_sinawb" for="sinawb">Weibo.<input id="sinawb" type="text" tabindex="5" value="<?php if(isset($_COOKIE['bigfa_sinawb'])) echo $_COOKIE['bigfa_sinawb'];?>" name="sinawb">
</label>

如果是默认的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;
}

 

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~

支付宝
微信