If you have a socialengine website and other website on wordpress just to sell your products or write blogs, you might think of integration of both websites. One may have google it and didn’t found a solution for websites integration.
“Socialengine-wordpress Single Sign On on allows you to integrate your socialengine and WordPress sites, so that you can get rid off managing two different sites for selling your products or managing blogs”
For the integration process, we have to develop two plugins, one for socialengine and other for wordpress. Let’s start from the wordpress plugin development.
WordPress SSO Development
For creating the plugin we are following the MVC structure, if you want to read about MVC, you can refer this link- MVC
Firstly we need a database table which will store the path of the socialengine site for the integration process and for this we have used “activation” as well as “uninstall” hook of wordpress.
register_activation_hook( __FILE__, array('Sso', 'seoauth_activation') );
When user will install the plugin then activation hook will run the “seoauth_activation” function and create a table to store the url of socialengine site.
register_uninstall_hook( __FILE__, array( 'Sso', 'seoauth_uninstall'));
Uninstall hook would be call when user uninstall the plugin and will call the “seoauth_uninstall”. This function will remove the database table which has been created by the activation hook.
Now we will need a admin section for the plugin which will handle the url enter and menus synchronization process.
Admin Panel Configurations
For admin section we will write the code in admin controller.
We can add the buttons, textboxes and labels by using
add_settings_field() hook.
Configure the menu from socialengine
For the menus synchronization process, on click on sync button created in admin page, we will call the ip_add_menu function by a ajax call.
jQuery("#sync-menu").click(function(e) { e.preventDefault(); jQuery.ajax({ url: ajaxurl, type: 'POST', data: { action: 'ip_add_menu' }, beforeSend: function() { jQuery("#sync-menu").hide(); jQuery("#success").html(' '); }, .......
ip_add_menu function will handle following processes –
1. It will hit the socialengine action by a curl request and get the menus of socialengine
if (isset ( $_COOKIE ['PHPSESSID'] )) { $phpSession = $_COOKIE ['PHPSESSID']; $url = $sso->seauth_BaseUrl () ."ipwpauth/index/menu"; $resp = $sso->curl ( $url, $phpSession, array("phpSession",$phpSession) ); }
2. After getting menu, it will check whether or not menu already exist. If menu does not exist, it will create a new menu with the name “se-menu”.
$menu_name = "Se-Menu"; $menu_exists = wp_get_nav_menu_object( $menu_name ); if( !$menu_exists) { $menu_id = wp_create_nav_menu($menu_name); ……
3. If menu already exist, it will add or delete the menu items accordingly,
elseif($menu_exists) { $menuitems_Exists = array(); $menu_id = $menu_exists->term_id; $menu_items = wp_get_nav_menu_items($menu_id); …….
wp_get_nav_menu_items(), will get the current menu existed in wordpress.
WordPress Authorization for Socialengine User
For Auto Authorization we will write the function “wp_ipauth_check” and call it in constructor of main controller class.
function socialengine_Sso_Main() { $this->register_plugin ('socialengine_Sso_Plugin', dirname (__FILE__)); $this->add_action('wp_head','wp_ipauth_check'); } …..
add_action will call the “wp_ipauth_check”.
“Wp_ipauth_check” calls the “ssoCheck()”, which has been written in model and handles the following three processes-
1. It will get the information of the user, if any user logged in in socialengine site by hitting the action of socialengine by curl request.
if (isset ( $_COOKIE ['PHPSESSID'] )) { $phpSession = $_COOKIE ['PHPSESSID']; $url = $this->seauth_BaseUrl () . "ipwpauth/session"; $resp = $this->curl ( $url, $phpSession, array("phpSession",$phpSession ) ); } ………
2. If user is log-in through socialengine then it checks in wordpress whether that user exists or not. If user exists then it will log-in in wordpress.
if ($resp) { $user = get_user_by ( 'email', $resp->email ); if ($user) { $user_id = $user->ID; wp_set_current_user ( $user_id, $user->user_login ); wp_set_auth_cookie ( $user_id ); ………
3. if user does not exist in wordpress, it will create a new user in wordpress and get that user log-in.
$user_id = wp_create_user ( $resp->email, $resp->password, $resp->email ); wp_set_current_user ( $user_id, $resp->username ); wp_set_auth_cookie ( $user_id ); ………
wp_create_user(), hook will create the new user in wordpress.
wp_set_current_user (), set the current logged in user to newly created user.
wp_set_auth_cookie(), set the cookie for newly created user.
Socialengine SSO Development
Now in socialengine, we will write a plugin to get the menus and user details when wordpress hit the action by curl.
Socialengine User Details:
For getting the user details we will write the Action “session” , which called the “getUserInfo” function.
$user = Engine_Api::_()->user()->getViewer(); if($user->getIdentity()>0) { $api = Engine_Api::_ ()->getApi ( 'auth', 'ipwpauth' ); $userData = $api->getUserInfo ( $user ); ………
“getUserInfo” function will provide the user details based on the session
$userValues = array(); $userValues["email"] = $user->email; $username = $user->username; if($username) { $userValues["username"] = $username; } else { $userValues["username"] = $user->email; } ………
Socialengine Menus
For getting the menus of socialengine we will write the “menu” action, which will call the “getMenu” function.
$api = Engine_Api::_ ()->getApi ( 'auth', 'ipwpauth' ); $menu = $api->getMenu(); $this->_helper->json ( $menu ); ……….
“getMenu” function will return the menus of the socialengine
$menuParams = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getMenuParams ( 'core_main' ); $host = (isset ( $_SERVER ['HTTP_HOST'] ) ? $_SERVER ['HTTP_HOST'] : ''); $proto = (isset ( $_SERVER ['HTTPS'] ) && $_SERVER ['HTTPS'] !== "off") ? 'https' : 'http'; $sSiteUrl = $proto . '://' . $host; ……
Hence by using this we can create a plugin which will integrate the socialengine and wordpress.Also it can sync the menus of both the platforms.
If you want to reduce your efforts, we have created a plugin for you. You can use that plugin for your integration process between socialengine and wordpress.