WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How do you get the WP domain only? Need help... (8 posts)

  1. Spencer Hill
    Member
    Posted 2 years ago #

    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 http://www. or trailing elements.

    Thanks!

  2. shirazdrum
    Member
    Posted 2 years ago #

    What's the purpose of this? Where does the output go?

  3. Spencer Hill
    Member
    Posted 2 years ago #

    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 donotreply@mysite.com - 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!

  4. shirazdrum
    Member
    Posted 2 years ago #

    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;
    
    ?>
  5. Spencer Hill
    Member
    Posted 2 years ago #

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

  6. shirazdrum
    Member
    Posted 2 years ago #

    That's an easy fix.:)

    <?php
    
       //Get rid of wwww
    $domain_name =  preg_replace('/^www\./','',$_SERVER['SERVER_NAME']);
    
       //output the result
    echo $domain_name;
    
    ?>
  7. Spencer Hill
    Member
    Posted 2 years ago #

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

  8. shirazdrum
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.