Jump to content

php tags within a function


V

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.