WordPress中對訪客評論功能的一些優(yōu)化方法,wordpress評論功能
Jun 13, 2016 am 08:50 AMWordPress中對訪客評論功能的一些優(yōu)化方法,wordpress評論功能
前幾天見到某 Blog (忘記名字和網(wǎng)址了) 有一個相當(dāng)實用的評論功能. 訪客留言之后資料輸入框會被隱藏起來, 如同登錄了一般. 訪客可以選擇修改相關(guān)資料再進(jìn)行評論. 給予訪客很好的用戶體驗. 今天我將這個功能移植到了自己的主題上, 制作不難, 分享一下吧.
需求
細(xì)心的朋友可能已經(jīng)注意到了: 當(dāng)在某個 WordPress 發(fā)表評論后再次訪問該 Blog, 資料就不需要再次填寫, 因為它們都已經(jīng)在資料輸入框里面. 但沒評論過的或者清除了 Cookie 之后, 資料輸入框?qū)⒖湛杖缫?
1. 當(dāng)訪客的資料已經(jīng)存在的情況下, 訪客很少關(guān)注資料本身, 那些資料輸入框就會變成 "礙眼的東西", 我們要想辦法將它們隱藏起來. 同時, 我們需要將這位訪客的名字顯示出來, 否則他/她根本不知道自己的身份.
2. 訪客有可能郵箱更換了, 或者就想換個酷點的名字, 此時的他/她肯定想更改一下那些資料. 所以要求有一些措施, 讓訪客可以重新看到資料輸入框.
3. 對于那些從未提供資料的訪客, 資料輸入框必須讓他們看到.
分析
由需求可以看到, 我們要處理的是兩種狀態(tài)的訪客: 有資料的, 無資料的.
對于有資料的, 具有顯示資料輸入框 (顯示昵稱) 和 隱藏資料輸入框 (顯示昵稱) 兩種狀態(tài).
而無資料的訪客只有顯示資料輸入框 (沒有昵稱) 一種狀態(tài).
好, 我們就為有資料的訪客配備兩個按鈕 (更改和取消), 一個用來顯示資料輸入框, 一個用來隱藏它.
思路
1. 頁面怎么寫? 編碼前, 我們還應(yīng)該理一下頭緒. 用偽代碼吧.
if (有資料的訪客) {
??? 放置訪客昵稱
??? 放置更改按鈕 (點擊后: 隱藏更改按鈕, 顯示取消按鈕, 顯示資料輸入框)
??? 放置取消按鈕 (點擊后: 顯示更改按鈕, 隱藏取消按鈕, 隱藏資料輸入框)
}
放置資料輸入框
if (有資料的訪客) {
??? 隱藏取消按鈕
??? 隱藏資料輸入框
}
2. 怎么獲知訪客是否評論過? 前面已經(jīng)談到, 已評論訪客的資料會在顯示出來, 也就是說, 代碼中已經(jīng)實現(xiàn)了獲取資料的方法. 那我們找找吧...
<input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" tabindex="1" />
就是它! $comment_author 是訪客的昵稱, 當(dāng)它為空的時候就說明訪客資料為空.
3. 有些控件又顯示又隱藏的, 怎么弄呢? 我們不需要為此轉(zhuǎn)跳頁面, 用 JavaScript 吧. 我們可以寫一個方法, 用來設(shè)定某些控件的顯示與否, 只是一個很簡單的方法:
/** * 設(shè)定控件的顯示風(fēng)格 * @param id 控件的 ID * @param status 控件的顯示狀態(tài) (顯示時為 '', 隱藏時為 'none') */ function setStyleDisplay(id, status) { document.getElementById(id).style.display = status; }
編碼
接著干嘛? 大概可以寫代碼了. 看我的...
<!-- 有資料的訪客 --> <?php if ( $comment_author != "" ) : ?> <!-- 轉(zhuǎn)換顯示狀態(tài)用的 JavaScript Q1: 為什么這段代碼放在這里呢? A1: 因為只有當(dāng)訪客有資料時, 它才被用上, 這樣可以減少無資料訪客下載頁面時的開銷. Q2: 為什么不用外部文件將 JavaScript 放起來? 也許那樣維護(hù)起來更方便. A2: 因為它只在這個地方用到了. 而且加載文件的數(shù)量也會影響頁面下載速度, 為了這么點字節(jié)的代碼, 不值得新開一個文件. --> <script type="text/javascript">function setStyleDisplay(id, status){document.getElementById(id).style.display = status;}</script> <div class="form_row small"> <!-- 訪客昵稱 (隨便歡迎一下) --> <?php printf(__('Welcome back <strong>%s</strong>.'), $comment_author) ?> <!-- 更改按鈕 (點擊后: 隱藏更改按鈕, 顯示取消按鈕, 顯示資料輸入框) --> <span id="show_author_info"><a href="javascript:setStyleDisplay('author_info','');setStyleDisplay('show_author_info','none');setStyleDisplay('hide_author_info','');"><?php _e('Change »'); ?></a></span> <!-- 取消按鈕 (點擊后: 顯示更改按鈕, 隱藏取消按鈕, 隱藏資料輸入框) --> <span id="hide_author_info"><a href="javascript:setStyleDisplay('author_info','none');setStyleDisplay('show_author_info','');setStyleDisplay('hide_author_info','none');"><?php _e('Close »'); ?></a></span> </div> <?php endif; ?> <!-- 資料輸入框 --> <div id="author_info"> <div class="form_row"> <input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" tabindex="1" /> <label for="author" class="small"><?php _e('Name'); ?> <?php if ($req) _e('(required)'); ?></label> </div> <div class="form_row"> <input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" tabindex="2" /> <label for="email" class="small"><?php _e('E-Mail (will not be published)');?> <?php if ($req) _e('(required)'); ?></label> </div> <div class="form_row"> <input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" tabindex="3" /> <label for="url" class="small"><?php _e('Website'); ?></label> </div> </div> <!-- 有資料的訪客 --> <?php if ( $comment_author != "" ) : ?> <!-- 隱藏取消按鈕, 隱藏資料輸入框 --> <script type="text/javascript">setStyleDisplay('hide_author_info','none');setStyleDisplay('author_info','none');</script> <?php endif; ?>
訪客評論顯示歡迎信息
關(guān)鍵問題:獲取訪客信息
花點時間去研究,其實整個實現(xiàn)過程并不復(fù)雜。這里的關(guān)鍵點是,如何判斷訪客已經(jīng)在近期發(fā)表過評論。
當(dāng)訪客評論時,會在 Cookie 中保存評論者的信息。我們可以通過 Firebug 或者 Chrome 的 Developer Tool 來查看:
>>> document.cookie "comment_author_bbfa5b726c6b7a9cf3cda9370be3ee91=helloworld; comment_author_email_bbfa5b726c6b7a9cf3cda9370be3ee91=dangoakachan%40gmail.com; comment_author_url_bbfa5b726c6b7a9cf3cda9370be3ee91=http%3A%2F%2Fexample.com"
從上面可以看到有三個與評論相關(guān)的信息,它們分別是comment_author,comment_author_url,comment_author_email。不過中間夾雜著字符串 bbfa5b726c6b7a9cf3cda9370be3ee91,我們可以看下 default-constants.php 的代碼,就可以知道這一段叫做 COOKIEHASH,它的值是博客 URL 的 md5值。
>>> import hashlib >>> hashlib.md5('http://localhost/wordpress').hexdigest() 'bbfa5b726c6b7a9cf3cda9370be3ee91'
我們只需要了解到這一點就可以了,因為這些信息 WordPress 已經(jīng)在comments_template方法中,通過wp_get_current_commenter為我們從 Cookie 中解析了訪客的信息。例如,我們可以在 comment.php 文件中,直接用$comment_author來獲取保存在 Cookie 中的訪客姓名。
代碼實現(xiàn)
接下來的實現(xiàn)就很簡單了,我們通過判斷$comment_author變量值是否為空,來確定訪客是否在近期有評論(有 Cookie)。
if (!is_user_logged_in() && !empty($comment_author)) { ... }
如果有,則在評論框上方顯示歡迎信息:
if (!is_user_logged_in() && !empty($comment_author)) { $welcome_login = '<p id="welcome-login"><span>歡迎回來, <strong>' . $comment_author . '</strong>.</span>'; $welcome_login .= ' <span id="toggle-author"><u>更改</u> <i class="icon-signout"></i></span></p>'; $comments_args['comment_field'] = '</div>' . $comments_args['comment_field']; $comments_args['comment_notes_before'] = $welcome_login . '<div id="author-info" class="hide">'; }
以上代碼,需要添加到主題的 comment.php 文件 comment_form($comments_args) 方法調(diào)用之前。
接下來,我們通過 Javascript 來實現(xiàn)訪客信息更改:
/* Toggle comment user */ $('#comments').on('click', '#toggle-author', function () { $('#author-info').slideToggle(function () { if ($(this).is(':hidden')) { /* Update author name in the welcome messages */ $('#welcome-login strong').html($('#author').val()); /* Update the toggle action name */ $('#toggle-author u').html('更改'); } else { /* Update the toggle action name */ $('#toggle-author u').html('隱藏'); } }); });
這樣,如果用戶需要更新信息時,可以點擊歡迎信息右側(cè)的更改按鈕;修改完成之后,用戶信息會在評論后更新。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

Web development design is a promising career field. However, this industry also faces many challenges. As more businesses and brands turn to the online marketplace, web developers have the opportunity to demonstrate their skills and succeed in their careers. However, as demand for web development continues to grow, the number of developers is also increasing, resulting in increasingly fierce competition. But it’s exciting that if you have the talent and will, you can always find new ways to create unique designs and ideas. As a web developer, you may need to keep looking for new tools and resources. These new tools and resources not only make your job more convenient, but also improve the quality of your work, thus helping you win more business and customers. The trends of web development are constantly changing.

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

To create an account on WordPress, simply visit its website, select the registration option, fill in the registration form, and verify your email address. Other ways to register include using a Google account or Apple ID. The benefits of signing up include creating a website, gaining features, joining the community, and gaining support.

Do you need to fix HTTP image upload errors in WordPress? This error can be particularly frustrating when you create content in WordPress. This usually happens when you upload images or other files to your CMS using the built-in WordPress media library. In this article, we will show you how to easily fix HTTP image upload errors in WordPress. What is the reason for HTTP errors during WordPress media uploading? When you try to upload files to Wo using WordPress media uploader
