Page 1 of 1

TIP: Using variables in a Shortcode

PostPosted: January 13th, 2011, 4:17 am
by Jason Caldwell
It is possible to configure Shortcode Attributes dynamically.

But first, you will need to install a PHP Execution plugin.
http://wordpress.org/extend/plugins/php ... on-plugin/

Now, let's assume you have a URL like this on your site:
Code: Select all
http://www.YOURSITE.com/membership-options-page/?amount=129.00

Inside your Membership Options Page, or whatever Page contains your Shortcode, you can do this:
Code: Select all
[s2Member-PayPal-Button ... ra="<?php echo esc_attr($_REQUEST["amount"]); ?>" ... /]
( this Button Code has been abbreviated for clarity; showing only the "ra" attribute )

So in this example, the "ra" attribute ( i.e. the Regular Amount ), is configured dynamically based on the value of ?amount=129.00 in the URL. This particular example may or may not be useful to you, but it demonstrates your ability to configure Shortcode Attributes dynamically. You could even take this a step further by constructing custom forms on your site that contain options; which ultimately dictate the amount that you'll charge. Where the final amount is configured dynamically.

* NOTE: You MUST be in the HTML tab of your editor when adding PHP tags.

Re: TIP: Using variables in a Shortcode

PostPosted: May 18th, 2011, 2:59 am
by Jason Caldwell
Related topic. Someone asked about configuring Pro Form Attributes dynamically.
~ the example above can be applied to ANY Shortcode, even for Pro Forms.

However, there are two ways to dynamically configure a PayPal Pro Form.

1. Using variables in your Shortcode ( explained here )
( note, you can apply the concept discussed in this thread, to any Shortcode Attribute )
viewtopic.php?f=36&t=1604

2. Or, you can apply a Filter of your own, to all Pro Forms during checkout processing.
Create this directory and file:
/wp-content/mu-plugins/s2-hacks.php
Code: Select all
<?php
add_filter 
("ws_plugin__s2member_pro_paypal_checkout_post_attr", "my_paypal_pro_checkout_form_attr");
function my_paypal_pro_checkout_form_attr ($attr = array ())
    {
        if ($_POST["something-custom"] === "some custom value")
            {
                $attr["ra"] = "10.00"; /* Adjust price dynamically. */
                /* And/or, you could adjust other Attributes too. */
                /* $attr["rp"], $attr["rt"], etc, etc. */
            }
        /**/
        return (array)$attr;
    }
?>
^ this is for all PayPal Pro Checkout Forms, except Specific Post/Page Access.

This is for PayPal Pro Specific Post/Page Checkout Forms.
Code: Select all
<?php
add_filter 
("ws_plugin__s2member_pro_paypal_sp_checkout_post_attr", "my_paypal_pro_sp_checkout_form_attr");
function my_paypal_pro_sp_checkout_form_attr ($attr = array ())
    {
        if ($_POST["something-custom"] === "some custom value")
            {
                $attr["ra"] = "10.00"; /* Adjust price dynamically. */
                /* And/or, you could adjust other Attributes too. */
                /* $attr["ids"], $attr["exp"], etc, etc. */
            }
        /**/
        return (array)$attr;
    }
?>
^ this is for PayPal Pro Specific Post/Page Checkout Forms.

The examples above will also work for Authorize.Net Pro Forms.
Just change all occurrences of the word "paypal" in the code, to "authnet".

Re: TIP: Using variables in a Shortcode

PostPosted: September 6th, 2011, 2:57 pm
by Jason Caldwell
Pro Forms can also be generated dynamically via PHP code.
See this article: viewtopic.php?f=40&t=12764&p=34117#p34117

Re: TIP: Using variables in a Shortcode

PostPosted: January 26th, 2012, 12:47 am
by angelazou
But why did the shortcode running within a PHP environment failed to get interpreted (and end up like a literal value)? What are the caveats?

So using your example, my code look something like this:

Code: Select all
<?php
  $amount = $_REQUEST["amount"];
   do_shortcode('[s2Member-PayPal-Button ... ra="$amount" ... /]');
');
?>

Re: TIP: Using variables in a Shortcode

PostPosted: January 26th, 2012, 1:06 pm
by Raam Dev
angelazou,

You're not using the PHP Variables properly. Please see my response here: viewtopic.php?f=4&t=16906#p61640