Welcome to WebmasterWorld Guest from 54.145.209.34

Forum Moderators: coopster & jatar k

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)

10+ Year Member



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 ...

<?php
$firstnum = 32;
$secnum = 27;
echo $firstnum - $secnum;
?>

... and displays 5.

This breaks ...

<?php
$firstnum = $[BasePrice];
$secnum = $[LowPrice];
echo $firstnum - $secnum;
?>

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

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

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

Thanks,
Kirk Ward

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

WebmasterWorld Senior Member 10+ Year Member



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)

10+ Year Member



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)

WebmasterWorld Senior Member 5+ Year Member



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)

10+ Year Member



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.

<snip>

Thanks for your interest.

Kirk

[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)

10+ Year Member



Any helpful ideas from that code?

Thanks,
Kirk

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

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



This breaks ...

<?php
$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)

10+ Year Member



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?

Kirk

 

Featured Threads

Hot Threads This Week

Hot Threads This Month