Skip to content

Instantly share code, notes, and snippets.

@khoipro
Last active August 26, 2023 04:50
Show Gist options
  • Save khoipro/c8106b9d35be48c7ea75414922e1ca07 to your computer and use it in GitHub Desktop.
Save khoipro/c8106b9d35be48c7ea75414922e1ca07 to your computer and use it in GitHub Desktop.
Escape WordPress Sample PHP code
<?php
/** Sample escape output for WordPress theme
* @author codetot
* @package codeStandard
**/
?>
<?php // Simple escape html // ?>
<h2 class="product-card__title"><?php echo esc_html( $title ); ?></h2>
<?php
// Escape link with esc_url_raw()
// Escape attribute with esc_attr()
// Escape html with esc_html()
?>
<h2 class="product-card__title">
<a class="product-card__title-link" href="<?php echo esc_url_raw( $link ); ?>" title="<?php echo esc_attr( sprintf( __('Visit %s', 'text-domain'), $title ) ); ?>">
<?php echo esc_html( $title ); ?>
</a>
</h2>
<?php
// Escape translation attribute with esc_attr_e()
// Escape
?>
<button class="btn btn-sm" aria-label="<?php echo esc_attr_e('Open mobile menu', 'text-domain'); ?>">
<span class="menu-icon"></span>
<span class="menu-icon"></span>
<span class="menu-icon"></span>
</button>
<?php
// Escape translation html
?>
<button class="btn btn-sm btn-danger">
<?php esc_html_e('View more', 'text-domain'); ?>
</button>
<?php
// Escape with numbers _n
$rating_number = 1;
$rating_text = sprintf( _n( '%s star', '%s stars', $rating_number, 'text-domain' ), $rating_number );
?>
<span class="rating-number"><?php echo esc_html( $rating_text ); ?></span>
<?php
// Escape with numbers _n
// Have number_format_i18n() to format
$comment_text = sprintf(
_n(
'%s comment',
'%s comments',
$comment_total,
'text-domain'
),
number_format_i18n($comment_total )
);
?>
<span class="comment-number"><?php echo esc_html( $comment_text ); ?></span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment