主题开发文档

UI 最佳实践

💡 云策文档标注

概述

本文档介绍了 WordPress 开发中用户界面设计的最佳实践,涵盖链接、表单和可访问性等方面,旨在提升网站可用性和用户体验。

关键要点

  • Logo 应链接到首页:使用 the_custom_logo() 或站点 Logo 块时默认实现,也可手动在 header.php 中添加链接。
  • 使用描述性锚文本:链接文本应清晰指示点击后的操作,避免使用模糊的“点击这里”。
  • 链接样式:保持下划线以增强可识别性,使用不同颜色区分未访问和已访问链接,并考虑 hover、focus 和 active 状态。
  • 颜色对比度:确保文本与背景的对比度符合 WCAG 2.0 AA 标准(至少 4.5:1),可使用 WebAIM 工具检查。
  • 字体大小:最小文本尺寸建议为 14px,以提高可读性。
  • 关联标签与输入:使用 label 的 for 属性连接标签和输入字段,提升表单可访问性,适用于文本输入和单选按钮。
  • 占位符文本:在表单中使用占位符作为示例提示,但不能替代标签。
  • 描述性按钮:按钮文本应使用 [动词] [名词] 模式(如“创建用户”),明确指示操作意图。

代码示例

<a href="<?php echo esc_url( home_url( '/' ) ); ?>"><img src="<?php echo get_stylesheet_directory_uri(); ?>/logo.png" alt="<?php esc_attr_e( 'Home Page', 'textdmomain' );?>" /></a>
<label for="username">Username</label>
<input type="text" id="username" name="login" />
<input type="radio" id="user_group_blogger" name="user_group" value="blogger" />
<label for="user_group_blogger">Blogger</label>
 
<input type="radio"  id="user_group_designer" name="user_group" value="designer" />
<label for="user_group_designer">Designer</label>
 
<input type="radio"  id="user_group_developer" name="user_group" value="developer" />
<label for="user_group_developer">Developer</label>
<label for="name">Name</label>
<input type="text" id="name" name="name" placeholder="John Smith" />

📄 原文内容

The logo at the top each page should send the user to the homepage of your site.
If you are using the recommended function, the_custom_logo() or the site logo block, the logo is linked to the homepage by default.

You can also add your logo manually. Assuming your logo is in your theme directory, this is how to display it in the header.php template file.

<a href="<?php echo esc_url( home_url( '/' ) ); ?>"><img src="<?php echo get_stylesheet_directory_uri(); ?>/logo.png" alt="<?php esc_attr_e( 'Home Page', 'textdmomain' );?>" /></a>

Descriptive Anchor Text

The anchor text is the visible text for a hyperlink. Good link text should give the reader an idea of the action that will take place when clicking it.

A bad example:

The best way to learn WordPress is to start using it. To Download WordPress, <a href="https://wordpress.org/download/">click here</a>.

A better example:

<a href="https://wordpress.org/download/">Download WordPress</a> and start using it. That's the best way to learn.

By default, browsers underline links to let the user know what is clickable. Some designers use CSS to turn off underlines for hyperlinks. This causes usability and accessibility problems, as it makes it more difficult to identify hyperlinks from the surrounding text.

Color is another visual cue that text is clickable. Styling hyperlinks with a different color than the surrounding text makes them easier to distinguish.

Hyperlinks are one of the few HTML features that have state. The two most important states are visited and unvisited.

Having different colors for these two states helps users identify the pages they’ve visited before. A good trick for taking the guess work out of visited links is to color them 10%-20% darker than the unvisited links.

There are 3 other states that links can have:

  • hover, when a mouse is over an element
  • focus, similar to hover but for keyboard users
  • active, when a user is clicking on a link

Since hover and focus have similar meanings, it is useful to give them the same styles.

Though hover and focus have similar meanings, they have different interaction patterns. If you choose a subtle hover state, you should have a more easily identifiable focus state. Hovering over a link is a directed activity, where the user knows where they are in the page and only needs to identify whether that spot is linked. Focus is an undirected activity, where the user needs to discover where their focus has moved to after shifting focus from the previous location.

Color Contrast

Color contrast refers to the difference between two colors. Contrast is low between navy blue and black. Contrast is high between white and black. WebAIM, a non-profit web accessibility organization, provides a color contrast calculator to help you determine the contrast in your website design. The WCAG 2.0 requires a ratio of 4.5:1 on normal text to be AA compliant.

Sufficient Font Size

Make your text easy to read. By making your text large enough, you increase the usability of your site and make the content easier to understand. 14px is the smallest text should be.

Associate Labels with Inputs

Labels inform the user what an input field is for. You can connect the label to the input by using the for attribute in the label. This will allow the user to click the label and focus on the input field.

<label for="username">Username</label>
<input type="text" id="username" name="login" />

Labels work for radio buttons as well. Since it works using the id field and not the name, each input for the group gets its own label.

<input type="radio" id="user_group_blogger" name="user_group" value="blogger" />
<label for="user_group_blogger">Blogger</label>
 
<input type="radio"  id="user_group_designer" name="user_group" value="designer" />
<label for="user_group_designer">Designer</label>
 
<input type="radio"  id="user_group_developer" name="user_group" value="developer" />
<label for="user_group_developer">Developer</label>

Placeholder Text in Forms

Placeholder text shows the user an example of what to type. When a user puts their cursor in the field, the placeholder text will disappear, while the label remains.

<label for="name">Name</label>
<input type="text" id="name" name="name" placeholder="John Smith" />

Use placeholders to suggest the type of data a field requires, and not as a substitute for the field label.

Descriptive Buttons

The web is filled with buttons that have unclear meanings. Remember the last time you used ‘OK’ or ‘submit’ on your login form? Choosing better words to display on your buttons can make your website easier to use. Try the pattern [verb] [noun] — Create user, Delete File, Update Password, Send Message. Each describes what will happen when the user clicks the button.