0

Trying to get a function working for wordpress. Using a little bit of code. Problems with syntax or something. Code is in wordpress functions.php. Output should be a text link with the product-id in it. So after click on link the url wil be example: http://www.websitenname.com/interesse?id=33

I have tested this code with-in a template (single-product woocommerce) with succes. This should now work in functions.php for another project (using a short-code in a template (wordpress)) Maybe someone know's what I'm doing wrong

function vComp(){
<a class="interessebutton" href="http://www.websitenname.com/interesse?id=<?php echo $product->id; ?>">Interesse</a>
}
add_shortcode( 'vInteresse', 'vComp' );

Hope to learn what I'm doing wrong. Syntax error: syntax error, unexpected 'vInteresse' (T_STRING)

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46

1 Answers1

1

I have no idea about the internal workings of Wordpress whatsoever but the cited function is wrong on a couple of levels. Perhaps look at the following to see if it does what you need - or gets you closer.

function vComp($product=false, $title=false){
    return $product && $title && is_object( $product ) && property_exists( $product, 'id' ) ? sprintf('<a class="interessebutton" href="/interesse?id=%s">%s</a>', $product->id, $title ) : false;
}

The function here will return a value which can then be used by the Wordpress function add_shortcode - whether this is what was intended or not I cannot say for sure.

Essentially the function says "if there is a product ( which is an object and has an ID attribute ) and a title - return the html link"

After looking more closely I suspect the above would not have worked ( unless it is possible to supply addition args to the Wordpress function add_shortcode ) so:

function vComp(){
    global $product;
    return $product && is_object( $product ) && property_exists( $product, 'id' ) ? sprintf('<a class="interessebutton" href="/interesse?id=%s">Interesse</a>', $product->id ) : false;
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • I've tried this, not working. No output.. no syntax or other errors.. that's a good step forward in a way.. – Roy Claassen Aug 07 '19 at 18:41
  • looking at what I posted and then looking at `add_shortcode( 'vInteresse', 'vComp' );` leads me to think this will not actually work as one would appear unable to add parameters to the `add_shortcode` function – Professor Abronsius Aug 07 '19 at 18:42