Skip to content

Instantly share code, notes, and snippets.

@simonlk
simonlk / gist:164e25293343bc502dda
Created June 1, 2014 12:24
Add/Remove custom fields in WordPress profile
/* =====================================================
Custom profile field
===================================================== */
function modify_contact_methods($profile_fields) {
// Add new fields
$profile_fields['company_name'] = 'Company Name';
// Remove old fields
unset($profile_fields['aim']);
@simonlk
simonlk / wp login logout main menu
Created June 9, 2014 20:58
Add login/logout menu to WordPress main menu
/* =====================================================
Login/logout menu button
===================================================== */
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
if( $args->theme_location == 'primary-menu' ) {
ob_start();
wp_loginout(get_permalink());
$loginoutlink = ob_get_contents();
ob_end_clean();
@simonlk
simonlk / soliloquy-link-caption
Created June 11, 2014 08:22
Add link to Soliloquy caption
@simonlk
simonlk / woocommerce-deny-pobox-shipping
Created June 22, 2014 04:53
WooCommerce - Don't allow shipping to PO Box addresses
add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');
function deny_pobox_postcode( $posted ) {
global $woocommerce;
// Put postcode, address 1 and address 2 into an array
$check_address = array();
$check_address[] = isset( $posted['shipping_postcode'] ) ? $posted['shipping_postcode'] : $posted['billing_postcode'];
$check_address[] = isset( $posted['shipping_address_1'] ) ? $posted['shipping_address_1'] : $posted['billing_address_1'];
$check_address[] = isset( $posted['shipping_address_2'] ) ? $posted['shipping_address_2'] : $posted['billing_address_2'];
@simonlk
simonlk / wordpress menu shortcode
Created July 28, 2014 23:54
Create menu shortcode
/*==================================================
Create menu shortcodes - [menu name="menu-name"]
================================================= */
function sk_menu_content_shortcode( $atts, $content = null ) {
extract( shortcode_atts(array( 'name' => null, ), $atts ) );
return wp_nav_menu( array( 'menu' => $name, 'echo' => false ) );
}
add_shortcode( 'menu', 'sk_menu_content_shortcode' );
@simonlk
simonlk / Woocommerce - list of available product variations
Created October 28, 2012 04:53
Output Woocommerce product variations as a table so they look like a single product
<?php
// Output variations as a list so they look like a single product
// Works in theme/woocommerce/single-product/add-to-car/variable.php
?>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Ref #</th>
<?php foreach ( $attributes as $name => $options) :?>
<th><?php echo $woocommerce->attribute_label($name); ?></th>
@simonlk
simonlk / Wordpress - login with email
Created October 28, 2012 07:35
Allow user to login to Wordpress with their email address
// Allow users to login with their e-mail address
// source: http://www.benblanco.com
function login_with_email_address($username) {
$user = get_user_by('email',$username);
if(!empty($user->user_login))
$username = $user->user_login;
return $username;
}
add_action('wp_authenticate','login_with_email_address');
function change_username_wps_text($text){
@simonlk
simonlk / Shortcode for adding menu items with custom arguments and
Created October 31, 2012 02:46
Create short code for menu items with arguments like wp_nav_menu and allow short codes to be added to widgets
// Function that will return our Wordpress menu
// from http://www.cozmoslabs.com/1170-wp_nav_menu-shortcode/
function list_menu($atts, $content = null) {
extract(shortcode_atts(array(
'menu' => '',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
@simonlk
simonlk / Roots - add description to menu item output
Created November 5, 2012 04:51
Output menu item description within Roots theme menu item
// goes within nav.php before $output .= $item_html;
// output menu description
$item_html .= ' <span class="menu-desc">' . $item->description . '</span>';
@simonlk
simonlk / Woocommerce - hide other shipping methods when free shipping is available
Created November 27, 2012 04:41
Woocommerce - hide other shipping methods when free shipping is available
// Based on http://wcdocs.woothemes.com/snippets/hide-other-shipping-methods-when-free-shipping-is-available/
// Goes beyond by hiding the default internation shipping too
// Hide shipping options when free shipping is available
add_filter( 'woocommerce_available_shipping_methods', 'hide_standard_shipping_when_free_is_available' , 10, 1 );
/**
* Hide Standard Shipping option when free shipping is available
*
* @param array $available_methods
*/