I am trying to create a shortcode to see if the url matches with the attribute, then return the content. So far I have:

add_shortcode( 'ifurl', 'ifurl' );

function ifurl( $atts, $content = null ) {
    $url =  'https://' . $_SERVER['SERVER_NAME']; 
    $current_tld = end( explode( ".", parse_url( $url, PHP_URL_HOST ) ) );
    $country_select = // Something to get attribute 'country'

    if( $current_tld == $country_select ) {
        return $content

But as you can see, I haven't been able to get the attributes to work correctly, and the if statement.

I would like to use it like this:

[ifurl country="com"] Show only if .com [/ifurl]

[ifurl country="uk"] Show only if .co.uk [/ifurl]
up vote 3 down vote accepted

to read the attribut country, you just need to read it in $atts

add_shortcode( 'ifurl', 'ifurl' );

function ifurl($atts, $content = null) {

 $url =  'https://' . $_SERVER['SERVER_NAME']; 
 $current_tld = end(explode(".", parse_url($url, PHP_URL_HOST)));

 if ($current_tld === $atts["country"]) {
  return $content;

  • Yep I just figured it out before you posted haha, thank you so much though! – Lisa Ivory Aug 19 at 6:10

While if ($current_tld === $atts["country"]) check will do the job for you when everything is perfect, it has the potential of generating error when your shortcode is either wrongly inserted or incomplete.

For example, check the following two scenarios:

// misspelled contry attribute
[ifurl contry="com"] Show only if .com [/ifurl]
// incomplete shortcode with no country attribute
[ifurl] Show only if .com [/ifurl]

In both these cases, WordPress will call your shortcode function, but your shortcode function will produce error or unexpected result because you haven't handled these.

One way to fix the CODE is to do an isset( $atts["country"] ) check, like this:

if ( isset( $atts["country"] ) && $current_tld === $atts["country"] )

However, an even better way is to first declare the default attributes before processing the shortcode using the shortcode_atts function. For example, the improved CODE could be like:

add_shortcode( 'ifurl', 'ifurl' );

function ifurl( $atts, $content = "" ) {
    $atts = shortcode_atts(
            'country' => 'com'
        ), $atts, 'ifurl' );

    $url =  'https://' . $_SERVER['SERVER_NAME']; 
    $current_tld = end( explode( ".", parse_url( $url, PHP_URL_HOST ) ) );

    if( $current_tld === $atts['country'] ) {
        return $content;
    return "";

With this CODE, we've made the .com site as default, so shortcode given in the following example will give output on .com site, but not on .co.uk site:

[ifurl] Show only if .com [/ifurl]

More importantly, it'll not throw any error.

Additionally, by doing it this way, you open the window to enhance your shortcode attributes to be altered by other plugins using a filter hook named shortcode_atts_{$shortcode_name} (in your case, it'll be shortcode_atts_ifurl).

This is very useful if you want to develop a plugin and want other developers to be able to enhance your plugin easily without having to modify it directly.

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.