startplatz / wordpress-integration-bundle
WordPress 在 Symfony 中的集成
Requires
- php: >=7.3
- doctrine/dbal: >2
- sensio/framework-extra-bundle: ~3.4|~4.0|~5.0
- symfony/framework-bundle: ~3.4|~4.0|~5.0
This package is auto-updated.
Last update: 2024-09-12 21:52:25 UTC
README
开源包,用于在 Symfony2 项目中集成 WordPress。
特性
- 并行使用 WordPress 和 Symfony
- 使用 WordPress 的布局作为 Twig 模板(通过注解)
- 使用 Symfony 控制器输出作为 WordPress 短代码
安装
请确保已全局安装 Composer,具体请参考 Composer 文档中的安装章节。
使用 Symfony Flex 的应用程序
打开命令行,进入项目目录并执行以下命令
$ composer require wordpress-integration-bundle
不使用 Symfony Flex 的应用程序
步骤 1:下载 Bundle
打开命令行,进入项目目录并执行以下命令以下载此 Bundle 的最新稳定版本
$ composer require startplatz/wordpress-integration-bundle
步骤 2:启用 Bundle
然后,通过将其添加到项目 config/bundles.php
文件中注册的 Bundle 列表中来启用该 Bundle
// config/bundles.php return [ // ... Startplatz\Bundle\WordpressIntegrationBundle\StartplatzWordpressIntegrationBundle::class => ['all' => true], ];
手动步骤
在路由配置(例如 config/routing.yaml)中启用一个应由 WordPress 处理的路由
....
wordpress:
resource: "@StartplatzWordpressIntegrationBundle/Controller/PassthruController.php"
type: annotation
注意:您应该事先配置好所有其他路由。
对于 Apache 2 用户:将示例 .htaccess 文件放到您的文档根目录,并根据您的需求进行调整,例如错误处理等。注意:最好防止 WordPress 更改此文件!
对于其他 HTTP 服务器,您需要根据 Apache 规则设置规则
设置 WordPress
您的 WordPress 安装应位于 Symfony2 项目的 public
文件夹中
为了完成集成,您应该通过调用 Symfony 控制台命令来更新全局缓存
app/console startplatz:wordpress-integration:build-global-names-cache
配置
添加文件 config/startplatz_wordpress_integration.yaml
(如果尚未由 flex 完成)
startplatz_wordpress_integration:
wordpress_root_dir: '%kernel.project_dir%/../public'
- wordpress_root_dir:WordPress 安装的目录 - 必填,无默认值
实现
使用 WordPress 输出作为 Twig 模板
在控制器和 Twig 模板中使用
通过注解定义 WordPress URL 作为模板
<?php
namespace Acme\Bundle\WebsiteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Startplatz\Bundle\WordpressIntegrationBundle\Annotation\WordpressResponse;
class DefaultController extends Controller
{
/**
* @Route("/",name="show_index")
* @WordpressResponse("/url-from-wordpress/", createTwigTemplate=true)
* @Template
*/
public function indexAction()
{
return array();
}
}
您的 Twig 模板应如下所示
{% extends template_from_string(app.request.get('_wordpressTemplate')) %}
{% block something %}
Hello world!
{% endblock %}
注意:默认情况下未激活 twig 函数 template_from_string()
。有关此信息,请参阅http://twig.sensiolabs.org/doc/functions/template_from_string.html 文档。
在 WordPress 中定义块
在您的 WordPress 页面(或帖子)中,您可以定义尽可能多的块。WordPress 集成 Bundle 将将通配符如 %%SOMETHING%%
转换为块定义 {%block something %}{% endblock %}
有一些默认块在每次集成时都会集成
{% block additionalHead %}{% endblock %}
{% block additionalBody %}{% endblock %}
{% block robots %}index,follow{% endblock %}