Hey guys,
I wanted to use Wordpress Login and user management for the Codeigniter.
So this is what I have made not only to use log in and user management but we can user other wordpress functions also by simply adding to library and calling it through object.
I have tried to make Codeigniter as a part of Wordpress. Of course this is a very basic version. I have created function according to my requirement. Feel Free to modify functions or add new according to your requirement..
Following are the steps to user it. Only 4 steps and you are through…
1. Install Codeigniter inside your wordpress root directory
2. Copy this file to your library folder
Create file named Wpbridge.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author Dirgh Buch
* @copyright Dirgh Buch.
* @link <a href="http://dirghbuch.com">http://dirghbuch.com</a>
* @since Version 1.0
*/
// ------------------------------------------------------------------------
/**
* Wordpress Bridge Class
*
* @package My Project
* @subpackage Libraries
* @category Wordpress Bridge
* @author Dirgh Buch
* @link <a href="http://dirghbuch.com">http://dirghbuch.com</a>
*/
include('/path_to_public_html/public_html/wp-blog-header.php');
class CI_Wpbridge{
// Private variables. Do not change!
var $CI;
/**
* WPLogin Class Constructor
*
* The constructor loads the Login
*/
public function __construct()
{
// Set the super object to a local variable for use later
$this->CI =& get_instance();
if(!is_user_logged_in())
{
//echo "http://{$_SERVER['SERVER_NAME']}/wp-login.php?redirect_to=".base_url()."index.php/{$this->CI->uri->uri_string()}";die;
redirect("http://{$_SERVER['SERVER_NAME']}/wp-login.php?redirect_to=".base_url()."index.php/{$this->CI->uri->uri_string()}");
}
else
{
global $current_user;
get_currentuserinfo();
$session_user_data['user_name']=$current_user->user_login;
$session_user_data['user_email']= $current_user->user_email;
$session_user_data['first_name']=$current_user->user_firstname;
$session_user_data['last_name']=$current_user->user_lastname;
$session_user_data['display_name']=$current_user->display_name ;
$session_user_data['user_id']=$current_user->ID;
$this->CI->session->set_userdata($session_user_data);
}
}
/**
* Fetching All blogs for Logged In User.
*
* @param
* @return list of blogs
*/
function get_user_blogs()
{
global $current_user;
$blogs = get_blogs_of_user( $current_user->id );
return $blogs;
}
/**
* Fetching All Posts of users.
*
* @param
* @return list of posts
*/
function get_all_post($number_of_post=50)
{
return get_posts(array('numberposts' => $number_of_post));
}
/**
* For Multisite Fetching Content of post blog.
*
* @param Integer $blog_id ID of the blog whose post are required
* @param Integer $post_id ID of the post whose content is required
* @return Post details
*/
function getPostsByBlog($blog_id,$post_id)
{
return get_blog_post($blog_id,$post_id);
}
/**
* For Multisite Copying same post to current blog
*
* @param Integer $blog_id ID of the blog whose post is to be copied
* @param Integer $post_id ID of the post which is to be copied
* @return Post details
*/
function duplicatePostToCurrentBlog($blog_id,$post_id)
{
$post=$this->getPostsByBlog($blog_id, $post_id);
unset ($post->ID);
return $new_post_id=wp_insert_post( $post, $wp_error );
}
function deleteSharedPost($post_id)
{
wp_delete_post($post_id, true);
}
/**
* For Multisite Current Site name
*
* @return Current Site name
*/
function getCurrentSite()
{
return get_current_site();
}
/**
* Tags of all posts
*
* @return array of tags
*/
function getAllTags()
{
return strip_tags(str_replace("</a>", ",", wp_tag_cloud( array( 'taxonomy' => array('post_tag','category'),'echo'=>false ))));
}
/**
* Get Logout URL
*
* @param String $redirect_url URL of the site where we want to redirect after logout
* @return URL Url to be passed to href of link
*/
function getWpLogoutURL($redirect_url='')
{
return wp_logout_url($redirect_url);
}
}3. In autoload file add wpbridge to
$autoload['libraries']=array("wpbridge");4. Open library file and modify path
include('/path_to_public_html/public_html/wp-blog-header.php');(Absolute Path to your wp-blog-header.php file
you can use all the function like
$this->wpbridge->functionName();Hope this library helps…
Happy Coding !!!