Archive by Author: Tom

Add screen to WooCommerce screen IDs

add_filter( 'woocommerce_screen_ids','wpcb_add_screen_to_wc_screen_ids' );
if ( ! function_exists( 'wpcb_add_screen_to_wc_screen_ids' ) ) {
    /**
     * wpcb_add_screen_to_wc_screen_ids.
     *
     * @see https://wpcodebook.com/snippets/add-screen-to-woocommerce-screen-ids/
     */
    function wpcb_add_screen_to_wc_screen_ids( $screen_ids ) {
        $screen_ids[] = 'your-screen-id'; // to get your screen ID call `$your_screen = get_current_screen()`, then get `$your_screen->id`
        return $screen_ids;
    }
}

Add product min, max and step quantity columns to admin products list in WooCommerce

add_filter( 'manage_edit-product_columns', 'wpcb_add_product_qty_columns' );
if ( ! function_exists( 'wpcb_add_product_qty_columns' ) ) {
    /**
     * Add qty columns to products list.
     */
    function wpcb_add_product_qty_columns( $columns ) {
        $columns['wpcb_min_qty']  = 'Min Qty';
        $columns['wpcb_max_qty']  = 'Max Qty';
        $columns['wpcb_qty_step'] = 'Qty Step';
        return $columns;
    }
}

add_action( 'manage_product_posts_custom_column', 'wpcb_render_product_qty_columns' );
if ( ! function_exists( 'wpcb_render_product_qty_columns' ) ) {
    /**
     * Output qty columns.
     */
    function wpcb_render_product_qty_columns( $column ) {
        switch ( $column ) {
            case 'wpcb_min_qty':
                echo apply_filters( 'woocommerce_quantity_input_min',  1, wc_get_product() );
                break;
            case 'wpcb_max_qty':
                echo apply_filters( 'woocommerce_quantity_input_max',  1, wc_get_product() );
                break;
            case 'wpcb_qty_step':
                echo apply_filters( 'woocommerce_quantity_input_step', 1, wc_get_product() );
                break;
        }
    }
}

Add Bcc to all emails in WordPress

add_filter( 'wp_mail', 'wpcb_add_bcc_to_all_emails' );
if ( ! function_exists( 'wpcb_add_bcc_to_all_emails' ) ) {
    /**
     * wpcb_add_bcc_to_all_emails.
     *
     * @see https://wpcodebook.com/snippets/add-bcc-to-all-emails-in-wordpress/
     */
    function wpcb_add_bcc_to_all_emails( $atts ) {
        if ( ! isset( $atts['headers'] ) ) {
            $atts['headers'] = '';
        }
        $atts['headers'] .= 'Bcc: ' . 'email@example.com' . "\r\n";
        return $atts;
    }
}

Get current user roles in WordPress

if ( ! function_exists( 'wpcb_get_current_user_roles' ) ) {
    /**
     * Get current user roles.
     *
     * @see    https://wpcodebook.com/snippets/get-current-user-roles-in-wordpress/
     * @return array
     */
    function wpcb_get_current_user_roles() {
        $current_user = wp_get_current_user();
        return $current_user->roles;
    }
}