Yii 1.1: WordPress Role Based Authentication in Yii


This tutorial is for people that are integrating Yii into WordPress. Meaning that WordPress is the host platform, and that Yii will be handling AR, CRUD, etc.

Our User Class

This should be saved in an auto-loaded location, in our application it was application.components, which was included as part of our application.import[].

class wpUser extends CApplicationComponent implements IWebUser, IApplicationComponent {
        public function 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()));

Setting up the main.php configuration to use our new user class

// application components
            'class' => 'wpUser'
//Make sure you delete the allowAutoLogin option

Sample Code inside of our controllers

<?php public function accessRules()
    return array(
            //WordPress capability check. 
            //  See @link http://codex.wordpress.org/Roles_and_Capabilities

All done

Voila! You will now be able to specify WordPress capabilities to limit access to certain features, all inside of your main controller class!

Be the first person to leave a comment

Please to leave your comment.

Write new article