Tùy biến giao diện bài viết Private và Protected

0

   Để tùy biến giao diện cho những bài viết PrivateProtected thì còn tùy thuộc vào từng theme mà mình sử dụng… Hướng dẫn dưới đây chỉ mang tính chất tham khảo dùng để lưu trữ lại để sau này cần tra cứu…


   Mỗi lần WordPress nâng cấp lên phiên bản mới thì sẽ bị chuyện lại giao diện mặc định của WordPress nên buộc phải sửa code lại…


   Mọi thay đổi cho giao diện của bài viết PrivateProtected đều nằm trong tập tin /wp-includes/post-template.php. Tuy nhiên tập tin ít khi được cập nhật nên có thể dùng lại phiên bản đã được sửa code nhưng phải kiểm tra kỹ lại và sửa lại tùy theo theme.


1. Giao diện cho bài viết Protected


   Để đổi giao diện mặc định cho bài viết được set password như hình minh họa sau. (Lưu ý bài viết này dựa trên theme SpicePress Pro.)



Mở tập tin post-template.php tìm đoạn code.


Code:

function get_the_password_form( $post = 0 ) {

    $post   = get_post( $post );

    $label  = 'pwbox-' . ( empty( $post->ID ) ? rand() : $post->ID );

    $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post">

    <p>' . __( 'This content is password protected. To view it please enter your password below:' ) . '</p>

    <p><label for="' . $label . '">' . __( 'Password:' ) . ' <input name="post_password" id="' . $label . '" type="password" size="20" /></label> <input type="submit" name="Submit" value="' . esc_attr_x( 'Enter', 'post password form' ) . '" /></p></form>

    ';(code-box)

 Thay thế bằng:

/* Bắt đầu mã nguồn thay đổi giao diện Bài Viết được set Password */

function get_the_password_form( $post = 0 ) {

    $post = get_post( $post );

    $label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID );

    $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post">

    <hr />

    <p>' . __( '<div align="center">&ldquo;Nội dung của bài viết/trang này đã được Bảo Vệ <b>&dagger;</b>.<br />Vui lòng nhập vào <b>(╭ರ_•́)</b> Mật Mã để truy cập.&rdquo;</div>' ) . '

    <br />

    <hr width="50%" />

    <div align="center">

    <img src="/wp-content/themes/spicepress/images/locked.png" alt="How to Bypass Locked Content" height="300" width="300" border="0" />

    </div>

    <hr width="50%" />

    <br />

    </p>

    <p align="center"><label for="' . $label . '">' . __( 'Mật Mã' ) . '<input name="post_password" id="' . $label . '" type="password" size="25" /></label></p>

    <p align="center"><input type="submit" name="Submit" value="' . esc_attr_x( 'Truy Cập', 'post password form' ) . '" /></p></form>

    <hr />

    ';

/* Kết thúc mã nguồn thay đổi giao diện Bài Viết được set Password */(code-box)

 Phần còn lại là tùy chỉnh theo sở thích… đoạn code trên chỉ là ví dụ thôi…


2. Việt hóa và thay đổi giao diện tiêu đề bài viết Private và Protected


Để Việt hóa tiêu đề bài viết như hình bên dưới…



Tiếp tục mở tập tin post-template.php tìm đoạn code.


Code:

function get_the_title( $post = 0 ) {

    $post = get_post( $post );

    $title = isset( $post->post_title ) ? $post->post_title : '';

    $id    = isset( $post->ID ) ? $post->ID : 0;

    if ( ! is_admin() ) {

        if ( ! empty( $post->post_password ) ) {

            /* translators: %s: Protected post title. */

            $prepend = __( 'Protected: %s' );

            /**

             * Filters the text prepended to the post title for protected posts.

             *

             * The filter is only applied on the front end.

             *

             * @since 2.8.0

             *

             * @param string  $prepend Text displayed before the post title.

             *                         Default 'Protected: %s'.

             * @param WP_Post $post    Current post object.

             */

            $protected_title_format = apply_filters( 'protected_title_format', $prepend, $post );

            $title                  = sprintf( $protected_title_format, $title );

        } elseif ( isset( $post->post_status ) && 'private' === $post->post_status ) {

            /* translators: %s: Private post title. */

            $prepend = __( 'Private: %s' );

            /**

             * Filters the text prepended to the post title of private posts.

             *

             * The filter is only applied on the front end.

             *

             * @since 2.8.0

             *

             * @param string  $prepend Text displayed before the post title.

             *                         Default 'Private: %s'.

             * @param WP_Post $post    Current post object.

             */

            $private_title_format = apply_filters( 'private_title_format', $prepend, $post );

            $title                = sprintf( $private_title_format, $title );

        }

    }(code-box)

 Sửa lại hoặc Việt hóa Protected: %sPrivate: %s. Hoặc sửa thành đoạn code sau:


Code:

function get_the_title( $post = 0 ) {

    $post = get_post( $post );

    $title = isset( $post->post_title ) ? $post->post_title : '';

    $id    = isset( $post->ID ) ? $post->ID : 0;

    if ( ! is_admin() ) {

        if ( ! empty( $post->post_password ) ) {

            /* translators: %s: Protected post title. */

            $prepend = __( 'Khóa 🔗: %s' );

            /**

             * Filters the text prepended to the post title for protected posts.

             *

             * The filter is only applied on the front end.

             *

             * @since 2.8.0

             *

             * @param string  $prepend Text displayed before the post title.

             *                         Default 'Protected: %s'.

             * @param WP_Post $post    Current post object.

             */

            $protected_title_format = apply_filters( 'protected_title_format', $prepend, $post );

            $title                  = sprintf( $protected_title_format, $title );

        } elseif ( isset( $post->post_status ) && 'private' === $post->post_status ) {

            /* translators: %s: Private post title. */

            $prepend = __( 'Ẩn 🔗: %s' );

            /**

             * Filters the text prepended to the post title of private posts.

             *

             * The filter is only applied on the front end.

             *

             * @since 2.8.0

             *

             * @param string  $prepend Text displayed before the post title.

             *                         Default 'Private: %s'.

             * @param WP_Post $post    Current post object.

             */

            $private_title_format = apply_filters( 'private_title_format', $prepend, $post );

            $title                = sprintf( $private_title_format, $title );

        }

    }(code-box)

    Đơn giản chỉ có vậy thôi có điều nhiều khi lâu quá không sửa code… lúc nâng cấp lên WordPress mới thì lại quên mất sửa code tập tin nào… 

Đăng nhận xét

0 Nhận xét

   * Xin vui lòng sử dụng Tiếng Việt "có dấu" nhằm hạn chế những hiểu lầm không đáng có khi để lại Nhận Xét của bạn. Tham khảo thêm †Quy Định Bình Luận -> [ Chi Tiết ]

Đăng nhận xét (0)

#buttons=(Đồng Ý !) #days=(15)

   Blog mình sử dụng 'Cookies' giúp nâng cao trải nghiệm cho khách truy cập. Nếu bạn vẫn chọn tiếp tục truy cập, bạn mặc nhiên chấp nhận các điều khoản hiện có trong †Quy Định Riêng Tư. -> [ Chi Tiết ]
Accept !
Top