Insert nofollow in a "Powered By" link, except in the homepage

I want to insert the nofollow value in a Powered by link if the page is different than the homepage.

So I have written this:

function prefix_poweredby() {
    if ( is_front_page() ) {
        $html = 'a href=https://example.com target=_blanklink/a';
        echo $html;
    } else {
        $html = 'a href=https://example.com rel=nofollow target=_blanklink/a';
        echo $html;
    }
}
add_action('wp_footer', 'prefix_poweredby');

It's working just fine, but I'm pretty sure that there are other more elegant ways to write this function and I would love if you could share with me.

Topic footer nofollow functions Wordpress

Category Web


I don't know if it's any more elegant, but if you don't like duplicating the HTML code, you could do this:

function prefix_poweredby() {
    $html = '<a href="https://example.com"';
    if ( ! is_front_page() ) {
       $html .= ' rel="nofollow"';
    }
    $html .= ' target="_blank">link</a>';
    echo $html;
}
add_action('wp_footer', 'prefix_poweredby');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.