Sep 232014
 

I found a small problem with the way I was generating the membership number, the solution I documented on this post works BUT if a member registers but then fails to activate their membership before another person registers they both have the same membership number and user name. WordPress, rightly, does not allow two members to have the same user name.

I therefore created a custom table in the WordPress database called membership. It has two fields ID and member, ID is set to 1 and the member field is set to one less than whatever you want the starting membership number to be. The code in functions.php therefore needs to be changed to read and write the membership number to the custom table. Rather than use a custom table I could have used the options table but I thought it better to have the custom table so that it is easier to edit with phpmyadmin if necessary. The code now looks like:

add_action('gform_pre_validation', 'pre_validation_handler');
function pre_validation_handler($form){
	if ( 2 == $form['id'] ) {
		global $wpdb;
        $myrows = $wpdb->get_row("SELECT * FROM membership WHERE ID = 1" );
        $member = $myrows->member;
        $member ++;
        $wpdb->update('membership',array('member' => $member),array('ID'=> 1));
        $_POST["input_5"] = $member;
		}
// have to return the form either way
	return $form;
}

This now works correctly but it does mean there may be gaps in membership numbers should members who register not activate their accounts.

 Posted by at 5:59 pm
Sep 192014
 

I have a requirement to build a WordPress sites for an organisation that wants to charge members to belong to the organisation. I looked at the membership plugins that are available currently and they are all geared to making members pay for access to content rather than for the simpler needs of a club or organisation that wants some of their website pages available to non-members and the remainder of the site only available to members. As a result of nothing being available off the shelf as a plugin I decided to build my own functionality to deliver what this organisation wants.

I have built this functionality using three plugins and a small amount of custom code in functions.php. The plugins that I used are Gravity Forms from Rocketgenius an addon for Gravity Forms User Registration and this is also available from Rocketgenius as part of their developer licencing and finally Extra User Details from Vadym Khukhrianskyi which is available in the WordPress repository. I used this plugin as it writes the additional user data to the WordPress usermeta table rather than a bespoke table as other plugins of this type do. This, in my opinion, makes for easier integration with WordPress and easier data retrieval.

The requirement I had was to create a membership number for each new user and to assign that as the WordPress username (which cannot be changed by the user through the WordPress interface). I achieved this by looking at the last user registration ID in the users table and adding 1001 to it as I needed to have 4 digit membership numbers. The username is a required field by WordPress for user registration and so needs to be populated onto the Gravity Form before it is submitted. It is therefore necessary to use the gform_ pre_validation hook in Gravity forms. The id for my form is 2 and so this is the code I added to my child themes functions.php

//Assign membership number to user
add_action('gform_pre_validation', 'pre_validation_handler');
function pre_validation_handler($form){
	if ( 2 == $form['id'] ) {
		global $wpdb;
		$result = $wpdb->get_results("SELECT ID FROM $wpdb->users ORDER BY ID DESC LIMIT 1");
		$mem_no = $result[0]->ID;
		$new_mem = 1001 + $mem_no;
		$_POST["input_5"] = $new_mem;
		}
// have to return the form either way
	return $form;
}

*********** I have now changed this code due to an unforeseen problem. Please read this post too. ***********

The hidden field on the form is field 5

This is my Gravity form

mem4
The additional fields that I required were the users postal address. I therefore created the additional fields in the Extra User Fields plugin as below:

mem2

I finally mapped those additional fields to the fields in the Gravity Form as below:mem1
You will see that I have also mapped the Biography field from the Gravity Form to the standard WordPress Biography field.

Finally below you will see how the new fields are displayed at the bottom of the User Profile page:

mem3
The final part of the equation is to sort out the PayPal payment for the users membership fee. I will be using another Gravity Forms addon PayPal Payments Pro to do this when the PayPal details are made available to me. If there is anything tricky with this functionality addition to my home grown membership functionality I will add it here.

 Posted by at 3:07 pm
Mar 252014
 
php

Hopefully when WordPress 3.9 is available next month with the new editor the stripping of formatting from uploaded content will be a thing of the past. Until then the way I have overcome the problem is with a small piece of additional code in functions.php.

//Retain formatting in posts
//move wpautop filter to AFTER shortcode is processed
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 99);
add_filter( 'the_content', 'shortcode_unautop',100 );

Hopefully this will help someone else

 Posted by at 11:11 am
Mar 102014
 
php

Rather than adding the bloat of one of the php plug-ins to your WordPress site you can just add a small piece of code to your functions.php.

 /** Execute PHP code in a standard text widget **/
 add_filter('widget_text','execute_php',100);
function execute_php($html){
     if(strpos($html,"<"."?php")!==false){
          ob_start();
          eval("?".">".$html);
          $html=ob_get_contents();
          ob_end_clean();
     }
     return $html;
}

You should really create a child theme and put this code in there rather than in your theme’s functions.php as if you upgrade your theme you will lose the custom code.

 Posted by at 2:43 pm