Title: PHP Code Not Working &#8211; Display Domain Name
Last modified: July 17, 2024

---

# PHP Code Not Working – Display Domain Name

 *  Resolved anonymized-23096784
 * (@anonymized-23096784)
 * [1 year, 10 months ago](https://wordpress.org/support/topic/php-code-not-working-display-domain-name/)
 * I have the following PHP Code as a PHP Snippet:
 *     ```wp-block-code
       function display_domain_name() {    // Get the site URL    $site_url = get_site_url();    // Parse the URL to get the host    $parsed_url = parse_url($site_url);    $domain = $parsed_url['host'];    // Return the domain    return $domain;}// Print domain nameecho display_domain_name();
       ```
   
 * I am configuring this code as a Shortcode.
 * For some reason this is not working properly. If I use the shortcode once in 
   the page, it displays the value correctly (domain name – e.g. `example.com`).
   If I use the shortcode more than once in the page, the page breaks (error 500).
 * I have tried to remove the `echo` as well, but in that case, it doesn’t display
   anything in the page where the shortcode is inserted once, and in the page where
   the shortcode is inserted more than once, the page still breaks (error 500).
 * Any idea why this is happening and how to fix it? Can the same shortcode be inserted
   multiple times in the same page?
    -  This topic was modified 1 year, 10 months ago by anonymized-23096784.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter anonymized-23096784
 * (@anonymized-23096784)
 * [1 year, 10 months ago](https://wordpress.org/support/topic/php-code-not-working-display-domain-name/#post-17896582)
 * For example, I have tried to modify the code a little bit. Instead of running
   as a Shortcode, I run it **Auto-Insert > Frontend Only** – This is the updated
   code:
 *     ```wp-block-code
       // Define the function that returns the domain namefunction display_domain_name() {    // Get the site URL    $site_url = get_site_url();    // Parse the URL to get the host    $parsed_url = parse_url($site_url);    $domain = $parsed_url['host'];    // Return the domain    return $domain;}// Create the shortcode and map it to the functionfunction register_domain_name_shortcode() {    add_shortcode('domain_name', 'display_domain_name');}// Hook into WordPress to register the shortcodeadd_action('init', 'register_domain_name_shortcode');
       ```
   
 * In this case, it is working properly.
   So I am not sure why in the first scenario,
   it is not working.
 *  Plugin Author [Mircea Sandu](https://wordpress.org/support/users/gripgrip/)
 * (@gripgrip)
 * [1 year, 10 months ago](https://wordpress.org/support/topic/php-code-not-working-display-domain-name/#post-17896647)
 * Hi @hustleou,
 * In the first scenario, your code is attempting to define the same function multiple
   times which is not possible in PHP.
 * This is happening because each time the shortcode is used the WPCode snippet 
   code is executed.
 * If you want to use that shortcode multiple times you can do the approach that
   you did in the 2nd message or you can define the function in a separate snippet
   that you set to “Run Everywhere” and then define a snippet that you use as a 
   shortcode where you just call the last line in your code with the echo.
 *  Thread Starter anonymized-23096784
 * (@anonymized-23096784)
 * [1 year, 10 months ago](https://wordpress.org/support/topic/php-code-not-working-display-domain-name/#post-17897904)
 * Ok, got it! Maybe you can provide a link to a documentation that explains this?
   And maybe add it in the plugin interface for better clarification.
 *  Plugin Author [Mircea Sandu](https://wordpress.org/support/users/gripgrip/)
 * (@gripgrip)
 * [1 year, 10 months ago](https://wordpress.org/support/topic/php-code-not-working-display-domain-name/#post-17897989)
 * Hi @hustleou,
 * Thank you for the suggestion, we’ll look into ways to make this clearer. I can
   see how it can be confusing, the snippet in WPCode basically acts as the callback
   function for how you would add a shortcode in PHP directly. Instead of wrapping
   the code in a function you can just drop it in the snippet.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘PHP Code Not Working – Display Domain Name’ is closed to new replies.

 * ![](https://ps.w.org/insert-headers-and-footers/assets/icon-256x256.png?rev=2758516)
 * [WPCode - Insert Headers and Footers + Custom Code Snippets - WordPress Code Manager](https://wordpress.org/plugins/insert-headers-and-footers/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/insert-headers-and-footers/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/insert-headers-and-footers/)
 * [Active Topics](https://wordpress.org/support/plugin/insert-headers-and-footers/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/insert-headers-and-footers/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/insert-headers-and-footers/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [Mircea Sandu](https://wordpress.org/support/users/gripgrip/)
 * Last activity: [1 year, 10 months ago](https://wordpress.org/support/topic/php-code-not-working-display-domain-name/#post-17897989)
 * Status: resolved