How do I create a Shortcode that returns text if domain is .com, not .co.uk

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]

2 s
2

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;
 }

};

Leave a Comment