Skip Navigation

[Resolved] Trying to get raw URL without HTTP / HTTPS

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 5 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1387219

Hey

I am trying to get the raw URL to print without the HTTP or HTTPS.

Looking at online documentation I used the following:

[types termmeta='image-for-category' output='raw' no_protocol='true'][/types]

But this doesn't seem to work? Am I missing something?

#1387229

Hi, based on the field name I'm assuming this is an image field. The no_protocol attribute is not available for the image field type, only for the URL field type: https://toolset.com/documentation/customizing-sites-using-php/functions/#image

So this would require a custom shortcode, or the use of a different field type here. The format and syntax for a custom shortcode looks like this:

add_shortcode( 'shortcode-name', 'shortcode_function');
function shortcode_function()
{
  // add your code here and return some value to display on screen:
  return $something;
}

I don't have a code snippet handy for this specific problem, but you would use the WordPress API get_term_meta to access custom term fields: https://developer.wordpress.org/reference/functions/get_term_meta/

$img_url = get_term_meta( 12345, 'wpcf-fieldslug', true );

... where 12345 represents the term ID.

To get the id of the current archive term, you can do something like this in your shortcode:

$current_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null;
$current_term_id = isset(get_queried_object()->taxonomy) ? get_queried_object()->term_id : 0;

Then you would need to do some string manipulation with PHP to strip off the leading protocol string, and return that value somehow with your custom shortcode. Here's a link I found with some code that seems to do the trick:
https://stackoverflow.com/questions/9549866/php-regex-to-remove-http-from-string

#1388099

Hey Christian

Thanks a lot for all this information. I think I am really close with this but for some reason it is stripping the HTTP/HTTPs from the URL and printing the correct URL on the page but as soon as I try and wrap an img tag round it, it doesn't work.

It looks like its printing the URL but with " " around it maybe, automatically. Here is the code I am using, any ideas why its not working correctly? Or is it not getting the raw URL from the database?

add_shortcode( 'category-url-no-http', 'shortcode_function');
function shortcode_function()
{
  $current_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null;
$current_term_id = isset(get_queried_object()->taxonomy) ? get_queried_object()->term_id : 0;
  $img_url = get_term_meta( $current_term_id, 'wpcf-image-for-category', true );
$str = $img_url;
$str = preg_replace('#^https?://#', '', $str);
echo $str;
  return $something;
}
#1388115

Don't use echo in a shortcode. Return the $str variable instead.

#1390545

Great thanks Christian. That worked.

Is there a way with Toolset to easily insert the alt tag set for the image? I can only see a way to insert the image tag or Raw URL no way to insert the alt text assigned to the image?

Cheers

#1390643

You can insert the alt attribute in the img tag using the Types field shortcode, but there's not an easy way to access just the alt text outside of that image tag. I think that would require a custom shortcode, because you would need to be able to access that image's attachment post ID somehow (probably by querying posts with the image URL as the post guid) then use that ID to get the postmeta value of "_wp_attachment_image_alt".

https://toolset.com/documentation/customizing-sites-using-php/functions/#image