Skip to content

Instantly share code, notes, and snippets.

View Inzman's full-sized avatar

Indrek Palm Inzman

  • Tartu, Estonia
View GitHub Profile
@Inzman
Inzman / gist:9b03b7ec114df236b65190611b4d72c1
Created August 16, 2018 05:02
Search multidimensional array by value
$key = array_search($item_id, array_column($array_name, 'id'));
____________________________
function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
@Inzman
Inzman / gist:59c66456f4373de04978536707996d5d
Created August 17, 2018 10:25
Cross-Origin Request Blocked
<FilesMatch "\.(php)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
@Inzman
Inzman / gist:ce52814df063e2b6abbbe53c05f944a7
Created August 22, 2018 07:13
To escape backslashes that cause problems for JSON data I use this function.
//escape backslash to avoid errors
var escapeJSON = function(str) {
return str.replace(/\\/g,'\\');
};
@Inzman
Inzman / gist:755283ea16ffe0f40a128ce73a6ac633
Created August 23, 2018 08:29
Remove key from array by value
$to_remove = array('john');
$result = array_diff($your_array_name, $to_remove);
@Inzman
Inzman / gist:c63baa0a7094925bdc2434d7d843c9e4
Created August 23, 2018 09:58
Determine if a WordPress post or page has children
function has_children() {
global $post;
return count( get_posts( array('post_parent' => $post->ID, 'post_type' => $post->post_type) ) );
}
if ( has_children() ) {
// do something if this item has children
}
@Inzman
Inzman / gist:8496d7e8613ddee6515572e46781b3e4
Created October 9, 2018 12:17
Remove any non-UTF8 characters
//reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ?
$some_string = preg_replace('/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'.
'|[\x00-\x7F][\x80-\xBF]+'.
'|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'.
'|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'.
'|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S',
'?', $some_string );
//reject overly long 3 byte sequences and UTF-16 surrogates and replace with ?
$some_string = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'.
@Inzman
Inzman / gist:bb76adca93fdc9b7cbfff04d1598c862
Created December 10, 2018 06:24
WordPress: save `get_template_part()` to variable
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
@Inzman
Inzman / gist:4bfdbd17a47a37b17396e58426a33690
Created March 14, 2019 07:31
Get element percent in viewport
_getPercentInView = function(element){
$element = $(element);
var pos = $element.offset(),
theViewport = {top:null, left:null, bottom:null, right:null, width:null, height:null},
theElement = {top:null, left:null, bottom:null, right:null, width:null, height:null},
elemLeft = pos.left,
elemTop = pos.top,
elemHeight = $element.height(),
elemWidth = $element.width();
@Inzman
Inzman / cachebuster
Created May 3, 2019 15:03
Cachebuster JS PHP
<script src="/js/panel/app.js?v={{ substr(md5(date("Y-m-d_Hi")),10,18) }}"></script>
@Inzman
Inzman / gist:dc6015d2381cad0075a16fc2e69eeb17
Created August 14, 2019 09:15
Wordpress - Manage Your Media Only in admin
add_filter( 'ajax_query_attachments_args', 'show_current_user_attachments' );
function show_current_user_attachments( $query ) {
$user_id = get_current_user_id();
if ( $user_id ) {
$query['author'] = $user_id;
}
return $query;
}