Home › Forums › Front-end Issues › Remove 'http://' from text fields
Hi!
I’ve got a number of listings with a text field that is a website that has been entered by our users. Some are full URLs (ie: http://site.com/) and some are simple ones (ie: site.com)
What I need to do is show the website’s URL, but without the “http://” or trailing “/” if they exist.
Is it possible to capture and filter out parts of a text field??
Thanks!
Using the get_field();
function you can store data in a variable.
http://www.advancedcustomfields.com/resources/get_field/
So let’s say the text box value is “http://example.com/” stored in a field called ‘url’.
First, we grab this value.
$site = get_field('url');
Then replace the http:// or https://
$site = str_replace(array('http://', 'https://'), '', $site);
Then we remove the trailing slash.
$site = rtrim($site, '/');
Finally, echo the value.
echo $site;
Hi, mine was:
<?php if( get_field('personal_website_or_blog_url') ): ?>
<a href="http://<?php $site = get_field('personal_website_or_blog_url');
$site = str_replace(array('http://', 'https://'), '', $site);
$site = rtrim($site, '/');
echo $site;?>" data-avia-related-tooltip="view website" target="_blank">
<i class="fa fa-television" title="Personal Website" aria-hidden="true"></i></a>
<?php endif; ?>
… obviously my field was ‘personal_website_or_blog_url’ so you would need to change that out for yours.
Here is my code. It return the correct name but not the url itself
It looks like this on my site
“facebook.com/abcphotography”
href = my.site.com/facebook.com/abcphotography
Does anyone know why the root url is there?
Thanks
<?php if ( $site = get_field( 'vendor_fb' ) ) : ?>
<?php
$site = str_replace(array('http://', 'https://'), '', $site);
$site = rtrim($site, '/');?>
<a class="vendor-link" href="<?php echo $site ?>" target="_blank"><?php echo $site ?></a>
<?php endif; ?>
@blueli The http(s)://
is the protocol part of the URL; it tells the browser that it’s an external link. An href
without a protocol is treated as an internal link.
If you’re removing the protocol for display reasons, you should save that to a separate variable instead.
Also, if you want to remove trailing slashes, there is a native WP function called untrailingslashit()
or trailingslashit()
for the opposite effect.
Good luck!
I see!! So I changed the code like below. It seems to work
Thanks
<?php if ( $site = get_field( 'vendor_fb' ) ) : ?>
<?php
$site = str_replace(array('http://', 'https://'), '', $site);
$site = rtrim($site, '/');
$url = get_field( 'vendor_fb' );?>
<a class="vendor-link" href="<?php echo $url ?>" target="_blank"><?php echo $site ?></a>
<?php endif; ?>
The topic ‘Remove 'http://' from text fields’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.