WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

May 21 2012

[Infographic] Tekka SEO for WordPress

Tekka SEO for WordPress
Tekka SEO for WordPress Click for Larger Image

Written by Travis Smith · Categorized: Infographic, WordPress

May 20 2012

A REAL Regular Expression (Regex) for URLs using preg_match()

Today I wanted to find a regex expression that would validate any URL. For example, I wanted it to be able to validate the follow urls where the .com could be any extension:

  • http://domain.com, http://www.domain.com, http://subdomain.domain.com
  • https://domain.com, https://www.domain.com, https://subdomain.domain.com
  • www.domain.com
  • subdomain.domain.com
  • domain.com

Since I too wasted a ton of time looking for this I wanted to do a re-blog/re-post on this for me because I am not sure if I will find all those posts again.

I have slightly (and very slightly) modified splattermania's post.

As I wasted lots of time finding a REAL regex for URLs and resulted in building it on my own, I now have found one, that seems to work for all kinds of urls:

[php]<?php
$regex = "((https?|ftp)://)?"; // SCHEME
// My Version: $regex = "((https?|ftps?)://)?"; // SCHEME
$regex .= "([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?"; // User and Pass
$regex .= "([a-z0-9-.]*).([a-z]{2,3})"; // Host or IP
$regex .= "(:[0-9]{2,5})?"; // Port
$regex .= "(/([a-z0-9+$_-].?)+)*/?"; // Path
$regex .= "(?[a-z+&$_.-][a-z0-9;:@&%=+/$_.-]*)?"; // GET Query
$regex .= "(#[a-z_.-][a-z0-9+$_.-]*)?"; // Anchor
[/php]

Then, the correct way to check against the regex ist as follows:

[php]<?php
if( preg_match( "/^$regex$/", $url ) )
return true;
[/php]

splattermania's post is priceless and belongs on php.net, and it shows the value in checking php.net before Googling or in the midst of Googling for information about PHP.

You can test the expression with the Regular Expression (Regex) Test Tool by selecting preg_match() & entering the following:
[code gutter="false"]/^((https?|ftps?)://)?([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?([a-z0-9-.]*).([a-z]{2,3})(:[0-9]{2,5})?(/([a-z0-9+$_-].?)+)*/?(?[a-z+&$_.-][a-z0-9;:@&%=+/$_.-]*)?(#[a-z_.-][a-z0-9+$_.-]*)?/[/code]

So as a practical use in one line:
[php]<?php
if ( preg_match( '/^((https?|ftp)://)?([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?([a-z0-9-.]*).([a-z]{2,3})(:[0-9]{2,5})?(/([a-z0-9+$_-].?)+)*/?(?[a-z+&$_.-][a-z0-9;:@&%=+/$_.-]*)?(#[a-z_.-][a-z0-9+$_.-]*)?/', $url ) )
// do something
[/php]

Now, later you may need to add http:// to the front of a "validated" URL. Here's what I used:
[php]<?php
if ( isset( $url ) && ! preg_match( '~^(?:f|ht)tps?://~i', $url ) )
$url = 'http://' . $url;
[/php]

Or as a function:
[php]<?php
wps_url_fix('test.com' ); // http://test.com
wps_url_fix('www.test.com' ); // http://www.test.com
wps_url_fix('test.com' ); // http://test.com
wps_url_fix('ftp://test.com' ); // ftp://test.com
wps_url_fix('https://test.com' ); // https://test.com
wps_url_fix('http://test.com' ); // http://test.com
wps_url_fix('test' ); // http://test

/**
* Fixes URL, adds http:// if missing
*
* @param string $url String containing URL with(out) prefix
*/
function wps_url_fix( $url ) {
if ( isset( $url ) && ! preg_match( '~^(?:f|ht)tps?://~i', $url ) )
$url = 'http://' . $url;
return $url;
}
[/php]

Written by Travis Smith · Categorized: WordPress

May 14 2012

[Infographic] OnExtraPixel WordPress Cheatsheet

OnExtraPixel WordPress Cheatsheet
OnExtraPixel WordPress Cheatsheet Click for Larger Image

Written by Travis Smith · Categorized: Infographic, WordPress

May 07 2012

[Infographic] WPBeginner What is a WordCamp?

WPBeginner What is a WordCamp
WPBeginner What is a WordCamp Click for a Larger Image

Written by Travis Smith · Categorized: Infographic, WordPress

Apr 30 2012

How to Prevent Featured Images in Custom Post Types

So recently, someone asked if they could enabled Genesis Featured Images in Genesis > Theme Settings yet "disable" them for custom post types. While there is no option for this, it can be done! This is a rather simple task that can be complicated if post types are mixed and other circumstances. However, let's assume a simple setup.

In functions.php or your core functionality plugin where you created the custom post types, enter this code:
[php]
add_action( 'genesis_before_post_content', 'wps_do_post_image_check' );
/*
* Remove genesis_do_post_image() action if on post type archive of
* a custom post type
*
* @global stdClass $post Post object
* @uses genesis_get_option() Gets the genesis option for content archive thumbnails
*/
function wps_do_post_image_check() {
global $post;
if ( ! is_singular() && genesis_get_option( 'content_archive_thumbnail' ) && is_post_type_archive() && ! in_array( $post->post_type, array( 'page', 'post', 'attachment' ) ) )
remove_action( 'genesis_post_content', 'genesis_do_post_image' );
}
[/php]

NOTE: Though posts, pages, and attachments by default do not have archive pages, I cannot assume that in this code snippet.

Written by Travis Smith · Categorized: Genesis

  • « Previous Page
  • 1
  • …
  • 18
  • 19
  • 20
  • 21
  • 22
  • …
  • 60
  • Next Page »

Need Help?

Please let us know how we can help you!

Get Help

Recommendations

Genesis WordPress Framework
Sucuri Security
Gravity Forms
GetSoliloquy
Get Envira
Scribe SEO
BackupBuddy
WordPress Video User Manuals

Recent Posts

  • Solving WordPress 5XX Server Errors on SiteGround
  • Hiding an User in the WordPress Admin
  • Custom Rewrite Rules for Custom Post Types and Taxonomies
  • WordPress JavaScript Manager Native Functions
  • Causes of WordPress Site Performance Slowdown

About Travis

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in