JoaMika
JoaMika

Reputation: 1827

Escape HTML to process PHP

I am using this code but the PHP part <?php echo wp_logout_url(); ?> is not rendered.

function choose_bar() {
    $html = <<<HTML
<div class="memlog"><a class="mem-in ttmem x-btn x-btn-small" href="/login/" ><i class="x-icon-globe" data-x-icon="&#xf0ac;"></i>Login</a><a class="mem-out ttmem x-btn x-btn-small" href="<?php echo wp_logout_url(); ?>" title="Logout">Logout</a></div>
HTML;

    return $html;
}

How can I do this?

Upvotes: 0

Views: 50

Answers (1)

elixenide
elixenide

Reputation: 44833

The problem is that you cannot use PHP tags (<?php ... ?>) inside a heredoc string (the <<<HTML ... HTML; in your code). While variables ($foo) are evaluated, functions (wp_logout_url()) and PHP tags are not.

The easiest solution is this: Just define a variable and use it in your HTML instead of calling the function wp_logout_url().

Working example:

function choose_bar() {
    $logoutUrl = wp_logout_url();
    $html = <<<HTML
<div class="memlog"><a class="mem-in ttmem x-btn x-btn-small" href="/login/" ><i class="x-icon-globe" data-x-icon="&#xf0ac;"></i>Login</a><a class="mem-out ttmem x-btn x-btn-small" href="$logoutUrl" title="Logout">Logout</a></div>
HTML;

    return $html;
}

Also, FYI, there's no need for a declaration of $html here; you can just do a return:

function choose_bar() {
    $logoutUrl = wp_logout_url();
    return <<<HTML
<div class="memlog"><a class="mem-in ttmem x-btn x-btn-small" href="/login/" ><i class="x-icon-globe" data-x-icon="&#xf0ac;"></i>Login</a><a class="mem-out ttmem x-btn x-btn-small" href="$logoutUrl" title="Logout">Logout</a></div>
HTML;
}

Upvotes: 3

Related Questions