专业WORDPRESS主题设计制作

wp_insert_comment钩子(手机wps钩子符号怎么打)

发布于: 2022-08-19

在开发留言表单的过程中,客户需要提交的字段都是不同的,而WordPress默认的只有四个字段:邮箱、网址、姓名和内容。如果我们想要增加几个字段就需要使用wp_insert_comment钩子。

wp_insert_comment钩子主要给评论表单增加新的字段。

语法

do_action( 'wp_insert_comment', int $id, WP_Comment $comment )

实例

add_action('wp_insert_comment','wp_insert_tel',10,1);
function wp_insert_tel($comment_ID) {
    $tel = isset($_POST['tel']) ? $_POST['tel'] : false;
    $qq = isset($_POST['qq']) ? $_POST['qq'] : false;
    update_comment_meta($comment_ID,'tel',$tel);
    update_comment_meta($comment_ID,'qq',$qq);
}

使用这个钩子需要请了解update_comment_meta()函数

通过这种方法已经可以在数据表中添加新的的键值,但是如果想要在我们的后台评论板块中显示出来还需要进行下面的操作。

实例

wp_insert_comment钩子
add_filter( 'manage_edit-comments_columns', 'my_comments_columns' );
function my_comments_columns( $columns ){
    $columns[ 'tel' ] = __( '电话' );
    $columns[ 'qq' ] = __( 'QQ号' );
    return $columns;
}

add_action( 'manage_comments_custom_column', 'output_my_comments_columns', 10, 2 );
function output_my_comments_columns( $column_name, $comment_id ){
    switch( $column_name ) {
        case "tel" :
            echo get_comment_meta( $comment_id, 'tel', true );
            break;
        case "qq" :
            echo get_comment_meta( $comment_id, 'qq', true );
            break;
    }
}

manage_edit-comments_columns钩子可以用来更改评论模块显示字段的值,比如我们提交了一个名为“tel”的键,但我们的网站是针对国内客户,需要把“tel”换成“电话”,这时就要使用这个钩子了。

manage_comments_custom_column钩子可以用来在评论模块添加自定义的字段

get_comment_meta()函数用来输出值,详细了解此函数请点击查看get_comment_meta()函数

WP技术资料 wordpress模板制作、wordpress主题开发相关知识常见问题总结
MORE
服务电话:
0533-2765967

微信 13280692153