Skip to content

Instantly share code, notes, and snippets.

View Inzman's full-sized avatar

Indrek Palm Inzman

  • Tartu, Estonia
View GitHub Profile
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: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;
}
}
@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: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:8abd6ee4656f0fc4068448c194f08190
Last active September 10, 2019 12:19
Get Wordpress post terms
// wp_get_object_terms() always queries the database
/* If you’re looping over WP_Query results, you should prefer get_the_terms() instead.
It’s pretty much the same for most use cases, but it uses the object cache,
which by default gets populated with the terms for the posts matching your query — unless you specifically
set update_post_term_cache as false when instantiating WP_Query.
*/
// Get terms for post
if ( ! function_exists( 'storefront_post_terms' ) ) {
function storefront_post_terms(){
@Inzman
Inzman / gist:97a1ab062b97e4c487674043613f485b
Created September 20, 2019 06:17
Woocommerce get variation product price in select
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( empty( $term ) ) return $term;
if ( empty( $product->id ) ) return $term;
$id = $product->get_id();
@Inzman
Inzman / gist:b41a53072ec41eda091cca7ab7327ada
Created October 17, 2019 13:37
Hierarchical terms list
/**
* Recursively sort an array of taxonomy terms hierarchically. Child categories will be
* placed under a 'children' member of their parent term.
* @param Array $cats taxonomy term objects to sort
* @param integer $parentId the current parent ID to put them in
*/
function sort_terms_hierarchicaly(Array $cats, $parentId = 0)
{
$into = [];
foreach ($cats as $i => $cat) {
@Inzman
Inzman / gist:00983bd2385710767b17e04716195663
Created November 20, 2019 12:22
This code rewrites the slug of the attachment upon uploading to add a prefix (or suffix) to the slug. In that case you have less chance of getting in the way of page slugs in the future.
add_action('add_attachment', function($postId) {
// get the attachment post object
$attachment = get_post($postId);
// get the slug for the attachment
$slug = $attachment->post_name;
// update the post data of the attachment with an edited slug
wp_update_post(array(
'ID' => $postId,
'post_name' => 'media-'.$slug, //adds a prefix
//'post_name' => $slug.'-photo', //adds a suffix
@Inzman
Inzman / gist:b515afaff3dbe55d52d2ef5ba38fe99a
Created January 8, 2020 07:58
Get last segment path in URL
function getLastPathSegment($url) {
$path = parse_url($url, PHP_URL_PATH); // to get the path from a whole URL
$pathTrimmed = trim($path, '/'); // normalise with no leading or trailing slash
$pathTokens = explode('/', $pathTrimmed); // get segments delimited by a slash
if (substr($path, -1) !== '/') {
array_pop($pathTokens);
}
return end($pathTokens); // get the last segment
}
@Inzman
Inzman / gist:781c6496b1167df8b567db7cadf61fc8
Created January 9, 2020 15:25
Wordpress - Delete auto-drafts
if (is_admin()){
if ( !get_option('db_last_auto_cnl') || (get_option('db_last_auto_cnl') < time()- 86400) ) {
global $wpdb;
$del= $wpdb->query("DELETE FROM $wpdb->posts WHERE post_status = 'auto-draft'");
$del= $wpdb->query("DELETE FROM $wpdb->posts WHERE post_type = 'revision'");
$del= $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timeout_rss%'");
$del= $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_rss_%'");
$del= $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_timeout_feed_%'");
$del= $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_feed_%'");
//if you want, you can uncomment these too