• Resolved anonymized-23096784

    (@anonymized-23096784)


    I have the following PHP Code as a PHP Snippet:

    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 name
    echo 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)

    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:

    // Define the function that returns the domain name
    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;
    }

    // Create the shortcode and map it to the function
    function register_domain_name_shortcode() {
    add_shortcode('domain_name', 'display_domain_name');
    }

    // Hook into WordPress to register the shortcode
    add_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

    (@gripgrip)

    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)

    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

    (@gripgrip)

    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.