underbooth Posted September 20, 2018 Share Posted September 20, 2018 Hello. Its my first time posting here. I got next issue: My code is this: function product_attribute_legatura(){ global $product; $taxonomy = 'pa_legatura'; $value = $product->get_attribute( $taxonomy ); $protocol = 'http'; $delim = '://'; $path = 'www'; $file = $value; $url = $protocol . $delim . $path . urlencode($value); if ( $value ) { $label = get_taxonomy( $taxonomy )->labels->singular_name; echo '<p>' . '<a href=' . $url;'></a>' . '</p>'; echo '<p>' . 'Link here:' . '</p>'; Getting $value from database work perfect (its a text filed) I want to make it a link by setting up protocols first time and the web site is retrieved from $value. I concatenate them and i just want to transform it in a link. My code generate a link but like this : http://www.google.com<a (the <a is the issue) need help writing the correct a href output. Thank you in advance Link to comment Share on other sites More sharing options...
Barand Posted September 20, 2018 Share Posted September 20, 2018 try echo "<p><a href='$url'>Link here</a></p>"; Link to comment Share on other sites More sharing options...
gizmola Posted September 20, 2018 Share Posted September 20, 2018 I would use Barand's suggestion. With that said, your code has a syntax error and that is why it didn't work as you expected: echo '<p>' . '<a href=' . $url;'></a>' . '</p>'; You have an extraneous ';' in there. To fix it would just require: echo '<p>' . '<a href=' . $url . '></a>' . '</p>'; You are doing needless concatenation of string constants which is one reason I would prefer Barand's take. However, my opinion is that html attributes should be enclosed within double quotes (even though single quotes are acceptable), so a more complete fix would be: echo '<p><a href="' . $url . '"></a></p>'; Link to comment Share on other sites More sharing options...
Barand Posted September 21, 2018 Share Posted September 21, 2018 On reflection, I concur with Gizmola on the url and double quotes issue (There is a possibilty that a url could contain an apostrophe). In which case echo "<p><a href=\"$url\">Link here</a></p>"; It still avoids concatenation, which as you have demonstrated, can be error prone. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.