Skip to content

Instantly share code, notes, and snippets.

View nadeem-khan's full-sized avatar

Nadeem Khan nadeem-khan

View GitHub Profile
@nadeem-khan
nadeem-khan / Scale smaller images to specified dimensions in wordpress
Last active August 29, 2015 14:10
Scale smaller images to specified dimensions in wordpress
function nk_thumbnail_upscale( $default, $orig_w, $orig_h, $new_w, $new_h, $crop ){
if ( !$crop ) return null; // let the wordpress default function handle this
$aspect_ratio = $orig_w / $orig_h;
$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
$crop_w = round($new_w / $size_ratio);
$crop_h = round($new_h / $size_ratio);
@nadeem-khan
nadeem-khan / gist:894c1929049545f7a489
Created October 28, 2014 14:28
Allow only alphanumeric characters in text input field with Jquery
$(function() {
$('#sidebar-filters-submit').click(function(e) {
$(".navigation").find(".error").remove();
allOk = true;
if ($("#search_page_keywords").val()) {
var input = $("#search_page_keywords").val();
if (/^[a-zA-Z0-9 ]*$/.test(input) === false) {
@nadeem-khan
nadeem-khan / script.md
Last active August 29, 2015 14:06
Script to create columns in CSV file based on values of two other CSV files
csvmain = CSV.read('data-cleaned.csv', headers:true);nil
csvsku = CSV.read('spree_products_sku.csv', headers:true)
new_rows = [csvmain.headers]
index = 0
index_parsed = 0
start_index = 0
indices = []
csvsku.each do |row|
not_found = true

index = start_index

This extends the built-in WordPress function get_the_ID() to return the post ID both inside and outside the loop.

Used outside the loop (in header.php):

<?php if ( function_exists( 'gt_hide_nav' ) && ! gt_hide_nav() ) : ?>
  <nav role="navigation">
    <?php if ( function_exists( 'bones_main_nav' ) ) bones_main_nav(); ?>
  </nav>

Share Counts

I have always struggled with getting all the various share buttons from Facebook, Twitter, Google Plus, Pinterest, etc to align correctly and to not look like a tacky explosion of buttons. Seeing a number of sites rolling their own share buttons with counts, for example The Next Web I decided to look into the various APIs on how to simply return the share count.

If you want to roll up all of these into a single jQuery plugin check out Sharrre

Many of these API calls and methods are undocumented, so anticipate that they will change in the future. Also, if you are planning on rolling these out across a site I would recommend creating a simple endpoint that periodically caches results from all of the APIs so that you are not overloading the services will requests.

Twitter

@nadeem-khan
nadeem-khan / order-emails.md
Last active August 29, 2015 14:05 — forked from mikejolley/gist:2263203
Add custom fields to Order email of Woocommerce

Add the field to order emails:

 add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');

 function my_woocommerce_email_order_meta_keys( $keys ) {
$keys['How did you hear about us?'] = 'hear_about_us';
return $keys;

}

@nadeem-khan
nadeem-khan / Create-Custom-Fields-in-Woocommerce-Checkout-Page.md
Last active July 21, 2022 19:16
Create Custom Fields in Woocommerce Checkout Page and Display them in Orders Details Page in Admin Area

Create Custom Fields in Woocommerce Checkout Page and Display them in Orders Details Page in Admin Area with this WordPress plugin:

<?php


/**
 * Plugin Name: Flowershop - WooCommerceCustomOverrides
 * Plugin URI: http://chillopedia.com
 * Description: Overrides WooCommerce Checkout Form Fields
  • Version: 1.0
@nadeem-khan
nadeem-khan / change-role-based-on-balance.md
Last active February 6, 2016 20:04 — forked from gabrielmerovingi/change-role-based-on-balance
Change WordPress user role based on Mycred Points

Change WordPress user role based on Mycred Points:

add_filter( 'mycred_add', 'check_for_role_change', 99, 3 );
 function check_for_role_change( $reply, $request, $mycred ) {
// Make sure that if any other filter has declined this we also decline
if ( $reply === false ) return $reply;

// Exclude admins
if ( user_can( $request['user_id'], 'manage_options' ) ) return $reply;
/**
* Custom myCRED Widget: This months leaderboard
* This widget will show this months leaderbaord with the option to set
* a title, the number of users to include and if it should be visible for
* non-members.
* @install Paste into your theme or child-themes functions.php file
* @author Gabriel S Merovingi
* @version 1.0
*/
@nadeem-khan
nadeem-khan / Rails-Commands.md
Last active November 6, 2017 12:48
Rails Commands

New Rails Project:

 rails new blog

Run Rails Console:

RAILS_ENV=production rails c  (restart console too whenever you make a change in the model)

View All Routes: