How to Include a Shortcode / Plugin Fetaure on the Free Registration PayPal Pro Form

Hi, I’m trying to include the functionality of two WP plugins on my Registration page which uses the PaPal Pro form for Free Registration. Can anyone show me the correct hook to use so I can add the functions in the same way as using…

add_action( ‘register_form’, ‘myplugin_add_registration_fields’ )
and the errors filter
add_filter( ‘registration_errors’, ‘myplugin_check_fields’, 10, 3 )

I’ve looked through the knowledgebase etc and perhaps I’m just missing it.

(WP user Avatars is probably the only Plugin anyone has heard of. The other adds a registration code that must be correct)

Thanks!

The way to do what you want isn’t to use hooks. It’s to create your own custom form that includes elements from other plugins. See https://s2member.com/kb-article/can-i-customize-pro-forms/

Thank you, Tim @KTS915

I did look at doing this but surely it only allows me to add shortcodes which is not the same as adding an action to the form?

One of your functions involves creating fields. So you could just add those fields directly to your form – no need for a hook.

I think you also overlook the possibilities available though shortcodes. Those shortcodes can be used to run code. So instead of needing, say, an add_action hook, you’d do add_shortcode and then paste in the shortcode for your function. See https://codex.wordpress.org/Shortcode_API

Thank you again for your thoughts @KTS915, alas, there are no shortcodes in either plugin for this, and it’s obvious why not when you think about it. These are for registration processes only, and the plugins already add themselves to the WP Registration form so do not include a shortcode for it.

One adds a registration Code that must be validated against the plugin’s settings BEFORE registration goes ahead to prevent unwanted sign-ups. (This is not all it does so it’s not a case of replacing it and using a Captcha which would not offer the same kind of validation anyway). I also can not add a custom field within S2Member for level 0 as there would be no validation.

The second (WP Avatar Users) has a shortcode for adding to a page for Uploading an avatar on the frontend for an EXISTING User although it does already successfully hook into the usual WP User Profile and therefore also appears on a page that uses [s2Member-Profile /] without any modification. However, during registration, it uses a different process to create an Avatar as the user DOES NOT exist, so the shortcode [avatar_upload] will not work on a registration form.

You may be wondering why I just do not stick with the default “/wp-login.php?action=register”

  1. It’s ugly and cannot add the extra information I require on the registration page.
  2. I’ve chosen to hide Wordpress from my users.

Workaround: For the Avatars, I can use a One-Time-Offer that redirects to a customised frontend Profile page upon the first login. This page would instruct Users to upload an Avatar. I have already done this as the difficulty I was having finding the hook outweighed the amount of time it would take to add this workaround.

WP-Leads-Press (Registration Code) - There is None.

I love S2Member as there are so many code examples and Hooks that IMHO it is far superior to any other Membership plugin out there (even after many years of use).

I am certain that somewhere there surely must be a way of hooking into the PayPal Pro Registration form to add these extra requirements just below the Custom Profile field section.

But those plugins don’t need to have their own shortcodes. That’s my point. You can use their code to create your own shortcode, which you can then add to the form.

1 Like

Aha! @KTS915 The penny drops ;-o
O.k, I’ll look into this. Thanks again for all your advice!

I’m having difficulty with the validation part.

I feel that it lies somewhere within adding a filter to ws_plugin__s2member_custom_registration_field_errors but as yet I’ve not conquered it. :unamused:

add_filter(‘ws_plugin__s2member_custom_registration_field_errors’, ‘wplp_s2_registration_errors’, 10, 3);
function wplp_s2_registration_errors ($errors, $sanitized_user_login, $user_email) {
global $wp, $_POST;

// Get all wplp options
//$options = get_option( ‘wp_leads_press_options’ );
$options = wp_load_alloptions();

$postRegCode = $_POST[‘wplp_registration_code’];
$regCode = $options[‘wp_leads_press_registration_code’];

if ( $postRegCode != $regCode && $options[‘wp_leads_press_username_regcode’] != ‘on’ ) {

  $errors->add( 'wplp_registration_code', __('<strong>ERROR</strong>: Incorrect Registration Code.', 'wp-leads-press' ) );				

  return $errors;

}

return $errors;

}

Are you saying that you have it all working apart from this? If not, I wouldn’t touch validation until you do. Otherwise you’ll make it impossible to debug.

Yes, adding the field into a shortcode and then the shortcode onto the Pro Form Template was simple enough. Using the correct S2Member Class auto-checks the required property. Now I just need that the “errors” pop-up to also appear when the field is not just empty, but the contents is not valid. Here in lies the whole issue really!

TBH adding a custom field would have been the answer and the most natural thing to do, but the validation of the contents was not possible. If only under the options for content validation was a “Must =” LOL which would only have needed a REGEX facility in the drop-down list of options.

I see. Have you tried now adding the field(s) using s2Member’s Custom Registration/Profile Fields facility?

I am not suggesting that you then use the form that s2Member generates, but I have found that this seems to be a good way of telling s2Member what to expect, so that it then validates my custom fields in my custom form. This way, if it works, will mean that you won’t have to try to bother with that filter.

After much thinking, I’ve decided that having a REGEX option in the Custom Registration Fields - Expected Format (Specific Input Types) would allow anyone to add a format that is not already Pre-defined. So, I have messaged @jaswrks to ask him to consider this addition.

@web-builder Thank you. Will certainly consider that and see what we can do. In the meantime, there’s a filter exposed by s2Member that you may find helpful. Take a look here: https://github.com/websharks/s2member/blob/fd79ec228c33906fcb7c635ee029034492793c7f/src/includes/classes/custom-reg-fields.inc.php#L792

1 Like

:kissing_heart: @jaswrks Thanks, Jason!

I was so close in my pasted code above just the wrong filter (just a “c_” out LOL). I had even later copied the correct one to investigate. - Off to try it now :grinning:

@jaswrks I was trying this yesterday to physically recode s2member/src/includes/classes/custom-reg-fields.php (temporarily of course) and I got no changes in output.

I’ve just created a Custom Field expecting a uczip and recoded that section to show a different message. However, when I test it on a PayPal-Pro-form I still see the original message.

Is this because of some kind of caching issue?

@web-builder Can you post a snippet of code that might shed light. Happy to review.

I changed line 725 to be:

  							case 'uczip':
  								if(!preg_match('/^[0-9]{5}(?:\-[0-9]{4})?$/', $input[$_field_var]) && !preg_match('/^[0-9A-Z]{3} ?[0-9A-Z]{3}$/i', $input[$_field_var]))
  									// $errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>'._x('Must be a zipcode (either a US or Canadian zipcode).', 's2member-front', 's2member').'</em>';
  									$errors[$_field_var] = '<strong>'.$_field_label.'</strong><br />&nbsp;&nbsp;<em>A Reg-Code !!!</em>';
  								break;

The code from the plugin that adds it to Wordpress errors is:

//2. Add validation. In this case, we make sure registration code is required.
add_filter(‘registration_errors’, ‘wplp_registration_errors’, 10, 3);
function wplp_registration_errors ($errors, $sanitized_user_login, $user_email) {
global $wp, $_POST;

  // Get all wplp options
  //$options = get_option( 'wp_leads_press_options' );
  $options = wp_load_alloptions();
  
  $postRegCode = $_POST['wplp_registration_code'];
  $regCode = $options['wp_leads_press_registration_code'];
  
  if ( empty( $_POST['wplp_registration_code'] ) ) {
  
  	$errors->add( 'wplp_registration_code', __('<strong>ERROR</strong>: You must include the registration code.', 'wp-leads-press' ) );
  	
  return $errors;				
  	
  }
  
  if ( $postRegCode != $regCode && $options['wp_leads_press_username_regcode'] != 'on' ) {
  
  	$errors->add( 'wplp_registration_code', __('<strong>ERROR</strong>: Incorrect Registration Code.', 'wp-leads-press' ) );				
  
  	return $errors;
  
  }
  
  return $errors;

I see. Thank you.

No, I don’t think. It could be because s2Member has two validation layers. One in JavaScript and another in server-side PHP code. Is the error message you’re seeing coming from a JavaScript alert? If so, please see: Quick Translation—Changing Words/Phrases

Even better, my suggestion is that you configure your custom field with s2Member and allow any value; i.e., don’t choose a specific validation rule. Instead, use a filter like this one to run your own validation server-side and produce your own error message.

<?php // Requires PHP 5.4+.
add_filter( 'c_ws_plugin__s2member_custom_reg_field_validation_errors', function( $errors, $vars ) {
    $my_field_id = 'my_field'; // As you configured for your custom field in s2Member.

    extract( $vars );

    foreach ( $fields_to_validate as $_field ) {
        if ( $_field['id'] !== $my_field_id ) {
            continue; // Not relevant here.
        }
        $_field_var   = preg_replace( '/[^a-z0-9]/i', '_', strtolower( $_field['id'] ) );
        $_field_label = ! empty( $_field['label'] ) ? $_field['label'] : ucwords( str_replace( '_', ' ', $_field_var ) );
        $_input_value = isset( $input[ $_field_var ] ) ? (string) $input[ $_field_var ] : null;
        
        if( ! isset( $_input_value ) || ( // From `uczip` adapt as necessary.
              ! preg_match( '/^[0-9]{5}(?:\-[0-9]{4})?$/', $_input_value )
              && ! preg_match( '/^[0-9A-Z]{3} ?[0-9A-Z]{3}$/i', $_input_value )
            )
        ) { // Set your custom error message when validation fails.
            $errors[$_field_var] = '<strong>'. $_field_label .'</strong><br />&nbsp;&nbsp;<em>A Reg-Code !!!</em>';
        }
    }
    return $errors; // Return filtered errors.
}, 10, 2 );
1 Like

@jaswrks Wonderful Jason! Many Thanks!!!

Yes, I was concentrating on the output of the Javascript Pop-up in my testing and it also didn’t occur to me to use an “Anything Goes” validation even if I had of been able to write the filter code correctly. I was close to it in my research but just looked straight past it due to my lack of knowledge.

This is EXACTLY why I love s2Member. There is virtually nothing that can’t be done. It has been expertly coded to allow so many bespoke additions and the support for it is divine.

Q1) Out of curiosity, can you add filters to the javascript or is it something that has to be hard-coded?

Secondly, I have bypassed the WP Leads Press plugin to prevent it adding its field on the wp-login.php?action=register Form and therefore creating a duplicate Reg-Code field. Naturally, on the s2-Pro-Forms you only see the Custom Registration fields (which is why I needed this filter in the first place.

I see that within the plugin it has code for another membership plugin to add the Reg-Code field to its form.

if( is_plugin_active( ‘paid-memberships-pro/paid-memberships-pro.php’ ) ) {

  add_action('pmpro_checkout_before_submit_button', 'wplp_register_form');
  add_filter('pmpro_registration_checks', 'wplp_pmpro_registration_checks');

}

Q2) Is it possible to do similar with s2Member?
If so I’ll pass the code to the developer so they can make it a permanent addition. (Not that it is actively developed nor very common).