V Posted May 15, 2010 Share Posted May 15, 2010 I'm trying to create my first function for Wordpress - unfortunately it's more complicated than I thought. It's to display a google checkout button. <?php function google_checkout() { echo "<div class="product"><input type="hidden" class="product-title" value="<?php the_title(); ?>"><input type="hidden" class="product-price" value="<?php echo get_post_meta($post->ID, "price_value", $single = true);?>"><div class="googlecart-add-button" tabindex="0" role="button" title="Add to cart"></div></div>"; } ?> the output is a "> and a google add to cart button. I tried removing the php tags from within the function but then the codes enclosed in those php tags don't work. Can someone please share some guidance? I would really like to learn how to make these type of functions work. Link to comment https://forums.phpfreaks.com/topic/201818-php-tags-within-a-function/ Share on other sites More sharing options...
trq Posted May 15, 2010 Share Posted May 15, 2010 This really is back to basics stuff. <?php function google_checkout() { echo "<div class=\"product\"><input type=\"hidden\" class=\"product-title\" value=\"" . the_title() . "\"><input type=\"hidden\" class=\"product-price\" value=\"" . get_post_meta($post->ID, "price_value", $single = true) . "\"><div class=\"googlecart-add-button\" tabindex=\"0\" role=\"button\" title=\"Add to cart\"></div></div>"; } ?> Your only real issue now is that $post does not existr anywhere within your function. You should pass it in as an argument. Link to comment https://forums.phpfreaks.com/topic/201818-php-tags-within-a-function/#findComment-1058588 Share on other sites More sharing options...
V Posted May 15, 2010 Author Share Posted May 15, 2010 Thank you for the reply Thrope! I originally had the code enclosed in single quotes so I didn't use the backslash. I'll look into making an argument for $post. Link to comment https://forums.phpfreaks.com/topic/201818-php-tags-within-a-function/#findComment-1058597 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.