站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
▼
建站程序
开发
服务器
办公软件
开发教程
▼
服务器教程
软件使用教程
运营教程
热门电子书
▼
CSS教程
WordPress教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
热点词条
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
WordPress:WordPress Coding Standards
”
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
Xxf3325
(
讨论
|
贡献
)
2008年4月28日 (一) 15:41的版本
(新页面: Some legacy parts of the WordPress code structure for PHP markup are inconsistent in their style. WordPress is working to gradually improve this by helping users maintain a consistent sty...)
(差异) ←上一版本 |
最后版本
(
差异
) |
下一版本→
(
差异
)
跳转至:
导航
、
搜索
警告:您正在编辑的是本页面的旧版本。
如果您发布该更改,该版本后的所有更改都会丢失。
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
Some legacy parts of the WordPress code structure for PHP markup are inconsistent in their style. WordPress is working to gradually improve this by helping users maintain a consistent style so the code can remain clean and easy to read at a glance. Keep the following points in mind when writing code for WordPress, whether for core programming code, [[WordPress:Writing a Plugin|Plugins]], or [[WordPress:Theme Development|WordPress Themes]]. The guidelines are similar to [http://pear.php.net/manual/en/standards.php Pear standards] in many ways, but differ in some key respects. Also see [http://comox.textdrive.com/pipermail/wp-hackers/2006-July/006930.html this post on the wp-hackers list]. There is also a page on proposed [[WordPress:Inline Documentation]] standards. ;Single and double quotes :Use single and double quotes when appropriate. If you're not evaluating anything in the string, use single quotes. You should almost never have to escape HTML quotes in a string, because you can just alternate your quoting style, like so: <pre><nowiki>echo "<a href='$link' title='$linktitle'>$linkname</a>"; echo '<a href="/static/link" title="Yeah yeah!">Link name</a>';</nowiki></pre> :The only exception to this is JavaScript, which sometimes requires double or single quotes. Text that goes into attributes should be run through <tt>attribute_escape()</tt> so that single or double quotes do not end the attribute value and invalidate the XHTML and cause a security issue. ;Indentation : Your indentation should always reflect logical structure. Use '''real tabs''' and '''not spaces''', as this allows the most flexibility across clients. :Exception: if you have a block of code that would be more readable if things aligned, you can do initial indentation with tabs and then make up the difference with spaces: <pre><nowiki> [tab]$foo = 'somevalue'; [tab]$foo2 = 'somevalue2'; [tab]$foo34 = 'somevalue3'; [tab]$foo5 = 'somevalue4'; </nowiki></pre> :Note how tabs are used for the initial indentation, and spaces are just used to make sure the equals signs line up. ;Brace Style : Braces should be used for multiline blocks: <pre><nowiki> if ( condition ) { action1(); action2(); } elseif ( condition2 && condition3 ) { action3(); action4(); } else { defaultaction(); } </nowiki></pre> :Furthermore if you have a really long block, consider if it can be broken into two or more shorter blocks or function. If you consider such a long block unavoidable, please put a short comment at the end so people can tell at glance what that ending brace ends -- typically this is appropriate for a logic block, longer than about 35 rows, but any code that's not intuitively obvious can be commented. :single line blocks can omit braces for brevity: <pre><nowiki> if ( condition ) action1(); elseif ( condition2 ) action2(); else action3(); </nowiki></pre> ;include_once vs. require_once :Learn the difference between [http://us3.php.net/manual/en/function.include-once.php <tt>include_once</tt>] and [http://us3.php.net/manual/en/function.require-once.php <tt>require_once</tt>], and use each as appropriate. To quote the [http://us3.php.net/manual/en/function.include.php php manual page on include()]: "The two constructs are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error." Fatal errors stop script execution. ;Regular expressions: Perl compatible regular expressions (PCRE, <tt>preg_</tt> functions) should be used in preference to their POSIX counterparts. ;No Shorthand PHP :'''Never use shorthand PHP start tags''' (<tt><? ... ?></tt> or <tt><?=$var?></tt>). Always use full PHP tags (<tt><?php ... ?></tt>). '''Make sure to remove trailing whitespace after closing PHP tag.''' ;Space Usage :Always put spaces after commas and on both sides of logical and assignment operators like <tt>"x == 23", "foo && bar", "array( 1, 2, 3 )",</tt> , as well as on both sides of the opening and closing parenthesis of <tt>if</tt>, <tt>elseif</tt>, <tt>foreach</tt>, <tt>for</tt> and <tt>switch</tt> blocks (e.g. <tt>foreach ( $foo as $bar ) { ...</tt>). When defining a function, do it like so: <tt>function myfunction( $param1 = 'foo', $param2 = 'bar' ) {</tt> and when calling a function, do it like so: <tt>myfunction( $param1, funcparam( $param2 ) );</tt> ;Formatting SQL statements :When formatting SQL statements you may break it into several lines and indent if it is sufficiently complex to warrant it. Most statements work well as one line though. Always capitalize the SQL parts of the statement like <tt>UPDATE</tt> or <tt>WHERE</tt>. :Functions that update the database should expect their parameters to lack SQL slash escaping when passed. Escaping should be done as close to the time of the query as possible, preferably by using <tt>$wpdb->prepare()</tt> :<tt>$wpdb->prepare()</tt> is a method that handles escaping, quoting, and int-casting for SQL queries. It uses a subset of the <tt>sprintf()</tt> style of formatting. Example : <pre><nowiki> $var = "dangerous'"; // raw data that may or may not need to be escaped $id = some_foo_number(); // data we expect to be an integer, but we're not certain $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) ); </nowiki></pre> : <tt>%s</tt> is used for string placeholders and %d is used for integer placeholders. Note that they are not 'quoted'! <tt>$wpdb->prepare()</tt> will take care of escaping and quoting for us. The benefit of this is that we don't have to remember to manually use <tt>$wpdb->escape()</tt>, and also that it is easy to see at a glance whether something has been escaped or not, because it happens right when the query happens. ;Database Queries :Avoid touching the database directly. If there is a defined function that can get the data you need, use it. Database abstraction (using functions instead of queries) helps keep your code forward-compatible and, in cases where results are cached in memory, it can be many times faster. If you must touch the database, get in touch with some developers by posting a message to the [[WordPress:Mailing_Lists#Hackers|wp-hackers mailing list]]. They may want to consider creating a function for the next WordPress version to cover the functionality you wanted. ;Variables, functions, and operators :If you don't use a variable more than once, don't create it. This includes queries. Always use the [[WordPress:Function_Reference/wpdb Class|wpdb Class]] of functions when interacting with the database. :[[WordPress:Wikipedia:Ternary_operation|Ternary]] operators are fine, but always have them test if the statement is true, not false. Otherwise it just gets confusing. <pre><nowiki>// GOOD example: // (if statement is true) ? (do this) : (if false, do this); $musictype = ('jazz' == $music) ? 'cool' : 'blah'; </nowiki> </pre> :Another important point in the above example, when doing logical comparisons always put the variable on the right side, like above. If you forget an equal sign it'll throw a parse error instead of just evaluating true and executing the statement. It really takes no extra time to do, so if this saves one bug it's worth it.
摘要:
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Wordpress-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)
本页使用的模板:
模板:WordPress导航
(
查看源代码
)(受保护)