Skip to content

Instantly share code, notes, and snippets.

@eminozlem
eminozlem / gist:de8300cb21fca8a7373d
Last active August 29, 2015 14:19
compare 2 function performance
public static function benchmark($func1,$func2='',$no=100,$e=true,$f1args=null,$f2args=null) {
$exists1 = (is_array($func1)) ? method_exists($func1[0],$func1[1] ) : function_exists($func1);
$exists2 = (is_array($func2)) ? method_exists($func2[0],$func2[1]) : function_exists($func2);
if( ! $exists1 ) return false;
$result ='<pre>';
// Run the function(s) given no# of time.
$f1total = 0;
$f2total = 0;
$f1p = 0;
$f2p = 0;
@eminozlem
eminozlem / gist:84c07a9834aed2062f63
Last active August 29, 2015 14:27 — forked from nickkoskowski/gist:2fb00a84b2be7f91cb30
Get a random post link from WP database using query
<?php
function get_random_post_link() {
$args = array(
'orderby' => 'rand',
'posts_per_page' => '1',
);
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
@eminozlem
eminozlem / gist:ac046093f8522d0f8ef1
Last active September 22, 2015 14:43
Fastest way to get 1 post link / post ID
function eo_get_a_rand_postlink() {
global $wpdb;
$rand_post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY RAND() LIMIT 1");
return get_permalink($rand_post_id);
}
@eminozlem
eminozlem / gist:7cddc65af3a8a5a6e96d
Last active October 21, 2015 23:54
Automatically create custom taxonomy selects for custom post type
<?php $pobjtaxes = get_object_taxonomies( $tcptyp, 'object' );
foreach( $pobjtaxes as $pobjk => $pobjtax ) {
$ptxargs = array(
'orderby' => 'name',
'order' => 'ASC',
'fields' => 'id=>name'
);
$pterms = get_terms($pobjk, $ptxargs);
?>
<div class="form-group">
@eminozlem
eminozlem / gist:43b786a190b9ea08bfc4
Created October 23, 2015 02:11
WP_Query /w transients
function eo_del_qy_trans($post_id,$post,$update) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
global $wpdb;
$ptyp = $post->post_type;
// Find and delete all query transients involved in this post type
$trans_name = $ptyp . '_qy_res';
$like = '%' . $wpdb->esc_like( $trans_name ) . '%';
$qs = $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", $like );
$qy_trans_names = $wpdb->get_results( $qs, ARRAY_N );
foreach ( $qy_trans_names as $qyt ) {
<?php header("Content-type: text/html; charset=utf-8");
// require( 'xyz.php' );
// global $abc;
set_time_limit(20000);
// kaçar kaçar ?
$getc = 100;
$offset = (isset($_GET['offset'] ) ) ? intval( $_GET['offset'] ) : 0;
@eminozlem
eminozlem / gist:541afd138a9f37083057
Created February 1, 2016 20:02
color pairs - difference test
<?php
function coldiff($R1,$G1,$B1,$R2,$G2,$B2){
return max($R1,$R2) - min($R1,$R2) +
max($G1,$G2) - min($G1,$G2) +
max($B1,$B2) - min($B1,$B2);
}
function brghtdiff($R1,$G1,$B1,$R2,$G2,$B2){
$BR1 = (299 * $R1 + 587 * $G1 + 114 * $B1) / 1000;
$BR2 = (299 * $R2 + 587 * $G2 + 114 * $B2) / 1000;
@eminozlem
eminozlem / gist:2868111defb8bdb62ec2
Last active February 20, 2016 19:31
get cached results for wp_query where available
/*
** Function to retrieve cached query result from database - Esp. handy for complicated queries.
* Assumes both will produce same result based on $args, ie. if WP_Query has the exact same arguments with the query to be requested
* - Parameter - | - Default - | - Description -
* $args = array | WP_Query($args) | no default, required;
* $exp = int | 86400 (1 day) | freshness, get new results if stored result is older than given time period in seconds. |
* $q_uid = string | NULL | unique query_id, used as suffix . If a result transient with this id exists it will be retrieved.
* $strict = boolean | false | ignore all assumptions, do a strict check.
* $d = boolean | false | Debug, prints a debug message informing if its read from cache, or performed a wp_query like it normally would.
@eminozlem
eminozlem / gist:ae634968903436c508b93a1dc8c07049
Created March 31, 2016 12:53
Postcode zipcode not required woocommerce
function my_billing_postcode( $address_fields ) {
$address_fields['postcode']['required'] = false;
$address_fields['postcode']['label'] = "Posta Kodu";
return $address_fields;
}
add_filter( 'woocommerce_default_address_fields', 'my_billing_postcode' );
@eminozlem
eminozlem / gist:6f770712504d42ec5f51723b14724e6f
Created April 15, 2016 14:23
woocommerce better related items
<?php
/**
* Related Products
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) {