Skip to content

Instantly share code, notes, and snippets.

View wpscholar's full-sized avatar
😀
Happy

Micah Wood wpscholar

😀
Happy
View GitHub Profile
@wpscholar
wpscholar / validate-postal-code.php
Created August 9, 2014 13:26
Validate postal code example
<?php
$postal_code = $_POST['postal_code'];
if ( preg_match( '/[0-9]{5}/', $postal_code ) ) {
update_post_meta( get_the_ID(), 'postal_code', $postal_code );
}
@wpscholar
wpscholar / sanitize-postal-code.php
Last active August 29, 2015 14:05
Sanitize postal code example
<?php
$postal_code = preg_replace( '/[^0-9]/', '', $_POST['postal_code'] );
update_post_meta( get_the_ID(), 'postal_code', $postal_code );
@wpscholar
wpscholar / esc-attr.php
Last active August 29, 2015 14:05
Escape attribute example
<div class="<?php echo esc_attr( $_POST['layout'] ); ?>">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy...
</div>
@wpscholar
wpscholar / esc-attr-e.php
Created August 9, 2014 19:47
Escape attribute with translation example
<input name="s" placeholder="<?php esc_attr_e( 'Search', 'textdomain' ); ?>" />
@wpscholar
wpscholar / esc-html.php
Last active August 29, 2015 14:05
Escape HTML example
<h1><?php echo esc_html( $title ); ?></h1>
@wpscholar
wpscholar / esc-url.php
Created August 9, 2014 19:55
Escape URL example
<a href="<?php echo esc_url( home_url( '/' ) ); ?>">
<img src="<?php echo esc_url( get_stylesheet_directory_uri() . '/img/logo.png' ); ?>" />
</a>
@wpscholar
wpscholar / esc-textarea.php
Created August 9, 2014 19:58
Escape textarea example
<label>
<span><?php _e( 'Label', 'textdomain' ); ?></span>
<textarea name="message"><?php echo esc_textarea( $_POST['message'] ); ?></textarea>
</label>
@wpscholar
wpscholar / esc-js.php
Last active August 29, 2015 14:05
Escape JS example
<script>
var name = '<?php echo esc_js( $_POST['name'] ); ?>';
</script>
<a href="<?php echo esc_url( home_url( '/blog/' ) ); ?>"
onclick="<?php echo esc_js( 'alert("Welcome " + name);' ); ?>">
<?php _e( 'Blog', 'textdomain' ) ?>
</a>
@wpscholar
wpscholar / wpdb-prepare.php
Last active January 15, 2025 21:08
$wpdb->prepare() example
<?php
global $wpdb;
$query = $wpdb->prepare(
"SELECT * FROM {$wpdb->posts} WHERE post_type = %s",
$post->post_type
);
$results = $wpdb->get_results( $query );
@wpscholar
wpscholar / like-escape.php
Created August 9, 2014 20:35
Like escape example
<?php
global $wpdb;
$post = get_post();
$query = $wpdb->prepare(
"SELECT * FROM {$wpdb->posts} WHERE post_type LIKE %s",
'%' . like_escape( $post->post_type ) . '%'
);
$results = $wpdb->get_results( $query );