wordpress主题制作
Want to develop a sense of deep contempt for your fellow web developers and designers? Then try managing a multi-site WordPress install that uses SSL.
是否想对您的Web开发人员和设计师产生一种深切的鄙视感? 然后尝试管理使用SSL的多站点WordPress安装。
Some would have you believe that the WordPress ecosystem is a veritable Shangri-La of themes and plugins. I want to destroy this illusion: it’s not. If you’ve had to manage a WordPress site or two, you’ll know what I mean when I say WordPress themes and plugins can be a labyrinth of compatibility problems and a morass of bad programming practices; full of empty promises. This article is my small contribution to making the world a better place; making my life have meaning; and assuaging the pain.
有人会让您相信WordPress生态系统是名副其实的香格里拉主题和插件。 我想消除这种幻想:不是。 如果您必须管理一个或两个WordPress网站,当我说WordPress主题和插件可能会引起兼容性问题的迷宫和不良的编程习惯时,您会明白我的意思。 充满了空洞的承诺。 本文是我对使世界变得更美好的一小部分贡献; 使我的生活有意义; 并减轻痛苦。
This is a list of crimes against WordPress. I found all of these questionable practices in themes and plugins that made my life a living hell.
这是针对WordPress的犯罪列表。 我在主题和插件中发现了所有这些可疑的做法,这使我的生活变得井井有条。
I discovered this in a theme:
我在一个主题中发现了这一点:
$my_jquery = get_bloginfo('template_url'). "/js/jquery.old.version.js"; wp_deregister_script( 'jquery' ); wp_register_script( 'jquery', $my_jquery, false, '' ); wp_enqueue_script('jquery');WordPress comes with jQuery included; please make sure your plugin or theme is compatible with the jQuery version WordPress includes. Never include jQuery with your plugin or theme. What do you think happens when your version of jQuery is a step or two behind the version included by WordPress, hmmm?
WordPress包含jQuery。 请确保您的插件或主题与WordPress包含的jQuery版本兼容。 切勿在插件或主题中包含jQuery。 当您的jQuery版本比WordPress所包含的版本hmmm落后一两步时,您会怎么想?
Adding code like that above feels like you’re invading my personal space. If your JavaScript code requires jQuery then simply do this:
像上面那样添加代码,感觉就像您在侵犯我的个人空间。 如果您JavaScript代码需要jQuery,则只需执行以下操作:
wp_enqueue_script('jquery');Ever known someone who finishes all your sentences for you? Themes and plugins that output page text that you can’t change feel the same way.
有没有人知道为您完成所有句子的人? 输出无法更改的页面文本的主题和插件的感觉也相同。
Here’s an example: if you’re a theme maker and you want to show a page submenu in the side column, avoid hard-coding it into your template; make it a widget. Allow the site owner to position it and add a custom title. The site owner may dislike the title “Sub Navigation” and prefer “In This Section.” Why do you assume you have the right to make all the decisions?
这是一个示例:如果您是主题制作者,并且想要在侧栏中显示页面子菜单,请避免将其硬编码到模板中; 使其成为小部件。 允许网站所有者放置它并添加自定义标题。 网站所有者可能不喜欢标题“ Sub Navigation”,而更喜欢“ In this Section”。 为什么您认为自己有权做出所有决定?
At least provide an options panel.
至少提供一个选项面板。
This is just plain cruel; you may as well start throwing rusty nails into my eyes, because that’d be more fun than tracking down the cause of the CSS bugs that arise because of this:
这简直是残酷的。 您不妨开始将生锈的钉子扔进我的眼睛,因为这比追查由于以下原因而引起CSS错误的原因更有趣:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">I found that in a theme I actually paid for. If you’re a theme maker and you’re unable to make your CSS work in IE unless it emulates IE7, you need to go back to the drawing board. Seriously bad form.
我发现这是我实际付费的主题。 如果您是主题制作者,并且除非它模仿IE7,否则您将无法在IE中使用CSS,那么您需要返回到绘图板上。 表格格式严重错误。
A WordPress theme is all about presentation. Providing features like Google Analytics support is unnecessary and just adds to the clutter of your theme options page, like a colleague who never shuts up.
WordPress主题就是关于演示。 不需要提供诸如Google Analytics(分析)支持之类的功能,而只会增加主题选择页面的混乱程度,就像从未关闭过的同事一样。
If I want to use Analytics, I can get a plugin for that. Besides, what if I want to use a site stats service other than Google Analytics?
如果我想使用Google Analytics(分析),可以获取一个插件。 此外,如果我想使用Google Analytics(分析)以外的网站统计服务怎么办?
If the site owner wants to remove your plugin or theme, don’t begrudge them the option to do so; it’s their site, after all. Leaving all your data lying around is like being a bad tenant who trashes the joint when they leave.
如果网站所有者想要删除您的插件或主题,请不要误解他们的选择; 毕竟是他们的网站。 将您的所有数据留在身边就像是一个坏租户,他们离开时会浪费接头。
Plugin authors, make sure sure you add actions for the deactivation of your plugin. For example:
插件作者,请确保您添加了用于停用插件的操作。 例如:
register_deactivation_hook(__FILE__,'my_plugin_deactivation'); function my_plugin_deactivation() { //get rid of stored data here }It’s simple to add and you can read all about it on the WordPress Codex.
添加起来很简单,您可以在WordPress Codex上阅读全部内容。
There’s currently no official method for themes to assign actions to a deregistration event, but here’s an approach that looks promising.
目前,尚无官方的主题分配活动给注销事件的方法 ,但这是一种很有希望的方法 。
If you’re not going to add a call to wp_footer in your theme you may as well steal my shoes. This is the most basic no-no—so many plugins depend upon this happening—and yet you still find themes, even commercial ones, that fail to call wp_footer.
如果您wp_footer在主题中添加对wp_footer的调用,则wp_footer偷走我的鞋子。 这是最基本的禁止行为-许多插件都取决于这种情况-但您仍然会发现无法调用wp_footer主题,甚至是商业主题。
You need to place <?php wp_footer(); ?> just before the closing </body> tag.
您需要放置<?php wp_footer(); ?> <?php wp_footer(); ?>就在结束</body>标记之前。
There are no arguments here. Just do it.
这里没有论据。 去做就对了。
Comparable to turning up late to your best friend’s wedding, I’ve noticed theme and plugin makers sometimes initialize important global variables or constants inside a function that is bound to wp_head. For example:
与您最好的朋友的婚礼迟到相比,我注意到主题和插件制造商有时会在绑定到wp_head的函数内初始化重要的全局变量或常量。 例如:
function my_init() { define('REALLY_IMPORTANT_CONSTANT', 'really_important_value'); } add_action('wp_head', 'my_init');Simply put, the wp_head action should only be used to output tags into the HTML head. If you ignore this rule you’re binding code logic with HTML output, and that’s generally bad.
简而言之, wp_head操作仅应用于将标签输出到HTML head 。 如果忽略此规则,则将代码逻辑与HTML输出绑定在一起,这通常是不好的。
Would you promise to bring a van to help your friend move house and then turn up on a motorbike? So why would you promise the feature of custom menus, but not use the WordPress custom menu feature? I recently paid for a theme that provided a field into which the site owner is supposed to enter a comma separated list of page IDs:
您是否愿意带一辆面包车来帮助您的朋友搬家,然后骑摩托车呢? 那么,为什么要承诺自定义菜单的功能,却不使用WordPress自定义菜单的功能呢? 我最近购买了一个主题,该主题提供了一个字段,网站所有者应该在该字段中输入逗号分隔的页面ID列表:
Enter the query string of the pages you want to display in the main menu (top left corner of the page). Example: include=9,10,20,21,32 (this would display 5 menu items for the pages with the id 9,10,20,21,32).
输入要在主菜单中显示的页面的查询字符串(页面的左上角)。 示例:include = 9,10,20,21,32(这将为ID为9,10,20,21,32的页面显示5个菜单项)。
If you offer custom menus in your theme, define menu positions and allow the site owner to use the WordPress custom menu maker; available in WordPress 3.0. it’s flexible and easy to use for the site owner; and the code to add custom menu slots to your theme is so simple. The Codex has all the code you need.
如果您在主题中提供自定义菜单,请定义菜单位置并允许网站所有者使用WordPress自定义菜单生成器; 在WordPress 3.0中可用。 网站所有者非常灵活且易于使用; 并且向主题添加自定义菜单栏的代码非常简单。 食品法典委员会拥有您所需的所有代码。
Here’s a great way to sentence the site owner to a life of hard labor. WordPress has had the post thumbnail feature since version 2.9, but you still find themes and plugins that force site-owners to manually paste image URLs into custom post fields.
这是判处网站所有者辛苦工作的好方法。 WordPress自2.9版起已具有帖子缩略图功能,但是您仍然可以找到主题和插件,它们迫使网站所有者手动将图像URL粘贴到自定义帖子字段中。
Instead create a few custom image sizes an then use the Post thumbnail feature. It turns hard labor into a set-and-forget option:
而是创建一些自定义图像尺寸,然后使用发布缩略图功能。 它将艰苦的工作变成了“一劳永逸”的选择:
add_theme_support( 'post-thumbnails'); if ( function_exists( 'add_image_size' ) ) { add_image_size( 'frontpage-news-thumb', 75, 45, true ); add_image_size( 'frontpage-news-large', 570, 300, true ); add_image_size( 'portfolio-page-thumb', 44, 30, true ); }With the above code, whenever an editor uploads an image, the additional sizes will be generated automatically. All the editor then needs to do is select the image as the feature image for the post.
使用以上代码,每当编辑者上传图像时,都会自动生成其他尺寸。 编辑者所需要做的就是选择图像作为帖子的特征图像。
In your theme or plugin you can output the appropriate image tag with the the_post_thumbnail function:
在主题或插件中,可以使用the_post_thumbnail函数输出适当的图像标签:
<?php the_post_thumbnail( 'frontpage-news-thumb' ); ?>Read all about it on the Codex. Please.
在法典上阅读所有相关内容。 请。
The add_meta_box function has been available since WordPress 2.5, but I still see the following:
自WordPress 2.5起, add_meta_box函数已可用,但我仍然看到以下内容:
add_action('edit_page_form', custom_write_panel); function custom_write_panel() { //echo lots of form HTML }The example custom_write_panel function above echos form HTML into the post edit form. This is equivalent to throwing up into the urn your best friend keeps her mother’s ashes in.
上面的示例custom_write_panel函数将表单HTML回显到后期编辑表单中。 这相当于把最好的朋友丢进妈妈的骨灰里丢进骨灰盒。
The add_meta_box function adds your custom write panel to the global $wp_meta_boxes array. Once there it can be accessed, overridden—generally managed by other code or a plugin. It’s the polite way to do it.
add_meta_box函数将您的自定义写入面板添加到全局$wp_meta_boxes数组中。 一旦可以访问,覆盖它,通常由其他代码或插件进行管理。 这是有礼貌的方式。
Read the function’s reference on the Codex for details and links to easy tutorials
阅读法典上的函数参考以获取详细信息和指向简单教程的链接
If you run a WordPress site under HTTPS, constant browser mixed content security warnings are almost inevitable, requiring you to modify the plugins and themes manually. This is because many theme and plugin authors hard-code ‘http’ when they include page resources like JavaScript files and Images. It feels like being conned into buying a brand new suit and realizing only the front half of the mannequin was covered. And now your butt’s hanging out in the breeze while you deliver your big speech to the shareholders.
如果您在HTTPS下运行WordPress网站,则不可避免的会出现浏览器不断出现混合内容安全警告的情况,这需要您手动修改插件和主题。 这是因为许多主题和插件作者在包含页面资源(例如JavaScript文件和图片)时都会硬编码“ http”。 感觉就像是要购买一套崭新的西装,而意识到只覆盖了模特的前半部分。 现在,当您向股东发表重要讲话时,您的屁股在微风中闲逛。
Since version 2.6 WordPress has had the is_ssl function:
从2.6版开始,WordPress具有is_ssl函数:
$scheme = ( is_ssl() ? 'https' : 'http' );Just use it before someone gets hurt.
只是在有人受伤之前使用它。
Hardcoding aside, there are times where you can’t really blame plugin and theme creators. After all many of them have simple copied code from WordPress itself. In WordPress 3.0.4, default-constants.php, line 77, we find:
除了硬编码,有时候您不能真正责怪插件和主题创建者。 毕竟,其中许多都是从WordPress本身复制的简单代码。 在WordPress 3.0.4的default-constants.php 77行中,我们发现:
if ( !defined('WP_CONTENT_URL') ) define( 'WP_CONTENT_URL', get_option('siteurl') . '/wp-content');Using get_option('siteurl') simply retrieves the value from the database without any SSL check. A better way is to use the site_url / get_site_url functions as these perform the appropriate check.
使用get_option('siteurl')只需从数据库中检索值,而无需任何SSL检查。 更好的方法是使用site_url / get_site_url函数,因为它们会执行适当的检查。
Alternatively you can do what I did and patch this yourself:
或者,您可以执行我所做的操作并自己打补丁:
function fix_ssl_siteurl($url) { if ( 0 === strpos($url, 'http') && is_ssl() ) $url = str_replace( 'http://', 'https://', $url ); return $url; } add_filter('option_siteurl', 'fix_ssl_siteurl'); add_filter('option_home', 'fix_ssl_siteurl'); add_filter('option_url', 'fix_ssl_siteurl'); add_filter('option_wpurl', 'fix_ssl_siteurl'); add_filter('option_stylesheet_url', 'fix_ssl_siteurl'); add_filter('option_template_url', 'fix_ssl_siteurl');You can place the above code in a plugin, in the functions.php file in a theme or, if you run a multi-site installation, put it in your mu-plugins folder and be done with it. Of course it’d be better if everyone just supported HTTPS properly.
您可以将以上代码放置在插件中,主题中的functions.php文件中,或者,如果您运行多站点安装,请将其放在mu-plugins文件夹中并完成操作。 当然,如果每个人都正确地支持HTTPS会更好。
If you claim to support WordPress 3.0 and up, then you must support multi-site WordPress installations. If you fail to do this you’re a schmuck. From my point of view the experience is like winning a family trip to Sea World, but finding out that only one of us is allowed to swim with the dolphins.
如果您声称支持WordPress 3.0及更高版本,则必须支持多站点WordPress安装。 如果您不这样做,那么您就是个傻瓜。 从我的角度来看,这种经历就像赢得一次海上世界家庭之旅一样,但是发现我们中只有一个被允许与海豚一起游泳。
Enabling multi-site on a WordPress install changes the file upload path, the URL to admin forms, and the database table names are unique for each blog. Also, plugins can be activated for the whole network. Writing about this topic could fill a whole tutorial itself. But here’s a few pointers:
在WordPress安装上启用多站点会更改文件上传路径,管理表单的URL和数据库表名称对于每个博客都是唯一的。 另外,可以为整个网络激活插件。 撰写有关该主题的文章可能会填满整个教程本身。 但是这里有一些提示:
In a multi-site WordPress installation the content file path depends upon a URL redirection. Often theme or plugin makers will include a supporting library, like an image manipulation library for example. If this is the case with your theme or plugin, make sure your library works with URL redirection. So many of them fail at this.
在多站点WordPress安装中,内容文件路径取决于URL重定向。 主题或插件制造商通常会包括一个支持库,例如图像处理库。 如果您的主题或插件属于这种情况,请确保您的库可用于URL重定向。 因此很多人都失败了。
One plugin I used wrote the options form action like this:
我使用的一个插件编写了如下的options表单动作:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] . '?page=' . basename(__FILE__); ?>&updated=true">The problem with this is that with multi-site WordPress, the path to the admin forms is different for each blog and not equal to $_SERVER['PHP_SELF']. Here’s the fix I used for that plugin; it basically changes the action from a root-relative path to just a plain relative one:
问题在于,对于多站点WordPress,每个博客的管理表单路径都不同,并且不等于$_SERVER['PHP_SELF'] 。 这是我用于该插件的修复程序; 它基本上将操作从根相对路径更改为普通相对路径:
<form method="post" action="<?php echo 'options-general.php?page=' . basename(__FILE__); ?>&updated=true">If your theme or plugin need custom database tables, then don’t forget in a multi-site installation each site has a unique table name prefix. Mostly this means using the global $wpdb object for database access and the $wpdb->prefix property. It’s all explained in the Codex and in this blog post.
如果您的主题或插件需要自定义数据库表,那么不要忘记在多站点安装中,每个站点都有一个唯一的表名前缀。 通常,这意味着使用全局$wpdb对象进行数据库访问,并使用$wpdb->prefix属性。 在法典和本博客中对此进行了解释。
The above blog post also has some useful examples for dealing with network activation and deactivation.
上面的博客文章也提供了一些有用的示例来处理网络激活和停用。
I feel a little better now that I’ve gotten that off my chest. It really comes down to this: do as little damage as you can, use existing WordPress features as much as possible, make everything customizable, and recognize that some sites need SSL or are multi-site installations.
现在,我已经摆脱了困境,现在感觉好多了。 这实际上可以归结为:尽可能少地造成损害,尽可能使用现有的WordPress功能,使所有内容可自定义,并认识到某些站点需要SSL或是多站点安装。
If site owners are forced to make edits to your code just to make it work acceptably, then you’ve failed, and the WordPress ecosystem isn’t working as it should.
如果网站所有者被迫对您的代码进行编辑以使其可以接受,那么您就失败了,并且WordPress生态系统无法正常工作。
翻译自: https://www.sitepoint.com/crimes-against-wordpress/
wordpress主题制作
相关资源:jdk-8u281-windows-x64.exe