wnet-intudio/yii2-wordpress-glue

使用 Yii2 框架开发 WordPress 插件的工具包

v2.0.0 2022-01-20 08:37 UTC

This package is auto-updated.

Last update: 2024-09-20 14:53:07 UTC


README

使用 Yii2 框架开发 WordPress 插件

快速开始

在例如 wp-content/plugins/awesome-plugin 的文件夹中创建一个新的 WordPress 插件文件夹,并在该文件夹中运行/执行以下任务

  • 创建至少包含以下内容的 composer.json
{
	"repositories": [
		{
			"type": "composer",
			"url": "https://asset-packagist.org"
		}
	]
}
  • 运行 composer require winternet-studio/yii2-wordpress-glue
  • 创建 WordPress 插件引导文件,例如 awesome-plugin.php(将 myplugin 替换为您的插件名称)
<?php
// wp-content/plugins/awesome-plugin/awesome-plugin.php

use winternet\yii2wordpress\KernelRunner;

require_once(__DIR__ .'/vendor/autoload.php');

$config = [
	'aliases' => [
		'@myplugin' => __DIR__,  //this will become your namespace, eg. myplugin\models\MyModel
	],
	'language' => 'en-US',
	'components' => [
		'cache' => [
			'class' => 'yii\caching\FileCache',
			'defaultDuration' => 600,
		],
		'i18n' => [
			'translations' => [
				'myApp*' => [
					'class' => 'yii\i18n\PhpMessageSource',
					'forceTranslation' => true,
					'basePath' => dirname(__DIR__) . '/messages',
				],
			],
		],
	],
];

$isDebug = true;  //set true during development
$kernel = new KernelRunner($config, null, $isDebug);
$kernel->run();

翻译

查看上面示例中的 $config-array:i18n 组件已配置 basePath 为 "plugin/messages"。有关更多信息,请参阅 Yii2 文档

迁移

  • 在您的应用程序中创建一个 "migrations" 文件夹(wp-content/plugins/awesome-plugin/migrations)
  • 在命令行中: vendor/bin/yii2wp migrate/create 'MyMigrationName'

在迁移文件夹中应生成一个新文件

<?php
class m191119_103515_createTables extends \yii\db\Migration {
	/**
	 * {@inheritdoc}
	 */
	public function safeUp() {
		$this->createTable(
			'{{%mymodule_mytable}}',
			array_merge(\winternet\yii2wordpress\helpers\MigrationHelper::getSystemCols(), [
				'foo' => $this->text(),
				'bar' => $this->string(10)
			])
		);
	}
}

核心迁移

在命令行中: vendor/bin/yii2wp migrate/create --migrationPath=@Yii2Wordpress/migrations 'AddTemplates'

鸣谢

这最初是 Henry Volkmer 项目的分支,但已经做了许多修改。