wordpress留言板comments.php添加自定义字段,WordPress评论框DIY自定义增加字段

您当前的位置: 首页 > Wordpress > Wordpress 综合 > 正文内容 WordPress评论框DIY自定义增加字段

WordPress评论框DIY自定义增加字段

01bf1570b9c3ef2ecea232cc31ff6170.png

小编曾经做过有关wordpres评论框自定义的文章,这里我们用另外一个方法实现自定义增加评论框的自定义字段。WordPress默认的评论框只有姓名、邮箱、站点和评论四个字段,但对于一个企业网站,或者一个个性化网站,这些字段显然是不够的,比如我们想增加国家/地区、微信号、QQ号码、电话、传真、地址等等,就用到了自定义增加评论框字段。

建议阅读

1、WordPress函数:comment_form( )个性化评论表单多种方法

2、WordPress函数:comment_form() 让你的 WordPress 评论表单随心所愿

在主题的functions.php文件中添加如下代码

add_filter('comment_form_default_fields','comment_form_add_ewai');

function comment_form_add_ewai($fields){

$label1 = __( 'Country/Regions' );

$label2 = __( 'Skype ID' );

$label3 = __( 'Telephone' );

$label4 = __( 'Fax' );

$label5 = __( 'Address' );

$value1 = isset($_POST['country']) ? $_POST['country'] : false;

$value2 = isset($_POST['skype']) ? $_POST['skype'] : false;

$value3 = isset($_POST['tel']) ? $_POST['tel'] : false;

$value4 = isset($_POST['fax']) ? $_POST['fax'] : false;

$value5 = isset($_POST['address']) ? $_POST['address'] : false;

$fields['country'] =< <

{$label1}

HTML;

$fields['skype'] =< <

{$label2}

HTML;

$fields['tel'] =< <

{$label3}

HTML;

$fields['fax'] =< <

{$label4}

HTML;

$fields['address'] =< <

{$label5}

HTML;

return $fields;

}

add_action('wp_insert_comment','wp_insert_ewai',10,2);

function wp_insert_ewai($comment_ID,$commmentdata){

$country = isset($_POST['country']) ? $_POST['country'] : false;

update_comment_meta($comment_ID,'country',$country);

$skype = isset($_POST['skype']) ? $_POST['skype'] : false;

update_comment_meta($comment_ID,'skype',$skype);

$tel = isset($_POST['tel']) ? $_POST['tel'] : false;

update_comment_meta($comment_ID,'tel',$tel);

$fax = isset($_POST['fax']) ? $_POST['fax'] : false;

update_comment_meta($comment_ID,'fax',$fax);

$address = isset($_POST['address']) ? $_POST['address'] : false;

update_comment_meta($comment_ID,'address',$address);

}

add_filter( 'manage_edit-comments_columns', 'my_comments_columns' );

add_action( 'manage_comments_custom_column', 'output_my_comments_columns', 10, 2 );

function my_comments_columns( $columns ){

$columns[ 'country' ] = __( 'Country/Regions' );

$columns[ 'skype' ] = __( 'Skype ID' );

$columns[ 'tel' ] = __( 'Telephone' );

$columns[ 'fax' ] = __( 'Fax' );

$columns[ 'address' ] = __( 'Address' );

return $columns;

}

function output_my_comments_columns( $column_name, $comment_id ){

switch( $column_name ) {

case "country" :

echo get_comment_meta( $comment_id, 'country', true );

break;

case "skype" :

echo get_comment_meta( $comment_id, 'skype', true );

break;

case "tel" :

echo get_comment_meta( $comment_id, 'tel', true );

break;

case "fax" :

echo get_comment_meta( $comment_id, 'fax', true );

break;

case "address" :

echo get_comment_meta( $comment_id, 'address', true );

break;

}

}

复制代码时,去除标签,为了增加代码的阅读性,添加了注释,在复制到functions.php文件中时去除!

在comments.php文件中的获取评论框下面添加下面这段代码:

这样从外观上增加评论样式的个性化。

您可能感兴趣的文章:

▪ 第七课WordPress主题制作综合教程头部Brand设计

▪ wordpress调用多说最近访客设置技巧

▪ WordPress函数:comments_template(加载评论模板)

▪ wordpress首页调取最新评论代码

▪ 第五课WordPress主题制作头部文件header.php制作

▪ WordPress文章页面获取评论次数

▪ Wordpress基于bootstrap自适应主题制作

▪ WordPress函数:comment_form( )个性化评论表单多种方法

▪ 第11课WordPress主题制作启用特色图像

▪ WordPress评论及多说评论圆形旋转头像设置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值