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 / 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 / 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-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 / esc-html-e.php
Created August 13, 2014 01:29
esc_html_e() example
<h1><?php esc_html_e( 'Blog', 'textdomain' ); ?></h1>
@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 / 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 / verify-nonce.php
Created August 13, 2014 01:43
Verify nonce example
<?php
if ( wp_verify_nonce( $_POST['nonce'], 'update_email' ) && is_email( $_POST['email'] ) ) {
update_post_meta( get_the_ID(), 'email', sanitize_email( $_POST['email'] ) );
}
@wpscholar
wpscholar / trust-wordpress.php
Created August 13, 2014 01:54
WordPress functions properly escape, but must be used in the proper context.
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
@wpscholar
wpscholar / sanitize-option.php
Last active August 29, 2015 14:05
Sanitize option example
<?php
add_filter(
"sanitize_option_{$option_name}",
function ( $option_value, $option_name ) {
return filter_var( $option_value, FILTER_VALIDATE_BOOLEAN );
},
10,
2
);
@wpscholar
wpscholar / query-arg-alias.php
Created September 11, 2014 14:16
Create an alias for a query argument
<?php
/**
* Class Query_Arg_Alias
*/
class Query_Arg_Alias {
/**
* Query var
*