1

I am using the following code to create a shortcode that generates the url of my website:

add_shortcode( 'base_url', 'baseurl_shortcode' );
function baseurl_shortcode( $atts ) {

    return site_url();

}

However, when the url is generated it has https:// at the beginning of it. Is there a way to remove this please?

1
  • (Although it might be helpful to know why you want to remove the https...) Nov 8, 2022 at 21:07

2 Answers 2

2

That would be: return parse_url( get_site_url(), PHP_URL_HOST ) );

Explanation:

get_site_url is the WP function that returns the full url (it is the function that site_url relies on). parse_url is a PHP function that splits the url into several components and returns one or more of them. In this case it returns the hostname, which you seem to be looking for.

0
1

That's great, thanks very much!

This is the code I ended up using:

// Add site url shortcode
function baseurl_shortcode($atts) {

    return parse_url(site_url(), PHP_URL_HOST);

}
add_shortcode( 'base_url', 'baseurl_shortcode' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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