1

Using Custom Fields in Joomla, I created several custom fields for my article layout. The data that will populate the custom fields will include 1) vendor name, and 2) a URL path to the vendor's website.

The php code that loads that data is:

(For the vendor name)

<?php echo $this->item->jcfields[7]->value; ?>

(For the vendor's website URL)

<?php echo $this->item->jcfields[19]->value; ?>

The result should be: Vendor

My current code (for testing) is:

<?php echo $buyURL; ?><br>
<a href='"<?php echo $buyURL; ?>"'> <?php echo $vendorName; ?></a>

I confirmed the $buyURL variable works correctly and displays the correct URL value when echo'd by itself.

When it's used inside <a href=""></a> statements, the resulting embedded URL link points to: https://my-website.com/demo/category/"<"a href="https://vendor-website.com/tdmSmO" rel="nofollow noopener noreferrer" target="_blank">https://vendor-website.com/tdmSmO</a>"

It appears Joomla's url.php from /plugins/fields/urls/tmpl has a major role in the wrong URL target.

How do I resolve this problem with the wrong URL target for custom URL fields?

1 Answer 1

1

Field's value property returns the rendered HTML. To get the value as entered in the form, use rawvalue property. Additionally, all user input must be escaped to prevent XSS attacks. And it would be a good idea to check that the fields are present, to prevent PHP warnings and invalid HTML output:

<?php if (!empty($this->item->jcfields[7]->rawvalue) && !empty($this->item->jcfields[19]->rawvalue)) : ?>
    <?php $vendorName = $this->item->jcfields[7]->rawvalue ?>
    <?php $buyURL = $this->item->jcfields[19]->rawvalue ?>
    <a href="<?= $this->escape($buyURL) ?>"><?= $this->escape($vendorName) ?></a>
<?php endif ?>
3
  • that code worked! I cannot thank you enough! I dont know enough about php to even know about those conditions. Much appreciation for your quick and excellent help!
    – GinA
    Commented Oct 29 at 10:02
  • I also have an image URL custom field and global variable that I want to display on the frontend with <img src=""> code. I've tried different variations of your suggestion above without success. Could you please provide me with code which uses $imgURL variable as the image source. That code is: '<?php if (isset($this->item->jcfields[12])): ?> <?php echo $this->item->jcfields[12]->value; ?> <?php endif; ?>' Thanks again for the excellent solution!
    – GinA
    Commented Oct 29 at 11:14
  • I figured it out. Thank you for telling me about the 'rawvalue' property. I added a global variable for imgURL with the 'rawvalue' property and used this code to display the image on the website: ' <div class="image-url"><?php if (isset($this->item->jcfields[12])): ?> <?php echo "<img src='{$imgURL}' alt='' class='imgURL>'"; ?> <?php endif; ?></div>'
    – GinA
    Commented Oct 29 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.