mukadi / wordpress-bundle
Integrate wordpress and symfony in the same application
Installs: 2 842
Dependents: 1
Suggesters: 0
Security: 0
Stars: 21
Watchers: 4
Forks: 11
Open Issues: 4
Type:symfony-bundle
Requires
- php: ^7.1.3
- composer/installers: ^1.5
- doctrine/doctrine-bundle: ^1.0||^2.0
- doctrine/orm: ^2.2
- hautelook/phpass: 0.3
- illuminate/support: 5.7.*||6.7.*
- johnpbloch/wordpress: 5.1
- mukadi/doctrine-crud: ^1.0
- phpoption/phpoption: ^1.7
- symfony/dotenv: >=3.4
- symfony/framework-bundle: ^3.4||^4.0||^5.0
- symfony/security-core: >=3.4
- vlucas/phpdotenv: ^2.4||3.6.*
Suggests
- doctrine/doctrine-migrations-bundle: For a best management of your database schema
README
This is a fork of EkinoWordpressbundle, this bundle adapt EkinoWordpressBundle to symfony >= 4 new architecture and features. Some features has been removed (such as automatic symfony authentication when authenticated in Wordpress...) and will be reintegrated as separated bundle to install if needed.
Here are some retained features:
- Use custom Symfony services into Wordpress (note: only public services),
- Use Symfony to manipulate Wordpress database,
- Create custom Symfony routes out of Wordpress,
- Dispatch Event from Wordpress into Symfony *(require mukadi-symfony-bridge Wordpress plugin)
Installation
Before install the bundle, edit your composer.json file and specify the following options:
"extra": { ... "symfony": { ... "allow-contrib": "true" # allow symfony flex to install recipe (if your are using symfony flex) } ... # set installation path for wordpress themes and plugins "installer-paths": { "public/mu-plugins/{$name}": ["type:wordpress-muplugin"], "public/plugins/{$name}": ["type:wordpress-plugin"], "public/themes/{$name}": ["type:wordpress-theme"] }, # install wordpress in a public sub-directory "wordpress-install-dir": "public/wp" },
Run php composer.phar require mukadi/wordpress-bundle
and let Symfony Flex configure the bundle.
Bundle configuration
If your are not using symfony flex, you have to configure manually your bundle, here is the minimal bundle configuration:
mukadi_wordpress: table_prefix: "%env(WP_PREFIX)%" wordpress_directory: '%kernel.project_dir%/public/%env(WP_DIR)%'
Add a public/wp-config.php file
Put the following content in your public/wp-config.php file :
declare(strict_types=1); require_once __DIR__.'/../vendor/autoload.php'; $config = new \Mukadi\WordpressBundle\Config( realpath(__DIR__) ); // define('WP_ALLOW_MULTISITE', env('WP_ALLOW_MULTISITE', true)); $table_prefix = env('WP_PREFIX', 'wp_'); /* That's all, stop editing! Happy blogging. */ $config->apply(); /** Sets up WordPress vars and included files. */ require_once(ABSPATH . 'wp-settings.php');
Update the public/index.php file
If you don't make modifications in your public/index.php file you can just copy the content of the generated 'sf-wp-bootstrap.php' (see the code below) into your index.php file, otherwise update your index.php accordingly to that file.
Here's what your index.php file should look like:
use App\Kernel; use Symfony\Component\ErrorHandler\Debug; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Dotenv\Dotenv; require dirname(__DIR__).'/config/bootstrap.php'; function run(){ if ($_SERVER['APP_DEBUG']) { umask(0000); Debug::enable(); } # setup WP_DEBUG $env = $_SERVER['APP_ENV'] ?? 'dev'; $debug = (bool) ($_SERVER['APP_DEBUG'] ?? ('prod' !== $env)); define('WP_DEBUG', $debug); if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) { Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST); } if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) { Request::setTrustedHosts([$trustedHosts]); } $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); # inject SF container in WP $GLOBALS['sf'] = function ($id) use (&$kernel) { return $kernel->getContainer()->get($id); }; $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send(); $kernel->terminate($request, $response); } run();
Add Wordpress routing into symfony
Add the WordpressBundle routing file in your config/routes.yaml
, after your custom routes to catch all Wordpress routes:
... mukadi_wordpress: resource: "@MukadiWordpressBundle/Resources/config/routing.xml"
Install Wordpress Plugins via Composer
Edit your composer.json file to add a custom repository:
... "repositories": [ { "type": "composer", "url": "https://wpackagist.org" } ]
Now you can install wordpress plugins, just run php composer.phar require wpackagist-plugin/<the-plugin-name>
.
Avoid Doctrine remove custom Wordpress tables
When you install plugins in Wordpress, plugin can create custom tables to store specific data. By default such tables will be removed by the doctrine:migrations:diff
command. You must configure doctrine/dbal to ignore those tables, just have to add the following configuration option to your doctrine configuration:
... doctrine: dbal: schema_filter: '~^(?!%env(WP_PREFIX)%)~'
Manipulate Wordpress database in Symfony
You can call Wordpress table managers in Symfony by calling the following services:
All of this services extends the Mukadi\Doctrine\CRUD\CRUD
class, so see the documentation to know how to deal with it.