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™

TIP: Using variables in a Shortcode

Common Questions/Problems/Tips. Posted by Administrators & Support Reps.

TIP: Using variables in a Shortcode

Postby Jason Caldwell » January 13th, 2011, 4:17 am

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.
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: TIP: Using variables in a Shortcode

Postby Jason Caldwell » May 18th, 2011, 2:59 am

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".
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: TIP: Using variables in a Shortcode

Postby Jason Caldwell » September 6th, 2011, 2:57 pm

Pro Forms can also be generated dynamically via PHP code.
See this article: viewtopic.php?f=40&t=12764&p=34117#p34117
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: TIP: Using variables in a Shortcode

Postby angelazou » January 26th, 2012, 12:47 am

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" ... /]');
');
?>
User avatar
angelazou
Registered User
Registered User
 
Posts: 72
Joined: July 14, 2011

Re: TIP: Using variables in a Shortcode

Postby Raam Dev » January 26th, 2012, 1:06 pm

angelazou,

You're not using the PHP Variables properly. Please see my response here: viewtopic.php?f=4&t=16906#p61640
Raam Dev || Wherever you are, be there. || Please rate s2Member!
User avatar
Raam Dev
Developer
Developer
 
Posts: 810
Joined: October 26, 2011


Return to Common Questions/Problems/Tips

Who is online

Users browsing this forum: No registered users and 1 guest