Skip to content

Instantly share code, notes, and snippets.

View Tusko's full-sized avatar

Tusko Trush Tusko

View GitHub Profile
@Tusko
Tusko / functions.php
Created July 23, 2016 12:01
Woocommerce variation product minimum price
function wpa_variation_price_min( $price, $product ) {
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
// Sale Price
$prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
sort( $prices );
@Tusko
Tusko / functions.php
Last active April 18, 2020 16:59
Get archives of Custom Post Type
function get_cpt_archives( $cpt, $echo = false ) {
global $wpdb;
$sql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' GROUP BY YEAR({$wpdb->prefix}posts.post_date), MONTH({$wpdb->prefix}posts.post_date) ORDER BY {$wpdb->prefix}posts.post_date DESC", $cpt );
$results = $wpdb->get_results( $sql );
if ( $results ) {
$archive = array();
foreach ( $results as $r ) {
$year = date( 'Y', strtotime( $r->post_date ) );
$month = date( 'F', strtotime( $r->post_date ) );
@Tusko
Tusko / copy.js
Created August 9, 2016 17:05
protect buffer copy JS
//copyProtection
function doc_clipboard(e) {
'use strict';
var rows = window.getSelection().toString().split(/\n/), r, i, j;
for (r = 0; r < rows.length; r+=1) {
var words = rows[r].split(/\s/);
for (i = 0; i < words.length; i++) {
if (words[i].length > 6) {
words[i] = words[i].split("");
for (j = 2; j < words[i].length - 2; j += 2) {
@Tusko
Tusko / formatted-atts.php
Created August 18, 2016 06:16
Woocomemrce formatted product attributes
<?php
$formatted_attributes = array();
$attributes = $product->get_attributes();
foreach($attributes as $attr=>$attr_deets){
$attribute_label = wc_attribute_label($attr);
if ( isset( $attributes[ $attr ] ) || isset( $attributes[ 'pa_' . $attr ] ) ) {
$attribute = isset( $attributes[ $attr ] ) ? $attributes[ $attr ] : $attributes[ 'pa_' . $attr ];
if ( $attribute['is_taxonomy'] ) {
$formatted_attributes[$attribute_label] = implode( ', ', wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) ) );
} else {
@Tusko
Tusko / get_currentuserinfo.php
Created August 22, 2016 08:44
get_currentuserinfo _deprecated_function
<?php
function get_currentuserinfo() {
_deprecated_function( __FUNCTION__, '4.5', 'wp_get_current_user()' );
return wp_get_current_user();
}
@Tusko
Tusko / mmae.js
Last active October 13, 2016 13:58
mousemove animate element
(function($) {
var width = window.innerWidth,
height = window.innerHeight,
wrappers = $('main article'); //parent selector
$.each(wrappers, function(index, wrapper) {
var triggerEl = $(wrapper).find('.home_wrap > div'), //animate selector
validPrefix = "",
perspective = 1200;
@Tusko
Tusko / getAJAX.js
Created September 4, 2016 09:06
getAjax vanilla JS
function getAjax(api_url, callback, errorCallback) {
var createCORSRequest = function(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
@Tusko
Tusko / db_dump_all.sh
Created September 18, 2016 09:08
Backup (mysql dump) all your MySQL databases in separate files
#! /bin/bash
TIMESTAMP=$(date +"%F")
BACKUP_DIR="/backup/$TIMESTAMP"
MYSQL_USER="backup"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="password"
MYSQLDUMP=/usr/bin/mysqldump
mkdir -p "$BACKUP_DIR/mysql"
@Tusko
Tusko / tags_2_attachment.php
Created October 4, 2016 14:38
Add tags to post type Attachments
<?php
// apply tags to attachments
function wptp_add_tags_to_attachments() {
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'wptp_add_tags_to_attachments' );
@Tusko
Tusko / tmp.php
Last active October 28, 2016 12:23
instagram + FB timeline
<?php
/**
Instagram API
*/
function instagram_user_ID($username, $client_id, $token){
$ins_id = get_transient( 'instagram_userid' );
if ( false === $ins_id || empty($ins_id) ) {
$url = 'https://api.instagram.com/v1/users/search?q=' . $username . '&client_id=' . $client_id .'&access_token=' . $token;
$api = wp_remote_get( $url );
$data = json_decode($api['body']);