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 / capability.php
Last active August 29, 2015 14:05
Capability validation example
<?php
$post = get_post();
if ( current_user_can( 'edit_post_meta', $post->ID, '_email' ) && is_email( $_POST['email'] ) ) {
update_post_meta( $post->ID, '_email', $_POST['email'] );
}
@wpscholar
wpscholar / absint.php
Created August 13, 2014 01:34
Absint validation example
<?php
$volume_level = absint( $_POST['volume_level'] );
update_post_meta( get_the_ID(), 'volume_level', $volume_level );
@wpscholar
wpscholar / esc-html-e.php
Created August 13, 2014 01:29
esc_html_e() example
<h1><?php esc_html_e( 'Blog', 'textdomain' ); ?></h1>
@wpscholar
wpscholar / wpdb-delete.php
Created August 9, 2014 20:43
$wpdb->delete() example
<?php
global $wpdb;
$wpdb->delete( $wpdb->postmeta, array( 'meta_id' => 101 ), array( '%d' ) );
@wpscholar
wpscholar / wpdb-update.php
Created August 9, 2014 20:40
$wpdb->update() example
<?php
global $wpdb;
$wpdb->update(
$wpdb->posts,
array( 'post_type' => 'new' ),
array( 'post_type' => 'old' ),
array( '%s' ),
array( '%s' )
);
@wpscholar
wpscholar / wpdb-insert.php
Last active November 30, 2016 13:39
$wpdb->insert() example
<?php
global $wpdb;
$wpdb->insert(
$wpdb->postmeta,
array(
'post_id' => get_the_ID(),
'meta_key' => 'name',
'meta_value' => 'John'
),
@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 );
@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 / 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 / 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>