WordPress 主题开发 - (二) 理解主题 待翻译
What is “Theme Sense”?
What is “Theme Sense”? Theme Sense is an intuitive understanding of WordPress themes: the relationship between the files that make up a theme, and how those files fit in with WordPress. Theme Sense is what you’ll have at the end of this tutorial.
什么是主题感(Theme Sense)?主题感就是对WordPress的一个直觉感知,即组成主题的各个文件之间的关系,并且这些文件是如何与WordPress适配的。(译注:主题与wordpress的交互,都是通过action这样的机制注入的)。主题感是你在本教程结束时将要掌握的技能。
Theme Sense is not about memorizing code
Let’s think about mathematics and “number sense” for a second. If you teach a kid to just memorize arithmetic facts with flashcards, without explaining why the answers are what they are, that kid may struggle when they encounter a fact they haven’t memorized. Being able to demonstrate, for example, 5+2 = 7 using blocks helps the kid “see” how numbers work. They can then use this knowledge to help solve any problem they run into in the future, even if they haven’t memorized the answer.
Theme Sense works the same way. It’s important to focus on the larger picture of what you’re trying to achieve, instead of memorizing specific bits of code.
How do I develop my Theme Sense?
You don’t have to understand the mechanics behind every single line of PHP.
If you’ve read this far, you’re mostly there. Theme Sense is about being aware of what you’re doing, and making conscious, informed decisions along the way. For example, if I asked you to design me a car, you’d instantly know what parts to include in your design — engine, frame, tires, windows, exhaust pipe, and so on. You might not know how every single part works, but you’d be able to explain why you need them in the design, and you’d know to check the quality of the parts — super important for safety. Your years of experience driving and riding in cars has led to your “car sense”.
Use a similar approach when approaching your next theme project. Great questions to ask yourself are: “Which files do I need to include in my theme? What roles do these files play? Why am I about to paste this bit of code into my theme? Where did this bit of code come from, and who wrote it? What purpose does this code bit serve? Is it up to date? Is it safe?”
You don’t have to understand the mechanics behind every single line of PHP, because it may change two, three, or four release cycles down the road. What’s important is that, like with cars, you’re able to demonstrate a macro understanding of what your theme is doing under the hood.
That said, here are some specific things you can do to develop that macro understanding and to keep your Theme Sense sharp.
Know “What’s New” with WordPress
WordPress code is all over the web. It’s even found its way offline, and into books and magazines. When you gaze at stars in the night sky, you’re seeing them as they existed millions of years ago. Likewise, when you examine a WordPress code sample, that sample is a snapshot of WordPress as it existed at the time of writing, and not necessarily WordPress as it exists now.
Awareness of the latest WordPress versions and major new features will help you evaluate the timeliness of WordPress tutorials, books, and plugins. To keep up with WordPress releases, subscribe to the WordPress News blog.
Get in the habit of checking dates
The first thing you should check before reading any tutorial or book is the date it was published. Remember how important it is to know “what’s new” with WordPress? That will help you evaluate which parts of a tutorial are still relevant, and which code is still current enough to use in your theme. As you may already know, it’s dangerous to use obsolete code in a theme because it may have known vulnerabilities that hackers can exploit. Relevancy is not an all-or-nothing deal, though, and code doesn’t come with expiration dates. You must determine relevancy on a case-by-case basis.
If you do use outdated code in a theme, it’s not the end of the world. There are plugins, such as Developer andTheme-Check, that will help you optimize your WordPress theme development environment and scan your theme’s code for currency, respectively.
In addition to the developer plugins, it’s great to get into the habit of adopting the best security practices listed in the Theme Security and Privacy Guidelines on the Theme Review page at the WordPress Codex. Scan the list and make note of any that refer to features you’re using in your theme. For example, item that refer to data validation and sanitation are necessary to protect your site fromCross-Site-Scripting (XSS) attacks.
I think the principle behind the second item on the list is especially pertinent when we’re talking about code relevance and timeliness:
Themes are required to implement Theme Options and Theme Settings pages deliberately, rather than relying on copy-and-paste scripts from website tutorials.
Copy-and-paste scripts may become obsolete very quickly. This applies not only to theme options code, but code from any tutorial. I mostly just want you to know to be aware of the importance of timeliness and keeping your theme as secure as possible.
Understand the essential elements of WordPress themes
Most websites, whether they run WordPress or not, have the same general sections: a header area, a main content area, a sidebar, and a footer. It might be helpful to think of these sections as blocks (or car parts, your choice). Some blocks, such as the header, footer, content, and sidebar, you’ll use in every theme you develop. You just might stick them together differently depending on the purpose your theme is trying to serve for users. With WordPress themes, template files such as header.php, index.php, sidebar.php, and footer.php are your “blocks”. Therefore, the Theme is the completed structure that you build with these blocks.
Awareness of these essential elements will go a long way in helping you to develop theme sense. For a quick primer on the parts of a WordPress theme, check out the The Theme Development page on the WordPress Codex.
Know where to find the latest coding examples and standards
The WordPress.org Codex is a great starting point to research WordPress functions and template tags that you find in tutorials. If a function is deprecated, The Codex page for that function will note it and point you to the most current alternative. You can also find a list on the Codex of all deprecated functions.
The Log Deprecated Notices plugin scans your theme for deprecated functions, as well as outdated template files and incorrect uses of functions.
The WordPress Theme Review Team has a set of guidelines that all themes must meet in order to be approved for the official WordPress.org free themes directory. Evaluating your code against these standards can help you to stay abreast of best practices and standards.
Finally, when in doubt about something, there are places you can go to ask questions, such as the Themes & Templates section on the WordPress.org Support forums. If you prefer live chat, there’s the general #wordpress support channel on the irc.freenode.net network.
This is not to say that you should never break out of the mold when building themes. Rather, the idea is that it’s smart to be aware of the decisions you’re making for each theme you develop and why you are making these decisions.
Take-aways
The points to take away from this lesson are:
- Be aware of how WordPress looks now.
- Know what major new features were introduced in the latest version of WordPress.
- Pay attention to the dates on all WordPress-related tutorials. Use those, combined with your knowledge of the newest WordPress features to evaluate the relevancy of any given tutorial.
- Understand that code changes regularly, but the basic building blocks of a theme stays generally the same.
- Be aware of current best practices and standards. This can also come in handy when evaluating a tutorial’s relevancy.
- Understand why you are making the decisions you’re making for your theme — for example, what purpose does each template file in your theme serve? If you’re about to paste a large block of code into your theme that you dug up from Google, what purpose is that code serving? Is the code relevant? Up to date? Secure? You don’t have to memorize every line of PHP. What’s important is that you’re able to comprehend your theme on a macro level.
WordPress 主题开发 - (二) 理解主题 待翻译的更多相关文章
- WordPress 主题开发 - (十三) Archive模板 待翻译
What archive.php does (and all its related templates) is show posts based on a select criteria. A da ...
- WordPress 主题开发 - (七) 让主题更安全 待翻译
We're just about ready to start building our theme's template files. Before we do this, however, it' ...
- WordPress 主题开发 - (八) Head模板 待翻译
THE WORDPRESS THEME HEADER TEMPLATE Now we get into the nitty-gritty: building up your header.php an ...
- WordPress 主题开发 - (六) 创建主题函数 待翻译
We’ve got a file structure in place, now let’s start adding things to them! First, we’re going to ad ...
- WordPress主题开发:开启文章缩略图功能
安装wordpress后,默认的主题里编辑文章都会看见这个缩略图功能,那么我们自己开发的新主题怎么有这个功能呢? 目录: 一.开启缩略功能 二.设置缩略图大小 三.编辑文章,上传缩略图 四.调用缩略图 ...
- 黄聪:《跟黄聪学WordPress主题开发》
又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...
- WordPress主题开发:开启feed功能
开启feed功能 步骤一:在模版文件的<head></head>元素中添加wp_head()函数,且wp_head()函数要放在</head>标签之前,而且紧邻&l ...
- wordpress 主题开发
https://yusi123.com/3205.html https://themeshaper.com/2012/10/22/the-themeshaper-wordpress-theme-tut ...
- WordPress 主题开发:从入门到精通(必读)
本专栏介绍如何开发设计你自己的 WordPress 主题.如果你希望了解更多如何安装和应用主题的内容,请参阅应用主题文档.本文的内容不同于应用主题,因为所讨论的是编写代码去构建你自己的主题的技术内容, ...
随机推荐
- EF——Guid类型数据的自增长、时间戳和复杂类型的用法 03 (转)
EF里Guid类型数据的自增长.时间戳和复杂类型的用法 通过前两章Lodging和Destination类的演示,大家肯定基本了解Code First是怎么玩的了,本章继续演示一些很实用的东西.文 ...
- 爬虫入门scrapy
Python之路[第十九篇]:爬虫 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用 ...
- 实用技术——Eclipse 常用快捷键
Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率. 和编辑相关的快捷键 Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] 此快捷键为用户编 ...
- spring mvc 重新定向到一个新的Url
在写项目的时候要求根据请求的参数的不同重新将请求分发,在查阅了spring mvc的一些资料无果后(想使用拦截器去做)就没办法使用重定向的方式去写了 /** * 通过访问API的方式分发请求 * * ...
- 【JS对象、JSON字符串】之间的相互转换
在Firefox,chrome,opera,safari,ie9,ie8等浏览器直接可以用JSON对象的stringify()和parse()方法. 1.JSON.stringify(obj)将JS对 ...
- XAML设计器卡死
在生成工程时,存在这样一个记录: “未能找到一个或多个间接引用的程序集.分析不需要这些程序集.但是,如果没有这些程序集,分析结果可能不完整”. 表现形式既不是错误,可也不是警告.之所以关注到这个问题, ...
- Winform---文件夹操作
一.文件夹的操作 private void button1_Click(object sender, EventArgs e) { //文件夹操作 ////新建文件夹 //Directory.Crea ...
- JS内存泄露常见原因
详细内容请点击 分享的笔记本-前端 开发中,我们常遇见的一些关于js内存泄露的问题,有时候我们常常会找半天找不出原因,这里给大家介绍简单便捷的方法 1.闭包上下文绑定后没有释放: 2.观察者模式在 ...
- 【CSS3】---结构性伪类选择器—nth-child(n)+nth-last-child(n)
结构性伪类选择器—nth-child(n) “:nth-child(n)”选择器用来定位某个父元素的一个或多个特定的子元素.其中“n”是其参数,而且可以是整数值(1,2,3,4),也可以是表达式(2n ...
- 【CSS3】---文本阴影text-shadow
text-shadow可以用来设置文本的阴影效果. 语法: text-shadow: X-Offset Y-Offset blur color; X-Offset:表示阴影的水平偏移距离,其值为正值时 ...