在本系列的前一部分中,我们了解了如何将 wordpress 安装与插件和主题捆绑在一起,以及如何调整 wp-config-sample.php 文件。
当然,捆绑主题和插件并不是一个令人惊奇的想法,但您必须承认编辑 wp-config-sample.php 以使用自定义 wp-config.php 调整来安装 wordpress 有点酷。另外,这两个技巧与我们将在本教程中看到的神奇技巧结合使用,并完成一种非常有用的方法,为您的未来项目使用开箱即用的 wordpress 安装。
在这一部分中,我们将发现一个令人兴奋的发现,即在安装 wordpress 时激活捆绑的主题和插件。您将首先在 tuts+ 上看到这一点,因为直到今天它才在互联网上的任何地方公开。
兴奋起来。
我见过的最方便的 wordpress 常量之一:wp_default_theme大约一年前,我在 tuts+ 上分享了一个关于使用 wp-config-sample.php 文件来自定义生成的 wp-config.php 文件之前的一个小发现安装 wordpress。这是一个名为 wp_default_theme 的已知 wp-config.php 常量的示例 - 如果您有兴趣,可以在此处阅读这篇文章。
如果你从默认的wordpress包中删除了所有默认的“twenty-something”主题,wordpress在安装后会给你一个错误而不是前端,因为每个wordpress版本都带有一个“默认主题”并且它如果默认主题不存在,则不会在 wp-content/themes 文件夹中查找另一个主题。
这就是为什么在写完那篇文章后,我认为我可以在另一个教程中使用这个调整,称为“构建开箱即用的 wordpress 包”。我只是记下了标题,并不想创建大纲,并将注释留在我的电脑中几乎一年。 (谈论拖延......我应该写一篇关于它的文章。我应该记下来。)
十多个月后,我决定创建一个大纲并提交到 tuts+ code 的项目管理系统,并得到我们的编辑 tom mcfarlin 的批准。当他批准了大纲并且我开始编写我最初想到的单部分教程时,我开始考虑 wp_default_theme。
虽然花了两天时间思考 wordpress 常量有点不寻常,但我最终想到可以使用这个常量以及在安装 wordpress 之前编辑 wp-config-sample.php 的技巧来执行一些操作我通常使用“入门插件”执行的任务(例如删除默认帖子和页面、更改永久链接结构和禁用评论)。然后我意识到我可以激活一些预先与软件包捆绑在一起的插件。然后我意识到我可以在完成这种主题后将主题切换到真实主题。
然后我突然想到:所有这一切意味着我实际上可以在安装 wordpress 时自动激活预捆绑的插件和主题!您可能可以从您现在阅读的文字中感受到我的兴奋 - 想象一下当我做出这一发现时我的感受。
这是一个解决方法吗?绝对地。您甚至可以将其称为 wordpress“黑客”。但它不会编辑任何核心文件(除了 wp-config-sample.php ,我们可以编辑它),并且除了“功能代码是插件领域”之外,它不违反任何 wordpress 约定,但我相信使用一个在一秒钟内自行停用的“一次性主题”并不是“不合规矩”。最后,它不会破坏任何文件或规则,并且它是开箱即用的 wordpress 安装的完全安全的解决方案。
制作“热身乐队”主题现在我们已经了解了要做的事情的逻辑,是时候创建一次性“热身带”主题了。
在此主题中,只有两个文件:强制的 style.css 和 functions.php 文件,该文件将运行我们的四部分代码:
更改默认选项删除默认内容激活我们预捆绑的插件切换到“headliner”主题我把 style.css 文件内容放在下面供您复制:
/*theme name: warm-up bandauthor: baris unver from tuts+ codedescription: disposable theme to run some errands.version: 0*/
更改默认选项wordpress 不允许您更改默认选项,因为如果这样做的话,安装时间会更长。但这并不意味着您不能以编程方式更改它们。借助一些核心功能,可以轻松根据您的需求定制选项:
<?php// set the options to change$option = array( // we don't want no description 'blogdescription' => '', // change category base 'category_base' => '/cat', // change tag base 'tag_base' => '/label', // disable comments 'default_comment_status' => 'closed', // disable trackbacks 'use_trackback' => '', // disable pingbacks 'default_ping_status' => 'closed', // disable pinging 'default_pingback_flag' => '', // change the permalink structure 'permalink_structure' => '/%postname%/', // dont use year/month folders for uploads 'uploads_use_yearmonth_folders' => '', // don't use those ugly smilies 'use_smilies' => '');// change the options!foreach ( $option as $key => $value ) { update_option( $key, $value );}// flush rewrite rules because we changed the permalink structureglobal $wp_rewrite;$wp_rewrite->flush_rules();?>
如您所见,我们:
首先创建一个选项及其值的关联数组在 foreach 循环中运行数组,以便对每个数组项使用 update_option() 函数刷新了重写规则,因为我们更改了永久链接结构您可以使用很多很多默认选项 - 在 wp-admin/includes/schema.php 文件中查看它们。
删除默认内容现在我们已经更改了一些默认选项,是时候删除我们总是手动删除的不需要的内容了。这个更容易:
<?php// delete the default comment, post and pagewp_delete_comment( 1 );wp_delete_post( 1, true );wp_delete_post( 2, true );?>
激活捆绑插件还记得我们在上一部分中决定将我们的包与三个流行的插件捆绑在一起吗?我们选择了 wp super cache、yoast 的 wordpress seo 和 contact form 7。现在让我们激活它们:
<?php// we need to include the file below because the activate_plugin() function isn't normally defined in the front-endinclude_once( abspath . 'wp-admin/includes/plugin.php' );// activate pre-bundled pluginsactivate_plugin( 'wp-super-cache/wp-cache.php' );activate_plugin( 'wordpress-seo/wp-seo.php' );activate_plugin( 'contact-form-7/wp-contact-form-7.php' );?>
我们还可以停用默认的 akismet 和 hello dolly 插件,但我相信您已经像我一样从包中删除了它们。
切换到“头条新闻”主题一切都已设置,现在我们可以切换到我们要使用的实际主题!这是最简单的部分,因为我们将运行 switch_theme() 函数,并将主题的文件夹名称作为参数:
<?php// switch the theme to headlinerswitch_theme( 'headliner' );?>
简单易行!
完整的 functions.php 文件<?php// set the options to change$option = array( // we don't want no description 'blogdescription' => '', // change category base 'category_base' => '/cat', // change tag base 'tag_base' => '/label', // disable comments 'default_comment_status' => 'closed', // disable trackbacks 'use_trackback' => '', // disable pingbacks 'default_ping_status' => 'closed', // disable pinging 'default_pingback_flag' => '', // change the permalink structure 'permalink_structure' => '/%postname%/', // dont use year/month folders for uploads 'uploads_use_yearmonth_folders' => '', // don't use those ugly smilies 'use_smilies' => '');// change the options!foreach ( $option as $key => $value ) { update_option( $key, $value );}// flush rewrite rules because we changed the permalink structureglobal $wp_rewrite;$wp_rewrite->flush_rules();// delete the default comment, post and pagewp_delete_comment( 1 );wp_delete_post( 1, true );wp_delete_post( 2, true );// we need to include the file below because the activate_plugin() function isn't normally defined in the front-endinclude_once( abspath . 'wp-admin/includes/plugin.php' );// activate pre-bundled pluginsactivate_plugin( 'wp-super-cache/wp-cache.php' );activate_plugin( 'wordpress-seo/wp-seo.php' );activate_plugin( 'contact-form-7/wp-contact-form-7.php' );// switch the theme to headlinerswitch_theme( 'headliner' );?>
结束虽然 wordpress 以其“五分钟安装过程”而闻名,但我相信,如果您知道自己在做什么,还可以节省几分钟。通过我们在本系列中介绍的内容,您可能会在 wordpress 安装过程之前和过程中获得更多时间。
您对创建自动化 wordpress 安装有何看法?您认为该系列还有更多改进的空间吗?请在下面的评论部分写下您的想法,告诉我们您的想法。如果您喜欢该系列,请不要忘记分享这两个部分!
以上就是activating plugins & themes during wordpress installation的详细内容。