How to Remove ‘http://’ from your ShortLink URL in WordPress PHP file?

Last updated
App Shah
Crunchify » WordPress Optimization and Tutorials » How to Remove ‘http://’ from your ShortLink URL in WordPress PHP file?

How to Remove http from your ShortLink URL in WordPress?

Do you show ShortLink on your blog post? Does it take somewhat bigger place?

If yes, then this post will help you remove “http://” from ShortLink URL. Of course, you can use the same php code for any URL.

// get WordPress Shortlink to variable $input
$input = wp_get_shortlink();

// in case scheme relative URI is passed, e.g., //www.crunchify.com/
//$input = trim($input, '/');

// If scheme not included, prepend it
if (!preg_match('#^http(s)?://#', $input)) {
    $input = 'http://' . $input;
}

$urlParts = parse_url($input);

// Get Domain. i.e. crunchify.com
$domain = preg_replace('/^www\./', '', $urlParts['host']);

// Get Path. i.e. /path
$path = preg_replace('/^www\./', '', $urlParts['path']);

// Merge to generate complete URL
$link = $domain. $path;

On Crunchify, we are using Genesis Framework and I’m using below complete code which I’ve put it in functions.php file.

NOTE: Be careful before you put below code to your theme. This works only for Genesis Theme. For non-Genesis theme, please use above custom code as per your theme’s functionality.

// This is custom utility related to my Genesis theme.

// Be careful before you put this into your theme. Make sure you test it on staging site
// OR Contact us and we are more than happy to help

function crunchify_get_updated_date_for_post() {
    $date = '<i>' . get_the_modified_date() . '</i>'; 
    return $date;
}
add_shortcode( 'crunchify_modified_date', 'crunchify_get_updated_date_for_post' );

add_filter( 'genesis_post_info', 'crunchify_add_updated_date_info' );
function crunchify_add_updated_date_info($post_info) {
	$post_info = 'Last Updated on [crunchify_modified_date] by [post_author_posts_link] [post_comments] [post_edit]';
        if (is_single()) { 

		// get WordPress Shortlink to variable $crunchifyURL
		$crunchifyURL = wp_get_shortlink();
 
		// in case scheme relative URI is passed, e.g., //www.crunchify.com/
		//$crunchifyURL = trim($crunchifyURL, '/');
 
		// If scheme not included, prepend it
		if (!preg_match('#^http(s)?://#', $crunchifyURL)) {
		    $crunchifyURL = 'http://' . $crunchifyURL;
		}
 
		$urlParts = parse_url($crunchifyURL);
 
		// Get Domain. i.e. crunchify.com
		$domain = preg_replace('/^www\./', '', $urlParts['host']);
 
		// Get Path. i.e. /path
		$path = preg_replace('/^www\./', '', $urlParts['path']);
 
		// Merge to generate complete URL
		$crunchifyURL = $domain. $path;

	?>
		<p class='entry-meta-shortlink'><span class='custom-shortlink'>Short link: <input readonly type='text' class='shortlink-textbox' value='<?php echo $crunchifyURL; ?>' onclick='this.focus(); this.select();' /></span>
	        <?php 
		 	return $post_info;
		?>
		</p>
		<?php
	}else{
		return $post_info;
	}
}

Let’s see how it looks before and after.

Before:

Before Removing HTTPS from Short URL - Crunchify Tips

After:

After Removing HTTPS from Short URL - Crunchify Tips

4 thoughts on “How to Remove ‘http://’ from your ShortLink URL in WordPress PHP file?”

  1. I wish this worked for me. I added the top piece of code to my functions.php (I’m not using Genesis but rather Roots Sage 8) and am using to create a twitter share but it doesn’t remove the https. any idea why it might not be working?

    Reply
    • Hi Jason – can you please share screenshot? Also, let me know your complete code snippet which you have added to your theme.

      Reply

Leave a Comment