作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.
安德鲁•舒尔茨
验证专家 在工程

自2004年以来,拥有工程学位和强大的商业分析背景, Andrew’s lately focused on web development and WordPress.

Expertise

以前在

HSBC
Share

我们只是人,作为人的特征之一就是我们会犯错误. 另一方面, 我们也在自我纠正, 这意味着我们倾向于从错误中学习,并希望因此能够避免犯同样的错误两次. 我在WordPress领域所犯的许多错误都源于在执行解决方案时试图节省时间. However, 当这种方法导致问题出现时,他们通常会抬起头来. 犯错是不可避免的. 然而,从别人的疏忽中学习(当然还有你自己的疏忽)!是一条你应该主动走的路.

Engineers look like superheroes, but we’re still human. 向我们学习.

常见错误No. 1:关闭调试开关

Why should I use debugging when my code is working fine? 调试是WordPress内置的一个功能,它会导致所有PHP错误, warnings, 并注意(关于已弃用的函数), etc.)显示. 关闭调试时, 可能会生成一些我们从未看到的重要警告或通知, 但如果我们不及时处理,以后可能会产生问题. 我们希望我们的代码与我们网站的所有其他元素很好地配合. So, when adding any new custom code to WordPress, 在进行开发工作时,应该始终打开调试开关(但要确保在将站点部署到生产环境之前关闭调试开关)!).

To enable this feature, you’ll need to edit the wp-config.php file in the root directory of your WordPress install. 下面是一个典型文件的片段:

//打开调试开关
定义(“WP_DEBUG”,真正的);

// Log all errors to a text file located at /wp-content/debug.log
定义(“WP_DEBUG_LOG”,真正的);

//不显示错误信息,将其写入日志文件/wp-content/debug.log
定义(“WP_DEBUG_DISPLAY”,假);

//确保所有PHP错误被写入日志文件,而不是显示在屏幕上
@ini_set (display_errors, 0);

这不是可以使用的配置选项的详尽列表, 但是这个建议的设置应该足以满足大多数调试需求.

常见错误No. 2:添加脚本和样式使用 wp_head Hook

What is wrong with adding the scripts into my header template? WordPress already includes a plethora of 流行的脚本. Still, many developers will add additional scripts using the wp_head hook. 这可能导致相同的脚本,但不同的版本,被加载多次.

在这里排队可以解决问题, 在我们的网站上添加脚本和样式的WordPress友好的方式是什么. 我们使用排队来防止插件冲突,并处理脚本可能具有的任何依赖关系. This is achieved by using the inbuilt functions wp_enqueue_script or wp_enqueue_style to enqueue scripts and styles respectively. The main difference between the two functions is that with wp_enqueue_script 我们有一个额外的参数,它允许我们将脚本移动到页面的页脚.

Wp_register_script ($handle, $src, $deps = array(), $ver = false, $in_footer = false)
Wp_enqueue_script ($handle, $src = false, $deps = array(), $ver = false, $in_footer = false)

Wp_register_style ($handle, $src, $deps = array(), $ver = false, $media = 'all')
Wp_enqueue_style ($handle, $src = false, $deps = array(), $ver = false, $media = 'all')

If the script is not required to render content above the fold, 我们可以安全地将其移动到页脚,以确保折叠上方的内容快速加载. It’s 良好的实践 to register the script first before enqueuing it, 因为这允许其他人通过他们自己插件中的句柄注销你的脚本, without modifying the core code of your plugin. 除此之外, 如果已注册脚本的句柄列在已排队的另一个脚本的依赖项数组中, 该脚本将在加载高亮显示的已排队脚本之前自动加载.

常见错误No. 3: Avoiding Child Themes and Modifying WordPress Core Files

Always create a child theme if you plan on 修改主题. 一些开发人员会对父主题文件进行更改,但在升级到主题后才发现他们的更改已被覆盖并永远丢失.

要创建子主题,请放置 style.css 该文件位于子主题文件夹的子目录中,内容如下:

/*
 主题名称:二十六个孩子
 主题URI: http://example.com/twenty-fifteen-child/
 Description:  Twenty Fifteen Child Theme
 作者:约翰·多伊
 作者URI: http://example.com
 模板:twentysixteen
 版本:1.0.0
 License:      GNU General Public License v2 or later
 许可URI: http://www.gnu.org/licenses/gpl-2.0.html
 标签:浅色,深色,两栏,右侧栏,响应式布局,可访问性准备
 文本域:twenty-sixteen child
*/

上面的例子创建了一个基于默认WordPress主题的子主题, 二千零一十六年. 这段代码中最重要的一行是包含单词“Template”的那一行,它必须与要从中克隆子主题的父主题的目录名相匹配.

同样的原则也适用于WordPress核心文件:不要简单地修改核心文件. 通过使用WordPress可插拔功能和过滤器来防止WordPress升级后您的更改被覆盖,从而付出额外的努力. Pluggable functions let you override some core functions, 但这种方法正在慢慢被淘汰,取而代之的是过滤器. 过滤器实现了相同的最终结果,并且被插入到WordPress函数的末尾,以允许修改它们的输出. A trick is always to wrap your functions with if ( !function_exists ()) 当使用pluggable函数时,因为多个插件试图在没有此包装的情况下覆盖同一个pluggable函数将产生致命错误.

常见错误No. 4: Hardcoding Values

通常,在代码的某个地方硬编码一个值(比如URL)看起来更快, 但是在调试和纠正由此产生的问题上花费的时间要多得多. 通过使用相应的函数动态生成所需的输出, 我们极大地简化了代码的后续维护和调试. For example, 如果您使用硬编码的url将站点从测试环境迁移到生产环境, all of a sudden you’ll notice your site it is not working. 这就是为什么我们应该使用函数, 就像下面列出的那样, 用于生成文件路径和链接:

//获取子主题目录的uri
stylesheet_directory_uri ();
//获取父主题目录
get_template_directory_uri ();
//获取当前站点的url
 site_url();

Another bad example of hardcoding is when writing custom queries. 例如,作为一种安全措施,我们将默认的WordPress数据表前缀从 wp_ 去做一些更独特的事,比如 wp743_. Our queries will fail if we ever move the WordPress install, as the table prefixes can change between environments. To prevent this from happening, we can reference the 表属性 of the wpdb class:

全球wpdb美元;
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );

注意我没有使用这个值 wp_users 为表名,但我让WordPress解决它. 使用这些属性生成表名将有助于确保我们返回正确的结果.

常见错误No. 5: Not Stopping Your Site from Being Indexed

Why wouldn’t I want search engines to index my site? 索引很好,对吧? Well, 在建立网站时, 你不希望搜索引擎索引你的网站,直到你已经完成了建设,并建立了永久链接结构. Furthermore, if you have a staging server where you test site upgrades, 你不希望像谷歌这样的搜索引擎索引这些重复的页面. When there are multiple pieces of indistinguishable content, 搜索引擎很难决定哪个版本与搜索查询更相关. 在这种情况下,搜索引擎会惩罚内容重复的网站, and your site will suffer in search rankings as a result of this.

如下所示: WordPress阅读设置 有一个复选框,上面写着“阻止搜索引擎索引此站点”。, 尽管下面确实有一个重要的注意事项,说明“这取决于搜索引擎是否尊重此请求”。.

WordPress阅读设置

Bear in mind that search engines often do not 尊重这个请求. 因此,如果你想 reliably prevent search engines from indexing your site, edit your .htaccess 文件并插入以下行:

Header set X-Robots-Tag "noindex, nofollow"

常见错误No. 6:不检查插件是否激活

如果我的插件总是打开,为什么我要检查插件函数是否存在? For sure, 99% of the time your plugin will be active. 然而,由于某种原因它被停用的那1%的时间呢? 如果发生这种情况,您的网站可能会显示一些丑陋的PHP错误. 为了防止这种情况,我们可以在调用插件函数之前检查插件是否处于活动状态. 如果插件函数是通过前端调用的,我们需要包含 plugin.php 以调用该函数 is_plugin_active ():

include_once (ABSPATH . “wp-admin /包括/插件.php' );
if ( is_plugin_active( 'plugin-folder/plugin-main-file.php' ) ) {
//运行插件代码
}

This technique is usually quite reliable. 但是,也有可能是作者更改了主插件目录名的情况. 一个更健壮的方法是检查插件中是否存在一个类:

if(class_exists(' WooCommerce ')) {
	//打开WooCommerce插件
}

Authors are less likely to change the name of a plugin’s class, so I would generally recommend using this method.

常见错误No. 7:加载太多资源

Why should we be selective in loading plugin resources for pages? 如果用户导航到的页面上没有使用插件,那么就没有理由加载插件的样式和脚本. By only loading plugin files when necessary, 我们可以减少页面加载时间, which will result in an improved end user experience. Take, for example, WooCommerce网站, where we only want the plugin to be loaded on our shopping pages. 在这种情况下, 我们可以有选择地删除所有其他网站页面上加载的文件,以减少臃肿. We can add the following code to the theme or plugin’s functions.php file:

函数load_woo_scripts_styles () {
	
if( function_exists( 'is_woocommerce' ) ){
    // Only load styles/scripts on Woocommerce pages   
	if(! is_woocommerce () && ! is_cart() && ! Is_checkout ()) { 		
		
		//取消脚本队列.
		wp_dequeue_script(“woocommerce”); 
		wp_dequeue_script(“wc-add-to-cart”); 
		wp_dequeue_script('wc-cart-fragments');
		
		//取消队列样式.	
		wp_dequeue_style('woocommerce-general'); 
		wp_dequeue_style('woocommerce-layout'); 
		wp_dequeue_style('woocommerce-smallscreen'); 
			
		}
	}	
}

add_action( 'wp_enqueue_scripts', 'load_woo_scripts_styles');

Scripts can be removed with the function wp_dequeue_script(处理) via the handle with which they were registered. Similarly, wp_dequeue_style(处理) will prevent stylesheets from being loaded. 但是,如果这对您来说太难实现,您可以安装 插件的组织者 这提供了基于特定标准选择性加载插件的能力, 例如帖子类型或页面名称. It’s a good idea to disable any caching plugins, like W3Cache, 您可能已经打开,以防止您必须不断刷新缓存以反映您所做的任何更改.

常见错误No. 8 .保留管理栏

Can’t I just leave the WordPress Admin Bar visible for everyone? Well, yes, you could allow your users access to the admin pages. However, 这些页面通常不能在视觉上与您选择的主题集成,也不能提供无缝集成. If you want your site to look professional, 你应该禁用管理栏,并提供自己的前端帐户管理页面:

add_action('after_setup_theme', 'remove_admin_bar');

函数remove_admin_bar() {
if (!current_user_can(“管理人”) && !is_admin ()) {
  show_admin_bar(假);
}
}

The above code, when copied into your theme’s functions.php 文件将只显示管理员栏的网站管理员. 你可以添加任何WordPress user roles 或者能力 current_user_can(能力) function to exclude users from seeing the admin bar.

常见错误No. 9:没有使用GetText过滤器

我可以使用CSS或JavaScript来更改按钮的标签,这有什么问题? 嗯,是的,你可以. However, you’re adding superfluous code and extra time to render the button, when you can instead use one of the handiest filters in WordPress, called gettext. 结合一个插件的 textdomain, 一个唯一的标识符,确保WordPress可以区分所有加载的翻译, 我们可以使用 gettext filter to modify the text before the page is rendered. If you search the source code for the function load_plugin_textdomain(域),它会给你的域名,我们需要覆盖有问题的文本. Any reputable plugin will ensure that the textdomain for a plugin is set on initialization of the plugin. 如果是要更改的主题中的某些文本,请搜索 load_theme_textdomain(域) line of code. Using WooCommerce once again as an example, 我们可以更改出现在“相关产品”标题的文本. Insert the following code into your theme’s functions.php file:

函数translate_string($translated_text, $untranslated_text, $domain) {
	if ( $translated_text == 'Related Products') {
			$translated_text = __( 'Other Great Products', 'woocommerce' );
	}
	返回translated_text美元;
}

add_filter( 'gettext', 'translate_string', 15, 3 );

这个过滤器钩子由国际化函数应用于翻译后的文本 __() and _e(),只要 textdomain 是通过上述函数设置的吗.

_e( 'Related Products', 'woocommerce' );

在插件中搜索这些国际化函数,以查看还可以自定义哪些字符串.

默认情况下,WordPress使用带有帖子ID的查询字符串来返回指定的内容. 但是,这不是用户友好的,用户在复制URL时可能会删除URL的相关部分. 更重要的是,这些默认永久链接没有使用搜索引擎友好的结构. 启用我们所谓的“漂亮”永久链接将确保我们的url包含文章标题的相关关键字,以提高搜索引擎排名的性能. 回顾性地修改您的永久链接可能是一项相当艰巨的任务, 特别是如果您的站点已经运行了很长一段时间, 你已经有数百篇文章被搜索引擎索引了. So after you’ve installed WordPress, ensure you promptly 将您的永久链接结构更改为更适合搜索引擎的东西,而不仅仅是帖子ID. I generally use the post name for the majority of sites I build, 但是您可以使用可用的自定义永久链接为您喜欢的任何格式 永久链接结构标签.

WordPress永久链接设置

Conclusion

这篇文章绝不是一个详尽的错误清单 WordPress的开发者. If there’s one thing you should take away from this article, though, 那就是你永远不应该走捷径(在任何开发平台上都是如此), 不仅仅是WordPress!). 现在由于糟糕的编程实践而节省的时间以后会回来困扰您. 欢迎在下面留言,与我们分享你过去犯过的错误,更重要的是你学到的经验教训.

聘请Toptal这方面的专家.
Hire Now
安德鲁•舒尔茨

安德鲁•舒尔茨

验证专家 在工程

阿德莱德,南澳大利亚,澳大利亚

2016年7月11日成为会员

作者简介

自2004年以来,拥有工程学位和强大的商业分析背景, Andrew’s lately focused on web development and WordPress.

作者都是各自领域经过审查的专家,并撰写他们有经验的主题. 我们所有的内容都经过同行评审,并由同一领域的Toptal专家验证.

Expertise

以前在

HSBC

世界级的文章,每周发一次.

By entering your email, you are agreeing to our 隐私政策.

世界级的文章,每周发一次.

By entering your email, you are agreeing to our 隐私政策.

Toptal开发者

加入总冠军® community.