Skip to content

Escaping and Late Escaping #2

@tomjn

Description

@tomjn

I'm seeing a lot of cases where output is being collected in variables e.g. $html then printed out later down the code, and there is no escaping being added.

Instead, print immediatley and escape at the moment of output.

E.g.

?>
<a href="<?php echo esc_url( $url ); ?>"><?php echo esc_html( $link_text ); ?></a>
<?php

esc_url guarantees that you safely print out a URL, even if $url contains a malicious javascript snippet, it forces the result to be URL shaped. Using a temporary variable to batch up markup then printing it out later on allows you to miss escaping, or worse still double escape. It also means that the browser can't do anything with that markup until you send it, so no progressive rendering occurs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions