MichelReap
MichelReap

Reputation: 5770

Integrating yii into Wordpress

I've got a yii CRUD created using gii and I would like to integrate it into WP admin's section.

I've seen a few tutorials that revolve around integrating Wordpress into yii's controller/router but since my app is really small and my WP is already working I would like to do the opposite. So basically what I would like to do is integrate WP authentication into yii's module.

Are there any tutorials on how to do this? What would be the cleanest and simplest way?

Upvotes: 1

Views: 837

Answers (2)

Bob Gregor
Bob Gregor

Reputation: 1161

Here's a simple class that wraps WordPress's API into Yii's role based auth manager - inside your controllers you'll specify the "roles" (a.k.a. WordPress Capabilities) you want to check for.

<?php public function accessRules()
{
    return array(
        array('allow',
            'actions'=>array('index','view'),
            'roles'=>array('publish_posts') 
            //WordPress capability check. 
            //  See @link http://codex.wordpress.org/Roles_and_Capabilities
        ),
 }
 ?>

This is your new User class, which will need to be added inside of your Yii config file in the section components => user => class = 'wpUser'. This will replace Yii's default CWebUser (which is not specified in the config array - it is loaded by default). Also - you'll need to delete the "allowAutoLogin" => true from the array.

<?php
class wpUser extends CApplicationComponent implements IWebUser, IApplicationComponent {
        public function init ()
        {
            parent::init();
        }
        function checkAccess ($operation, $params = array()) {
            return current_user_can($operation);
        }
        function getId() {
            return get_current_user_id();
        }
        function getIsGuest () {
            $is_user_logged_in = is_user_logged_in();
            return ! $is_user_logged_in;
        }
        function getName () {
            $name = wp_get_current_user()->user_login;
            return $name;
        }
        public function loginRequired()
        {
            wp_login_form(array('redirect' => Yii::app()->getRequest()->getUrl()));
        }
    }
?>

Published on Yii's Wiki

Upvotes: 0

Alexander Kuzmin
Alexander Kuzmin

Reputation: 1120

You could extend CWebUser with something like WPUser, the only necessary function are getIsGuest and getName or something like that.

So, you could basically build all your authentication like you would with a normal CWebUser, but do a little puzzling with the wp_-functions to make everything work.

These functions basically; http://codex.wordpress.org/Function_Reference/wp_get_current_user http://codex.wordpress.org/Class_Reference/WP_User

With WP_User you can emulate a yii user and RBAC etc, look at IWebUser to learn what your user class needs to work: http://www.yiiframework.com/doc/api/1.1/IWebUser

To include yii in wordpress, the only thing you need to do is make a template and include /yii-app/index.php in the content, and everything will work gracefully.

This is a bit short since I'm in a hurry. If you need more help i can return tomorrow or so with code i wrote for a project exactly like this.

Upvotes: 1

Related Questions