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:4b37ba375ec63d44ccacbcb5ffb64772
Created September 5, 2019 12:17
Wordpress - fix child CSS version cache
function _fix_child_css_version( $src ) {
$parts = explode( '?', $src );
if ( stristr( $parts[0], '-child/style.css' ) ) {
$child_ver = filemtime( get_stylesheet_directory() . '/style.css' );
return $parts[0] . '?v=' . $child_ver;
}
else {
return $src;
}
}
@Inzman
Inzman / gist:e8a5cb25fb949bcda26e5f091b3f9df7
Created September 5, 2019 08:15
Dump All Wordpress Custom Fields
<h3>All Post Meta</h3>
<?php $getPostCustom=get_post_custom(); // Get all the data ?>
<?php
foreach($getPostCustom as $name=>$value) {
echo "<strong>".$name."</strong>"." => ";
foreach($value as $nameAr=>$valueAr) {
@Inzman
Inzman / gist:d3b1126b965ecb5fe3d3fe8eba62227d
Created September 4, 2019 08:56
Disable Wordpress search
function disable_search( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
// to error
if ( $error == true )
$query->is_404 = true;
}
}
global $sitepress;
$trid = $sitepress->get_element_trid($post_id);
if($trid){
$translations = $sitepress->get_element_translations($trid);
// Unset current language
unset($translations[ICL_LANGUAGE_CODE]);
foreach($translations as $translation){
echo get_post_meta($translation->element_id, 'my_field', true);
}
@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;
}
@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: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 / 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: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: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
}