Support

Account

Home Forums Front-end Issues Remove 'http://' from text fields

Solving

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;

  • This really helped me Daron, thanks for the post!

  • can you please share final code. i want to do similar thing also.

  • 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.

  • thank you so much. it is working nicely

  • 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; ?>
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.