Using Yii2 extension in WordPress

WordPress is the most popular CMS in the world. Yii2, probably, is the best PHP-framework. Why don’t bind them together?
They both baked from the same dough – PHP. Both were made thoroughly enough.
So, let’s begin

Prerequisites:
1. WordPress 4.7.1.
2. WordPress Theme -Twenty Seventeen
3. Yii2 extension – lslsoft/poll

1. Instal WordPress locally.
We will use “wordpress_widget” as our database name.

2. Choose Twenty Seventeen as out Theme.

3. Install wordpress-bootstrap-css plugin.
4. Install Yii2.
Change folder for wordpress local folder
Follow the steps of standard installation of the Yii2 framework. Basic template.

php composer.phar create-project yiisoft/yii2-app-basic basic 2.0.10

In WordPress folder Yii2 folder “Basic” will be created.

5. Install lslsoft/poll
Yii2-poll can be installed using composer. Run following command to download and install yii2-poll (from “basic” folder:

composer require lslsoft/yii2-poll

or add this in

require

section of composer.json of your Yii2 project

"lslsoft/yii2-poll" : "dev-master"

6. Config poll widget

Add wordpress db parameters in db.php

Add i18n parameters in components section of web.php

'i18n' => [
            'translations' => [
                'sourceLanguage' => 'en-En',
                 
                'polls*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@yii/vendor/lslsoft/poll/messages',
                ],
                 
            ],

Add migration parameters in components section of console.php

'controllerMap' => [
        'migrate-lslsoft-create' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => 'vendor/lslsoft/yii2-poll/migrations/create',
            'migrationTable' => 'migration_lslsoft_create',
        ],
        'migrate-lslsoft-insert' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => 'vendor/lslsoft/yii2-poll/migrations/insert',
            'migrationTable' => 'migration_lslsoft_insert',
        ],

7. Run migration

php yii migrate-lslsoft-create

for creating tables and relations

php yii migrate-lslsoft-insert

for some sample data

The tables a in the base (without prefix, but it’s not fatal)

Yii2 and WordPress can exist together!

8.Create WordPress widget
In the folder wp_includes\widgets create file
class-wp-widget-lslsoft.php

Head of the file

class WP_Widget_Lslsoft extends WP_Widget {

function __construct() {
		parent::__construct(
			'widget_lslsoft', // Base ID
			esc_html__( 'LSL soft widget', 'text_domain' ), // Name
			array( 'description' => esc_html__( 'A LSLsoft Widget', 'text_domain' ), ) // Args
		);
	}

for using Poll widget just add

echo Poll::widget();

8. Add information about our widget in wordpress

widgets.php

'wp_widget_lslsoft' 

in

$GLOBALS['_wp_deprecated_widgets_callbacks'] = array(

And

register_widget('WP_Widget_Lslsoft');

in

function wp_widgets_init() {

Add

require_once( ABSPATH . WPINC . '/widgets/class-wp-widget-lslsoft.php' );

in file default-widgets.php

9. Copy folder basic/assets in root folder of wordpress

Don’t ask…

10.Modify wp-config.php

require_once (__DIR__ . '/basic/vendor/yiisoft/yii2/Yii.php');
$yiiConfig = require(__DIR__ . '/basic/config/web.php');
new yii\web\Application($yiiConfig); 

11. Run wordpress and go to dashboard

Look at appearance\widget list

Add widget in Footer 1

12. Run WordPress

In the page footer will be widget

After sending vote

11 Replies to “Using Yii2 extension in WordPress”

    1. Thank you for your response! You are quite right – this way for registering widget is far from optimal.
      The mail goal was to make sure that we really can use yii2 and wordpress together. And this problem was solved.
      And if will be the subject of the next post how do it properly.

  1. I blog often and I seriously appreciate your information. The article has really peaked my interest. I am going to take a note of your site and keep checking for new information about once per week. I subscribed to your Feed too.

  2. Hello everybody.
    I read this article with fascination. There aren’t much information about coupling yii and wordpress together. Therefore I’m very interested in further informations.
    When you will release the next article in this serie?

    Best regards
    Stephan

Leave a Reply

Your email address will not be published. Required fields are marked *