How to set a ShortCode attribute dynamically, using Hooks
Posted: 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:
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.
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.