PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

How to set a ShortCode attribute dynamically, using Hooks

s2Member Plugin. A Membership plugin for WordPress®.

How to set a ShortCode attribute dynamically, using Hooks

Postby toddz88 » April 22nd, 2011, 7:09 pm

I just spent most of the last two days on this, and I think finally got it working for my needs, so wanted to share. I'm sure it can be improved, so go for it.

GOAL
To set ShortCode attributes dynamically, specifically the PRICE ("ra" or Regular Amount) and DESCRIPTION, using HOOKS, instead of a php-exec plugin.

BACKGROUND
I read the Forum post from Jason "TIP: Using variables in a Shortcode" (viewtopic.php?f=36&t=1604), but I wanted to use HOOKS (Filters or Actions), instead of a PHP-exec plugin. I also watched the new Video series, related to the post "Custom Registration Fields ( a developer how-to )" (viewtopic.php?f=36&t=2834).

I found the ShortCode prep code in "s2member-pro/includes/classes/gateways/paypal/paypal-form-in.inc.php", and I found this hook:

ws_plugin__s2member_pro_before_sc_paypal_form_after_shortcode_atts

I created my /wp-content/mu-plugins/s2-hacks.php file, and wrote this:

Code: Select all
/*
DYNAMIC SHORTCODES USING HOOKS IN S2MEMBER PRO.

Get the Quantity var from the url, multiply it by the Price (per user), to derive a Total, and update the RA (Regular Amount) attribute of the ShortCode. Also update the Description accordinly, using our own custom Replacement Codes! Using Hooks instead of php-exec plugins.
*/

add_action('ws_plugin__s2member_pro_before_sc_paypal_form_after_shortcode_atts','my_dynamic_price',10);

function 
my_dynamic_price $vars = array() ) {
    
    
# PREP
    
$attr = &$vars["__refs"]["attr"]; // get the vars as REFERENCES so we actually modify the originals, not just copy them.

    # QUANTITY
    
$quantity htmlentities($_GET['qty']); // from link on Membership Options page.
    
if(!is_int($quantity)) { // verify it's an integer, otherwise default to 1.
        
$quantity 1;
    }
    
    
# PRICE
    
$price_each $attr["ra"]; // price for Individual; from orig RA (price) in ShortCode.     
    
$total $quantity $price_each// math.
        //$total = 9876; //TEST with hardcode value.
    
    # DESCRIPTION
    
$orig_desc $attr["desc"]; // orig DESCRIPTION expects two Custom Replacement Codes: %%My-Qty%% and %%My-Price%%.
    
$new_desc str_ireplace('%%My-Qty%%'$quantity$orig_desc); // replace
    
$new_desc str_ireplace('%%My-Price%%'$total$new_desc); // replace

    # OUTPUT
    
$attr["ra"] = "$total"// update RA to new value. $total must be in quotes, to make $attr["ra"] be a string, which I guess it needs to be.
    
$attr["desc"] = $new_desc// update DESCRIPTION so user sees new values.



So if my form url is "domain.com/signup?qty=20" and if my shortcode has these attributes: ra="99" and desc="Group account for %%My-Qty%% users: $%%My-Price%% USD / Monthly ( recurring charge, for ongoing access )"

then my form shows "Group account for 20 users: $1980 USD / Monthly ( recurring charge, for ongoing access )"

And after payment, my PayPal dev sandbox > Test Emails shows:
For:Group account for 20 users: $1980 USD / Monthly ( recurring charge, for ongoing access )
Amount paid each time:$1,980.00 USD

Which looks good, I think.
User avatar
toddz88
Experienced User
Experienced User
 
Posts: 61
Joined: November 19, 2010

Re: How to set a ShortCode attribute dynamically, using Hook

Postby Cristián Lávaque » April 22nd, 2011, 10:36 pm

That is very nice, Todd. I like your approach to this. Thanks for sharing it! :)

I personally enjoy discussing code and how it can be written in other ways, so here's my version of it in case you're interested and find it helpful.

Code: Select all
add_action('ws_plugin__s2member_pro_before_sc_paypal_form_after_shortcode_atts', 'my_dynamic_price', 10);

function my_dynamic_price($vars)
{
    $_GET['qty'] = isset($_GET['qty']) && is_numeric($_GET['qty']) && $_GET['qty'] > 1 ? (int) $_GET['qty'] : 1;
    $vars['__refs']['attr']['ra'] *= $_GET['qty'];
    $vars['__refs']['attr']['desc'] = strtr($vars['__refs']['attr']['desc'], array('%%My-Qty%%' => $_GET['qty'], '%%My-Price%%' => $vars['__refs']['attr']['ra']));
}
 


You will notice that I changed the is_int for is_numeric, because it is recommended in the documentation.

is_int wrote:To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().


I also make sure that there is a quantity passed in the URL and that it's at least 1, e.g. not 0.

I changed the str_ireplace to strtr because it's faster and am assuming that the replacement code can be case sensitive (faster than insensitive).

Let me know what you think.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: How to set a ShortCode attribute dynamically, using Hook

Postby toddz88 » April 22nd, 2011, 11:50 pm

Wow! 3 lines! That's pretty amazing. Very interesting to see some of your techniques and shortcuts too. Personally, I need to spread things out over more lines, so i can more easily see (and remember) what's going on, and have space for documentation comments. But your efficiency is inspiring.

And you're right.. after I posted, I found the need for is_numeric, and also added bit of error checking and user feedback.
User avatar
toddz88
Experienced User
Experienced User
 
Posts: 61
Joined: November 19, 2010

Re: How to set a ShortCode attribute dynamically, using Hook

Postby Cristián Lávaque » April 22nd, 2011, 11:55 pm

I'm glad it helped!

Yes, I also add some comments to my code when I need to know what's going on. Most of the time, though, the function and vars names almost say all I need, and being in 3 lines I can see what's going on all right there.

I'm curious, what error checking and user feedback did you add?
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010


Return to s2Member Plugin

Who is online

Users browsing this forum: No registered users and 3 guests

cron