Một số dòng code hữu ích để quản trị website Wordpress

Nếu bạn đang có website WordPress và không muốn sử dụng quá nhiều plugins để thực hiện một số tính năng cơ bản thì hãy tham khảo những dòng code dưới đây của ScapBot nhé.

Tắt trường nhập website trong phần bình luận

add_action( 'after_setup_theme', 'tu_add_comment_url_filter' );
function tu_add_comment_url_filter() {
    add_filter( 'comment_form_default_fields', 'tu_disable_comment_url', 20 );
}

function tu_disable_comment_url($fields) {
    unset($fields['url']);
    return $fields;
}

Mặc định lưu tên và và email trong lần bình luận tiếp theo

add_filter( 'comment_form_default_fields', function( $fields ) {
    $commenter = wp_get_current_commenter();

    $consent   = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';

    $fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" checked="checked" value="yes"' . $consent . ' />' . '<label for="wp-comment-cookies-consent">Save my name and email in this browser for the next time I comment.</label></p>';

    return $fields;
}, 20 );

Chỉnh lại tên một số trường trong phần bình luận

add_filter( 'gettext', function( $text ) {
    if ( 'Leave a Comment' === $text ) {
        $text = 'Bình luận của bạn';
    }

    if ( 'Post a Comment' === $text ) {
        $text = 'Đăng bình luận';
    }

	 if ( 'Post Comment' === $text ) {
        $text = 'Gửi bình luận';
    }
	
	 if ( 'Reply' === $text ) {
        $text = 'Trả lời';
    }
	
	if ( 'Cancel reply' === $text ) {
        $text = 'Hủy trả lời';
    }
    return $text;
} );

add_filter( 'comment_form_default_fields', 'tu_filter_comment_fields', 20 );
function tu_filter_comment_fields( $fields ) {
    $commenter = wp_get_current_commenter();

    $fields['author'] = '<label for="author" class="screen-reader-text">' . esc_html__( 'Name', 'generatepress' ) . '</label><input placeholder="Tên của bạn *" id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" />';

    $fields['email'] = '<label for="email" class="screen-reader-text">' . esc_html__( 'Email', 'generatepress' ) . '</label><input placeholder="Địa chỉ email *" id="email" name="email" type="email" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30" />';

    $consent   = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';

    $fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"' . $consent . ' />' . '<label for="wp-comment-cookies-consent">Ghi nhớ thông tin cho lần bình luận tiếp theo</label></p>';
	
    unset( $fields['url'] );

    return $fields;
}

Tuỳ chỉnh dành cho website sử dụng theme GeneratePress

Tắt sticky menu cho trang tuỳ chỉnh

add_action( 'wp_enqueue_scripts', function() {
    if ( is_page( array(1245, 2, 14, 31, 40, 42, 36, 34, 2896, 4104, 4886) ) ) {
        wp_dequeue_style( 'generate-sticky' );
        wp_dequeue_script( 'generate-sticky' );
    }
}, 150 );

Trong phần code bên trên có thể có những tuỳ biến như sau:

  • in_category( 40 ): Tắt cho danh mục bài viết với ID = 40
  • is_single( 59 ): Tắt cho bài viết với ID = 59
  • is_singular(‘post’): Tắt cho trang bài viết đơn

Giúp Bạn Làm Marketing Và Kinh Doanh Tốt Hơn

Đăng ký ngay hôm nay để nhận những nội dung đầy giá trị trong hộp thư đến của bạn.

Hoàn toàn miễn phí. Cam kết không spam. Bảo mật thông tin vĩnh viễn.