wordpress主题开发
WordPress theme development is more than just creating an appealing design. It includes writing clean code, testing of all types of content, and using the right template files. However, one of the most important things to take care of is the WordPress theme development standards. Yes, there are certain Theme Development standards set by WordPress which should be followed strictly. Therefore, we have gathered important standards to help you get started with WordPress theme development.
WordPress主题开发不仅仅是创建吸引人的设计。 它包括编写干净的代码,测试所有类型的内容以及使用正确的模板文件。 但是,最重要的事情之一是WordPress主题开发标准。 是的,WordPress设置了某些主题开发标准,应严格遵循这些标准。 因此,我们收集了重要的标准来帮助您开始WordPress主题开发。
编码标准 (Coding standards)
WordPress has set high standards for CSS, HTML, JavaScript and PHP. These coding standards have been introduced to make it easier for other developers to read and alter your code. If you are planning on developing for others and really want to “sell your code”, it’s the only way out. As a matter of fact, not every developer has same style of coding. Probably you are one of them who like writing each line of code in a single sentence. But while you think that your code is quite clean, there are certain common standards that are followed by developers around the globe to help developers code in the same style. All download centers and serious theme marketplaces need their developers to code keeping in with these standards.
WordPress为CSS,HTML,JavaScript和PHP设置了高标准。 引入这些编码标准是为了使其他开发人员更容易阅读和更改您的代码。 如果您打算为他人开发,并且真的想“出售您的代码”,那是唯一的出路。 实际上,并非每个开发人员都具有相同的编码风格。 您可能是其中之一,他们喜欢用一个句子编写每一行代码。 但是,尽管您认为自己的代码很干净,但是全球开发人员仍遵循某些通用标准,以帮助开发人员以相同的样式进行编码。 所有下载中心和严肃的主题市场都需要其开发人员编写符合这些标准的代码。
HTML coding standards: There are certain simple yet important policies to write standards-validated code.
HTML编码标准:有一些简单而重要的策略可以编写经过标准验证的代码。
W3C compliant code: Writing W3C compliant code is one of the most important things. If your code is not W3C (World Wide Web Consortium) validated, it would be considered as an error. W3C compliant websites load faster and get higher search engine rankings. Therefore it is extremely important to always keep in mind W3C standards while writing the code. Self-closing elements: Elements should be enclosed in proper self closing elements. Example: Correct: <br /> ending an element with a space, a slash and a “greater than” “>” sign Incorrect: <br/> The W3C standards specify that you should precede the self closing elements with a single space. Wrapping the attributes: It is always suggested that you should wrap your attributes in either single ” or double "" quotes for better readability. Indentation: Since HTML is used with PHP, proper indentation is required. Use tabs instead of spaces. Example:
W3C兼容代码:编写W3C兼容代码是最重要的事情之一。 如果您的代码未经W3C(万维网联盟)验证,则将其视为错误。 符合W3C的网站加载速度更快,并获得更高的搜索引擎排名。 因此,在编写代码时始终牢记W3C标准非常重要。 自封闭元素:元素应包含在适当的自封闭元素中。 示例:正确:<br />以空格,斜杠和“大于”“>”符号结尾的元素不正确:<br/> W3C标准规定,自闭元素应以单个空格开头。 包装属性:始终建议您将属性包装在单引号或双引号中,以提高可读性。 缩进:由于HTML与PHP一起使用,因此需要适当的缩进。 使用制表符代替空格。 例:
<?php if ( ! have_posts() ) : ?>
<div id="post-1" class="post">
<h1 class="entry-title">Unable to Find</h1>
<div class="entry-content">
<p>Sorry, we couldn't find anything.</p>
<?php get_search_form(); ?>
</div>
</div>
<?php endif; ?>
<?php if ( ! have_posts() ) : ?>
<div id="post-1" class="post">
<h1 class="entry-title">Unable to Find</h1>
<div class="entry-content">
<p>Sorry, we couldn't find anything.</p>
<?php get_search_form(); ?>
</div>
</div>
<?php endif; ?>
CSS编码标准: (CSS coding standards:)
Like HTML, CSS coding standards were introduced to set a baseline for collaboration.
与HTML一样,CSS编码标准也被引入来为协作设定基准。
![](https://i-blog.csdnimg.cn/blog_migrate/667eceb543c1ed1fd954f064aec47c19.png)
![](https://i-blog.csdnimg.cn/blog_migrate/667eceb543c1ed1fd954f064aec47c19.png)
Structure: Though there are various different styles of structuring a stylesheet, it is imperative to maintain high degree of legibility. In order to indent different properties, use tabs instead of spaces Example:
结构:尽管样式表的结构样式多种多样,但必须保持高度的易读性。 为了缩进不同的属性,请使用制表符而不是空格。 示例:
#selector-1,
#selector-2,
#selector-3 {
padding: 15px;
color: #000;
}
#selector-1,
#selector-2,
#selector-3 {
padding: 15px;
color: #000;
}
Selectors: Use hypens and lowercase letters to name “class” and “ID” in your stylesheet. Example:
选择器:使用连字符和小写字母在样式表中命名“类”和“ ID”。 例:
#comment-form {
margin: 1em 0;
}
input[type="text"] {
line-height: 1.4;
}
#comment-form {
margin: 1em 0;
}
input[type="text"] {
line-height: 1.4;
}
Properties: Terminate all the properties with a colon and a space. Use only shorthand for padding values, margin, list-style, font, border, background and hex code for colors #000000. Property ordering: When it comes to writing properties, there is a particular order which should be followed. Example:
属性:用冒号和空格终止所有属性。 对于颜色#000000,仅将速记用于填充值,边距,列表样式,字体,边框,背景和十六进制代码。 属性顺序:在编写属性时,应遵循特定的顺序。 例:
- Order 订购
- Positioning 定位
- Box model 箱型
- Colors and typography 颜色和版式
- Other 其他
JavaScript标准: (JavaScript standards:)
Spacing: use liberal spaces for better readability. Indent with spaces and do not use any whitespace at the end of the line. Also, any comma “,” and “;” should not have any following space. Objects: Object declarations should be made in different lines with one property per line only if that declaration is too long. Example:
间距:使用自由空间以提高可读性。 缩进空格,不要在行尾使用任何空格。 另外,任何逗号“,”和“;” 不得有以下空格。 对象:对象声明应在不同的行中进行,每行只有一个属性,如果该声明太长。 例:
var map = {
ready: 9,
when: 4,
'you are': 15
};
var map = {
ready: 9,
when: 4,
'you are': 15
};
Function calls and arrays: There should always be extra spaces around elements and arguments though there can be certain exceptions. Example:
函数调用和数组:尽管可能会有某些例外,但是元素和参数周围始终应该有多余的空格。 例:
array = [ a, b ];
foo( arg );
foo( 'string', object );
foo( options, object[ property ] );
foo( node, 'property', 2 );
array = [ a, b ];
foo( arg );
foo( 'string', object );
foo( options, object[ property ] );
foo( node, 'property', 2 );
PHP编码标准 (PHP coding standards)
There are numerous coding standards to keep into account when it comes to PHP such as Yoda conditions, Regular expressions, whitespace usage, single and double quotes, naming conventions, and formatting database queries.
当涉及到PHP时,有许多编码标准需要考虑,例如Yoda条件,正则表达式,空格用法,单引号和双引号,命名约定以及格式化数据库查询。
测试您的主题: (Testing your theme:)
You should always test your theme before putting it online. It should be able to deal with a variety of user data. There are various plugins available to help you test your theme such as Monster widget, RTL Tester, Theme mentor and much more. You can also make use of WordPress theme unit test. Aforementioned are some of the major WordPress theme development standards. WP comes with some default themes. You can check these themes to have a better idea about development and coding standards. WordPress standards play a crucial role in theme development and therefore should be strictly followed when building a theme in WordPress.
在线放置主题之前,您应该始终对其进行测试。 它应该能够处理各种用户数据。 有各种可用的插件可帮助您测试主题,例如Monster小部件,RTL测试器,主题导师等等。 您还可以利用WordPress主题单元测试。 上面是一些主要的WordPress主题开发标准。 WP带有一些默认主题。 您可以检查这些主题,以更好地了解开发和编码标准。 WordPress标准在主题开发中起着至关重要的作用,因此在WordPress中构建主题时应严格遵循。
翻译自: https://www.script-tutorials.com/understanding-the-details-of-wordpress-theme-development-standards/
wordpress主题开发