Support

Account

Home Forums General Issues How to clean up URL's for consistent output – from textfield

Solving

How to clean up URL's for consistent output – from textfield

  • Hello,

    I have a text field (well three of them actually) where users can enter their facebook, twitter, and soundcloud pages. the problem is some people enter the values with http, with https, or without either. How can I clean these values so that I can properly link to the pages?

    Here is an example of my current template code.

    	<div class="social-shit clearfix">
    	<h3><?php the_title(); ?> Social Links</h3>
    
    <?php if( get_field('facebook_url') ): ?>
    	<a href="http://<?php the_field('facebook_url'); ?>">
    	<div class="ww-sl-facebook">	
    	</div></a>
    <?php endif; ?>
    
    <?php if( get_field('twitter_url') ): ?>
    	<a href="http://<?php the_field('twitter_url'); ?>">
    	<div class="ww-sl-twitter">	
    	</div></a>
    <?php endif; ?>
    
    <?php if( get_field('soundcloud_url') ): ?>
    	<a href="http://<?php the_field('soundcloud_url'); ?>">
    	<div class="ww-sl-soundcloud">	
    	</div></a>
    <?php endif; ?>
    
    	</div>
    

    Thanks!

  • Better use a URL field type that gets validated upon post publish or update so the users get an error message if the URL is not in the correct format without http:// or https:// . It’s better practice not to hardcode the http:// in your template because users usually copy links from the browsers’ address bar and http:// always gets copied and that will brake the links.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.