Manage sessions in wordpress plugin

Posted by shadow_of__soul | Posted in Uncategorized | Posted on 01-02-2009

0

Hi,

i’m doing a lot of things in wordpress the last time :P

my last project was the integration of a script what i made before in a wordpress blog. the script are a custom shopping cart, and it use $_SESSION vars to manage products, and a unique ID for the cart.

so i thinked it going to be easy, a simple wrap plugin what call’s to the respective php. my surprise has been when i tested it and all the sessions are empty WTF?

so searching over the net for a solution, i get what it’s the problem. the thing it’s wordpress erase all the $_SESSION vars calling to a function named wp_unregister_GLOBALS and it’s defined in wp-settings.php

if you have the register_globals off, you dont need to worry, because then the function dont erase the vars but if not, you need to hack the function.

so, change this:

$noUnset = array(‘GLOBALS’, ‘_GET’, ‘_POST’, ‘_COOKIE’, ‘_REQUEST’, ‘_SERVER’, ‘_ENV’, ‘_FILES’, ‘table_prefix’);

into this:

$noUnset = array(‘_SESSION’,'GLOBALS’, ‘_GET’, ‘_POST’, ‘_COOKIE’, ‘_REQUEST’, ‘_SERVER’, ‘_ENV’, ‘_FILES’, ‘table_prefix’);

after this, in your plugin, add a hook to fire the session_start() function:

add_action(‘init’,’sessionset’);

function sessionset(){
if(!session_id()){
session_start();
}
}

with that you have the all your $_SESSION vars available in your plugin :D

i hope you find this useful

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

5 tip to make a wordpress plugin

Posted by shadow_of__soul | Posted in Uncategorized | Posted on 28-01-2009

0

Hi,

this days i’m making a wordpress plugin for a client and i have to search some things in google.so i want to make this tips for you to make the things more easy.

1)how to make a widget for your plugin

this it’s something hard to find the right solution, i implemented this code in my plugin:

function widget_init() {
// Check for required functions
if (function_exists(‘register_sidebar_widget’)){
register_sidebar_widget(‘Simple Widget’,’simple_widget’);
}
}

function simple_widget($args) {

extract($args);

echo $before_widget;
echo $before_title
. ‘Simple Widget’
. $after_title.$after_widget;

}

add_action(“plugins_loaded”, “widget_init”);
add_action(‘widgets_init’, ‘widget_init’);

2)how to create your own plugin pages in the frontend

if you need to show the content of your plugin in a separated page, the best solution its to create a page for it.

$sql =”INSERT INTO “.$wpdb->posts.”
(post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt,  post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_type)
VALUES
(‘1′, ‘$post_date’, ‘$post_date_gmt’, ‘[PLUGIN PAGE DONT DELETE THIS]‘, ”, ‘–plugin page’, ”, ‘publish’, ‘closed’, ‘closed’, ”, ‘[PLUGIN PAGE DONT DELETE THIS]‘, ”, ”, ‘$post_date’, ‘$post_date_gmt’, ‘$post_parent’, ‘0′, ‘page’)”;
$wpdb->query($sql);
add_option(‘plugin_page_id’,$wpdb->insert_id);

with this you are creating a page in the DB, then you add the hook and function to filter the content:

add_filter(‘the_posts’,'cmfrontend’);
function cmfrontend($posts){

$id=get_option(‘plugin_page_id’);

if($id==$_GET['page_id']){

$newpost[0]=$posts[0];
$newpost[0]->post_content=”your plugin page content”;
$newpost[0]->post_title=”Your plugin page title”;

}

}

and it’s done :D

3)remember to add the uninstall hook

to avoid future problems, in WP versions and DB updates, always add the deactivation hook, to erase (or clean) the DB and options. if the data it’s important, you can add a message warning this :D .

register_deactivation_hook(__FILE__,’uninstallfunction’);

4)use a external css for the styles

when you make the style for the frontend of the plugin, do it assigning classes or id and save it using an external css file. this give you the feature for the future users/designer/programer change the style, colors and size whiteout touching your plugin code and avoiding he break something :D

you can do it setting a wp_head hook and in the function printing the import of the css file:

add_action(‘wp_head’,'echohead’);

function echohead(){
echo ‘<link rel=”stylesheet” href=”wp-content/plugins/yourpluginname/style.css” type=”text/css” />’;

}

5)keep a clean code

dont harcode the plugin, if you need to do something, search on the web, you going to find some hook or filter to make what you need. if everthing fails, then, make what you need and try to keep the compatiblity.

good places where find wodpress hooks and filters:

http://wphooks.flatearth.org/

i hope you find this useful :D

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]