• Resolved Spencer Hill

    (@s3w47m88)


    Heya,

    Can anyone tell me the simplest way to get the domain from a URL within a plugin function?

    This means I only want mydomain.com, not proceeding https://www. or trailing elements.

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • What’s the purpose of this? Where does the output go?

    Thread Starter Spencer Hill

    (@s3w47m88)

    Well I have a plugin that uses wp_mail() to send an email to the user. The problem is that the user isn’t suppose to reply to that email address so I set it to [email protected] – but I need mysite.com to change based on the domain that the plugin is installed on instead of being static like it currently is.

    Does that make sense? Thanks for the reply!

    I see, you can use:

    <?php
        //Output curent domain without http
        echo $_SERVER['SERVER_NAME'];
    ?>

    Or you could make a constant out of it and then use it:

    <?php
    
       $domain_name = $_SERVER['SERVER_NAME'];
    
       //Output curent domain without http
       echo $domain_name;
    
    ?>
    Thread Starter Spencer Hill

    (@s3w47m88)

    Thanks for the solution but that still includes www. prefix…

    That’s an easy fix.:)

    <?php
    
       //Get rid of wwww
    $domain_name =  preg_replace('/^www\./','',$_SERVER['SERVER_NAME']);
    
       //output the result
    echo $domain_name;
    
    ?>
    Thread Starter Spencer Hill

    (@s3w47m88)

    Ah, okay, I presumed there was an actual function that did that. It seems like such a common need! Thanks!

    Well there are many ways to get the domain name and they all have different applications. This way is more suitable for your needs as it will work on every platform and it’s simple.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How do you get the WP domain only? Need help…’ is closed to new replies.