search
HomeBackend DevelopmentPHP TutorialActivating Plugins & Themes during WordPress Installation

重写后的标题为: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.

One of the most convenient WordPress constants I’ve ever seen: 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.

Create a "Warm-up Band" theme

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:

  1. Change default options
  2. Delete default content
  3. Activate our pre-bundled plugins
  4. Switch to "Headliner" theme

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
*/

Change default options

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' );

?>

我们还可以停用默认的 AkismetHello 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor