homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Using PHP in WordPress with "Shortcode"
Trying to get PHP to calculate using Wordpress shortcodes as variables

 5:30 am on Jan 22, 2010 (gmt 0)

I have a Wordpress blog with the exec-php plugin activated.

I also have a custom plugin that I use to insert data into posts.

For example, the shortcode [BasePrice] may contain 27. The shortcode is used in a Wordpress post as $[BasePrice] to insert the 27 into the post, and $$[BasePrice] to insert $27.

I suspect that these shortcodes retrieve the data as strings, but I did not code the plugin, as I am not a coder.

I have another plugin called "Raw HTML" which keeps WordPress from breaking some HTML. Using this plugin, I been able to dynamically price my PayPal button code by inserting the $[BasePrice] shortcode from the custom plugin.

However, I do not seem to be able to use the shortcode to insert a variable into a PHP calculation.

This works ...

$firstnum = 32;
$secnum = 27;
echo $firstnum - $secnum;

... and displays 5.

This breaks ...

$firstnum = $[BasePrice];
$secnum = $[LowPrice];
echo $firstnum - $secnum;

As does this (using the "Raw HTML" plugin) ...

$firstnum = <!--end_raw-->$[BasePrice]<!--start_raw-->;
$secnum = <!--end_raw-->$[LowPrice]<!--start_raw-->;
echo ($firstnum-$secnum);

Can anyone identify my problem (other than I shouldn't be allowed on a computer at my age) and a solution?

Kirk Ward



 5:09 pm on Jan 22, 2010 (gmt 0)

Is $[BasePrice] and $[LowPrice] strings or numbers? As I understand it if adding string together you are effectively doing "27" - "16" which probably won't come out as expected.


 2:24 am on Jan 23, 2010 (gmt 0)

I did not write the code, and don't know how to check what type of variable they are, but as I stated above, my guess is they are strings.

If they are strings, that would explain the code breaking, but I didn't think types were normally used in PHP.

How would I check to answer your question?


 4:50 am on Jan 23, 2010 (gmt 0)

If you could copy and paste the code where baseprice and lowprice are declared, we might be able to offer a solution. There is nothing functionally wrong with the code you have posted above, so the root problem must lie elsewhere.


 1:20 pm on Jan 23, 2010 (gmt 0)

The custom plugin dynamically creates shortcodes to insert the variable into a wordpress page. I had it built so I could manage product pricing on any page in my site. For example, if I wanted to mention the base price of my product on 43 different pages, I could create a shortcode and raise or lower prices whenever I wanted and they would all be instantly updated.

Here is a link to download the full code.


Thanks for your interest.


[edited by: eelixduppy at 4:34 am (utc) on Jan. 25, 2010]
[edit reason] Please post relevant code to thread [/edit]


 4:06 pm on Jan 24, 2010 (gmt 0)

Any helpful ideas from that code?



 12:03 pm on Jan 29, 2010 (gmt 0)

This breaks ...

$firstnum = $[BasePrice];
$secnum = $[LowPrice];
echo $firstnum - $secnum;

When you say "this breaks" ... what do you mean, what error message do you receive? At first pass I would guess that BasePrice is a constant [php.net], as is LowPrice. But then the braces syntax doesn't make sense so these are more likely template variables that need to be parsed by WP instead.

Understanding PHP string [php.net] syntax will help clarify and perhaps point you in the right direction. I believe it's going to be the WP plug-in documentation.


 2:33 pm on Jan 29, 2010 (gmt 0)

When I say it "breaks," I am saying that the remainder of the WordPress page after where that code appears does not run. The page is rendered or shown on screen up until where the code appeared. After that there is blank page.

The brackets are standard WordPress formatting for "shortcode" variables which are parsed by various plugins. I had previously pasted a link to the zipped, but unencrypted plugin code, but it appears to have been deleted (I presume by an admin or moderator- see the <snip> in the prior post).

I do not know how to code, so me examining it is useless, which is why I tried to provide access to the code as a previous poster had suggested I paste it here. It seemed a bit long for that.

Any suggestions?


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved