Home >Backend Development >PHP Tutorial >Activating Plugins & Themes during WordPress Installation
In the previous part of this series, we looked at how to bundle a WordPress installation with plugins and themes, and how to adjust wp-config-sample.php
document.
Of course, bundling themes and plugins is not an amazing idea, but you have to admit editing wp-config-sample.php
to use custom wp-config.php
Tweaking to install WordPress is kind of cool. Plus, these two tips combine with the Magical tips we'll see in this tutorial and complete a very useful way to use an out-of-the-box WordPress installation for your future projects.
In this section we will make the exciting discovery of activating bundled themes and plugins when installing WordPress. You'll see this first on Tuts+, as it hasn't been made public anywhere on the internet until today.
Get excited.
WP_DEFAULT_THEME
About a year ago, I shared a post on Tuts+ about using a wp-config-sample.php
file to customize the generated wp-config.php
file A little discovery before installing WordPress. Here's an example of a known wp-config.php
constant called WP_DEFAULT_THEME
- you can read the article here if you're interested.
If you remove all the default "Twenty-Something" themes from the default WordPress package, WordPress will give you an error after installation instead of the frontend, because every WordPress version comes with a "Default Theme" and It will not look for another theme in the wp-content/themes
folder if the default theme does not exist.
That’s why after writing that article, I thought I could use this tweak in another tutorial called “Building an out-of-the-box WordPress package”. I just jotted down the title, not trying to create an outline, and left the notes on my computer for almost a year. (Talk about procrastination...I should write an article about it. I should write it down.)
More than ten months later, I decided to create an outline and submit it to Tuts+ Code’s project management system, and it was approved by our editor, Tom McFarlin. When he approved the outline and I started writing what I originally had in mind as a one-part tutorial, I started thinking about WP_DEFAULT_THEME
.
While it's a bit unusual to spend two days thinking about WordPress constants, I finally figured out that I could use this constant and the trick of editing wp-config-sample.php
before installing WordPress to perform some actions on my Tasks commonly performed using "starter plugins" (such as deleting default posts and pages, changing the permalink structure, and disabling comments). Then I realized that I could activate some plugins that were pre-bundled with the package. Then I realized that I could switch the theme to real theme after finishing this kind of theme.
Then it occurred to me: all this means is that I can actually automatically activate pre-bundled plugins and themes when I install WordPress! You can probably sense my excitement in the words you're reading now - imagine how I felt when I made this discovery.
Is this a workaround? Absolutely. You might even call it a WordPress “hack”. But it doesn't edit any core files (except wp-config-sample.php
, which we can edit), and it doesn't violate any WordPress conventions other than "function code is plugin domain", but I believe it's not "out of order" to use a "disposable theme" that deactivates itself within a second. Finally, it doesn't break any files or rules, and it's a completely secure solution for WordPress installations out of the box.
Now that we understand the logic of what we're going to do, it's time to create the disposable "warm-up band" theme.
In this theme, there are only two files: the mandatory style.css
and the functions.php
file, which will run our four-part code:
I put the contents of the style.css
file below for you to copy:
/* Theme Name: Warm-Up Band Author: Baris Unver from Tuts+ Code Description: Disposable theme to run some errands. Version: 0 */
WordPress does not allow you to change the default options because if you do, the installation will take longer. But that doesn't mean you can't change them programmatically. Options can be easily customized to your needs with a few core features:
<?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 structure global $wp_rewrite; $wp_rewrite->flush_rules(); ?>
As you can see, we:
foreach
循环中运行数组,以便对每个数组项使用 update_option()
函数您可以使用很多很多默认选项 - 在 wp-admin/includes/schema.php
文件中查看它们。
现在我们已经更改了一些默认选项,是时候删除我们总是手动删除的不需要的内容了。这个更容易:
<?php // delete the default comment, post and page wp_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-end include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); // activate pre-bundled plugins activate_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 "Headliner" switch_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 structure global $wp_rewrite; $wp_rewrite->flush_rules(); // delete the default comment, post and page wp_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-end include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); // activate pre-bundled plugins activate_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 "Headliner" switch_theme( 'headliner' ); ?>
虽然 WordPress 以其“五分钟安装过程”而闻名,但我相信,如果您知道自己在做什么,还可以节省几分钟。通过我们在本系列中介绍的内容,您可能会在 WordPress 安装过程之前和过程中获得更多时间。
您对创建自动化 WordPress 安装有何看法?您认为该系列还有更多改进的空间吗?请在下面的评论部分写下您的想法,告诉我们您的想法。如果您喜欢该系列,请不要忘记分享这两个部分!
The above is the detailed content of Activating Plugins & Themes during WordPress Installation. For more information, please follow other related articles on the PHP Chinese website!