Skip to content

Instantly share code, notes, and snippets.

@jgalea
jgalea / activation-deactivation.php
Last active December 19, 2015 10:19
Activation and deactivation procedure for licenses in Easy Digital Downloads, Software Licensing add-on.
<?php
add_action( 'admin_init', 'wprss_et_activate_deactivate_license' );
/**
* Handles the activation/deactivation process
*
* @since 1.1
*/
function wprss_et_activate_deactivate_license() {
// listen for our activate button to be clicked
@jgalea
jgalea / add-capability.php
Last active December 19, 2015 16:19
Adds a capability to an existing role.
<?php
function add_capability() {
// gets the author role
$role = get_role( 'author' );
// This only works, because it accesses the class instance.
$role->add_cap( 'edit_others_posts' );
}
add_action( 'admin_init', 'add_capability');
@jgalea
jgalea / current-user-can.php
Created July 12, 2013 09:20
Check if current user has a particular capability.
<?php
if ( current_user_can( 'moderate_comments' ) ) {
echo 'The current user can moderate comments';
}
@jgalea
jgalea / change-metabox-order.php
Created July 13, 2013 12:09
How to change taxonomy metabox order in custom post types.
<?php
add_action('add_meta_boxes_wprss_feed', 'wprss_c_meta_box_order');
function wprss_c_meta_box_order() {
global $wp_meta_boxes;
$category = $wp_meta_boxes['wprss_feed']['side']['core']['wprss_categorydiv'];
unset( $wp_meta_boxes['wprss_feed']['side']['core']['wprss_categorydiv'] );
# We're hooking into: do_action('add_meta_boxes_' . $post_type, $post);
$wp_meta_boxes['wprss_feed']['side']['low'] = array( 'wprss_categorydiv' => $category ) + $wp_meta_boxes['wprss_feed']['side']['low'];
}
@jgalea
jgalea / genesis-byline.php
Created August 14, 2013 14:30
Use the last updated date rather than publish date on posts.
<?php
add_filter( 'genesis_post_info', 'custom_post_info' );
function custom_post_info( $post_info ) {
$u_time = get_the_time( 'U' );
$u_modified_time = get_the_modified_time( 'U' );
if ( $u_modified_time >= $u_time + 86400 ) {
$post_date = get_the_modified_time( 'F jS, Y' );
}
@jgalea
jgalea / custom-role.php
Last active January 31, 2020 10:47
Creating a custom role in WordPress
<?php
$result = add_role( 'advanced_contributor', 'Advanced Contributor', array(
'read' => true, // True allows that capability
'edit_posts' => true,
'delete_posts' => false, // Use false to explicitly deny
));
@jgalea
jgalea / new-post-type-capabilities.php
Last active May 15, 2020 02:01
Creating a new post type with capabilities.
<?php
add_action( 'init', 'create_my_post_types' );
function create_my_post_types() {
register_post_type(
'movie',
array(
'public' => true,
'capability_type' => 'movie',
'capabilities' => array(
@jgalea
jgalea / remove-billing.php
Last active June 9, 2020 19:58
Remove billing details from WooCommerce checkout.
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
unset($fields['billing']['billing_first_name']);
unset($fields['billing']['billing_last_name']);
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']);
@jgalea
jgalea / user-can.php
Created July 12, 2013 10:24
Check if a user has a capability or role.
<?php
// Checking for capability
if ( user_can( 6, 'read' ) ) {
// Do something
}
// Checking for role
if ( user_can( 6, 'administrator' ) ) {
// Do something
}
@jgalea
jgalea / microtime.php
Last active December 7, 2022 01:30
Test execution time of WordPress PHP function
<?php
function my_function() {
$start = microtime(true);
// function code here
$time_taken = microtime(true) - $start;
wp_die( $time_taken ); // in seconds
}