dongsechuan0535 2017-03-29 14:35
浏览 38
已采纳

Wordpress将输入字段中的值设置为登录用户的评论作者

I have a shared account, lets call it "marketing". Now I have three people using this account named Anna, Ben and Max.

When someone logs into this account and wants to type a comment with their name, it always shows the name "marketing" as author. How can I change this so that I put a name in my input field and I get the value from this as my comment author. In my comments.php I do not check if the user is logged on and call it here:

<input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="2"/>

I tried to make following in my functions.php:

function change_author( $commentdata ) {
    if ( $commentdata['user_ID'] == 2 ) {  // User ID for Marketing

    $commentdata['user_ID'] = 0; 
    $commentdata['comment_author'] = '';

  }

  return $commentdata;
}
add_filter( 'preprocess_comment' , 'change_author' ); 

In this case my comment_author will always be Anonymous and also if I set it to NULL.

When i give the variable a value $commentdata['comment_author'] = 'test'; I get "test" as my comment_author but as I said, I would like to have the value I set in my input field. It works for not logged in users very well so I thought there might be a trick for logged in users as well.

In phpMyAdmin I checked for the entries in the database and it is giving me the right values for ID etc. But it also empties the comment_author field which results in an anonymous author.

I really hope that somebody can give me the right hint to accomplish this task.

  • 写回答

2条回答 默认 最新

  • du8828 2017-03-30 07:10
    关注

    Goddamit, I didn't know that this was so f***ing easy.

    function change_author( $commentdata ) {
    
        if ( $commentdata['user_ID'] == 2 ) { 
    
        $commentdata['user_ID'] = 0;
        $commentdata['comment_author'] = $_POST['author'];
        $commentdata['comment_author_email'] = '';
        $commentdata['comment_author_url'] = '';
    
      }
    
      return $commentdata;
    }
    add_filter( 'preprocess_comment' , 'change_author' ); 
    

    I think this would work also with != 0 instead of == 2. This is just a solution for my custom theme but I think it would also work with

    $commentdata['comment_author_email'] = $_POST['mail'];
    $commentdata['comment_author_url'] = $_POST['url'];
    

    Note: the value in $_POST[] is either the name or the ID from the input field. Didn't test everything because name and ID have the same value in my theme.

    Tested it a few times local and it worked very well!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b