Title: Use variables within shortcode
Last modified: August 30, 2016

---

# Use variables within shortcode

 *  Resolved [2cats](https://wordpress.org/support/users/2cats/)
 * (@2cats)
 * [11 years ago](https://wordpress.org/support/topic/use-variables-within-shortcode/)
 * Hi, I’d like to make the cart buttons more easily updated by assigning variables
   to the price and title and inserting those into the shortcode. So instead of `[
   wp_cart_button name="Test Product" price="29.95"]` I would have something like(
   this is using the ACF plugin but you could just use custom fields):
 *     ```
       <?php $title = the_field('product_title');
       	$price = the_field('product_price');
       echo do_shortcode('[wp_cart_button name="'.$title.'" price="'.$price.'"]'); ?>
       ```
   
 * This doesn’t work — neither title nor price are registering in the button — is
   this possible? This way I could call the same product into the Shop page and 
   into sidebars without having to change the shortcode in several places every 
   time there is a product or price change. Thank you!
 * [https://wordpress.org/plugins/wordpress-simple-paypal-shopping-cart/](https://wordpress.org/plugins/wordpress-simple-paypal-shopping-cart/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [mra13 / Team Tips and Tricks HQ](https://wordpress.org/support/users/mra13/)
 * (@mra13)
 * [11 years ago](https://wordpress.org/support/topic/use-variables-within-shortcode/#post-6235114)
 * Before you enter the variable in the shortcode, make sure it actually has value
   in it. So echo the following variable content and make sure it actually has some
   value before you use it in the shortcode:
 * $title = the_field(‘product_title’);
    $price = the_field(‘product_price’);
 *  Thread Starter [2cats](https://wordpress.org/support/users/2cats/)
 * (@2cats)
 * [11 years ago](https://wordpress.org/support/topic/use-variables-within-shortcode/#post-6235123)
 * Hi mra13, thanks for your reply. Yes, I checked that first — the variables do
   have a value. So I think I have done something wrong inserting them into the 
   shortcode, but haven’t been able to figure out what. Thanks again!
 *  Thread Starter [2cats](https://wordpress.org/support/users/2cats/)
 * (@2cats)
 * [11 years ago](https://wordpress.org/support/topic/use-variables-within-shortcode/#post-6235160)
 * Ack — my apologies, mra13! You were totally right. `the_field` was echoing out
   the field contents, I had to switch to `get_field` to register the variable. 
   Now all is right as rain. Thank you so much — the site will only be selling two
   products, via PayPal, so your lightweight simple plugin seems ideal. Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Use variables within shortcode’ is closed to new replies.

 * ![](https://ps.w.org/wordpress-simple-paypal-shopping-cart/assets/icon-128x128.
   png?rev=2722490)
 * [Simple Shopping Cart](https://wordpress.org/plugins/wordpress-simple-paypal-shopping-cart/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wordpress-simple-paypal-shopping-cart/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wordpress-simple-paypal-shopping-cart/)
 * [Active Topics](https://wordpress.org/support/plugin/wordpress-simple-paypal-shopping-cart/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wordpress-simple-paypal-shopping-cart/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wordpress-simple-paypal-shopping-cart/reviews/)

## Tags

 * [variable product price](https://wordpress.org/support/topic-tag/variable-product-price/)

 * 3 replies
 * 2 participants
 * Last reply from: [2cats](https://wordpress.org/support/users/2cats/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/use-variables-within-shortcode/#post-6235160)
 * Status: resolved