Thêm số điện thoại trong comment wordpress

Thêm số điện thoại trong comment wordpress

14/03/2023 - 147

Code dưới đây sẽ giúp bạn thêm trường số điện thoại vào comment form. Đồng thời xoá trường email và trường website khỏi comment form. Kết quả sẽ được như hình bên dưới

Bạn chỉ cần thêm code sau vào functions.php của theme là được

/*
 * Remove website field, Email field and Add phone field on comment form
 * Add to functions.php
 * Author: levantoan.com
*/
add_filter('comment_form_default_fields', 'devvn_website_remove');
function devvn_website_remove($fields)
{
    if (isset($fields['email'])) unset($fields['email']);
    if (isset($fields['url'])) unset($fields['url']);
    return $fields;
}
if(!function_exists('devvn_array_insert_before')) {
    function devvn_array_insert_before($key, array &$array, $new_key, $new_value)
    {
        if (array_key_exists($key, $array)) {
            $new = array();
            foreach ($array as $k => $value) {
                if ($k === $key) {
                    $new[$new_key] = $new_value;
                }
                $new[$k] = $value;
            }
            return $new;
        }
        return FALSE;
    }
}
add_filter('comment_form_default_fields', 'devvn_add_phone_comment_form_defaults');
function devvn_add_phone_comment_form_defaults($fields)
{
    $commenter = wp_get_current_commenter();
    $fields_phone = '<p class="comment-form-url">' .
        '<label for="phone">' . __('Số điện thoại') . '<span class="required">*</span></label>' .
        '<input id="phone" name="phone" type="text" size="30"  tabindex="4" required="required"/></p>';
    $fields['author'] = '<p class="comment-form-author">' . '<label for="author">' . __('Họ tên <span class="required">*</span>') . '</label> ' .
        '<input id="author" name="author" type="text" value="' . esc_attr($commenter['comment_author']) . '" size="30" maxlength="245" /></p>';
    $fields_new = devvn_array_insert_before('cookies', $fields, 'phone', $fields_phone);
    if ($fields_new) $fields = $fields_new;
    return $fields;
}
add_action('comment_post', 'devvn_save_comment_meta_data');
function devvn_save_comment_meta_data($comment_id)
{
    if ((isset($_POST['phone'])) && ($_POST['phone'] != ''))
        $phone = wp_filter_nohtml_kses($_POST['phone']);
    add_comment_meta($comment_id, 'phone', $phone);
}
add_filter('preprocess_comment', 'devvn_verify_comment_meta_data');
function devvn_verify_comment_meta_data($commentdata)
{
    if (!is_admin() && !is_user_logged_in() && 'post' === get_post_type( absint( $_POST['comment_post_ID'] ))) {
        if (!isset($_POST['phone']))
            wp_die(__('Lỗi: Số điện thoại là bắt buộc'));
        $phone = $_POST['phone'];
        if (!(preg_match('/^0([0-9]{9,10})+$/D', $phone))) {
            wp_die(__('Lỗi: Số điện thoại không đúng định dạng'));
        }
        if ($commentdata['comment_author'] == '')
            wp_die('Lỗi: Xin hãy nhập tên của bạn');
    }
    return $commentdata;
}
add_filter('comment_text', 'devvn_modify_comment');
function devvn_modify_comment($text)
{
    $commentphone = get_comment_meta(get_comment_ID(), 'phone', true);
    if ($commentphone && is_admin()) {
        $commentphone = '<br/>SĐT: <strong>' . esc_attr($commentphone) . '</strong>';
        $text = $text . $commentphone;
    }
    return $text;
}
add_filter('option_require_name_email', '__return_false');

Nguồn Lê Văn Toản

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Bài viết liên quan
Contact Me on Zalo