Skip to content

Instantly share code, notes, and snippets.

View Niq1982's full-sized avatar

Niku Hietanen Niq1982

View GitHub Profile
@Niq1982
Niq1982 / sparse-clone.md
Last active June 9, 2017 10:26
Clone only a part of a git project

Create empty repository and add origin without checkout:

mkdir <repo>
cd <repo>
git init
git remote add -f origin <url>

Set sparse checkout:

git config core.sparseCheckout true

@Niq1982
Niq1982 / responsive-divi.php
Created June 21, 2017 11:30
Make images added through divi (or other shortcode-based page builder) responsive in multisite
// Make DIVI images responsive by adding srcset
function make_divi_img_responsive( $output, $tag ) {
if ( 'et_pb_image' !== $tag ) {
return $output;
}
if ( ! preg_match_all( '/<img [^>]+>/', $output, $images ) ) {
return $output;
}
foreach ( $images as $image ) {
@Niq1982
Niq1982 / debug-translations
Created July 11, 2017 09:57
Find out what WordPress translation text domains an element is using
add_filter( 'gettext', 'debug_translations', 30, 3 );
function debug_translations( $translated_text, $text, $domain ) {
if ( $translated_text ) {
$output = $translated_text;
$output .= '<span style="font-size: 18px; color: white; line-height: 1; background-color: red; width: 24px; padding: 3px; border-radius: 18px;" title="text-domain: ';
$output .= $domain;
$output .= '">�</span>';
}
return $output;
}
@Niq1982
Niq1982 / footer.php
Last active April 16, 2021 13:08
Social icons functionality for WordPress
@Niq1982
Niq1982 / show_stock.php
Created May 21, 2021 08:28
Woocommerce: Show stock amounts for a product depending on the local and wholesale stock
<?php
/**
* Echo stock amounts for a product depending on the local and wholesale stock
* quantities and possibly add wholesale information.
*
* @param int $product_id Product ID (optional)
*/
function show_stock( $product_id = 0 ) {
$product_id = $product_id ? $product_id : get_the_ID();
@Niq1982
Niq1982 / katselmus.md
Last active May 21, 2021 09:06
WP/Woocommerce Koodikatselmus part 1

WP/Woocommerce Koodikatselmus part 1

Katselmus on tehty WP/Woofi -facebookryhmässä silmiin osuneesta koodista alkup. tekijän luvalla.

Lähtökohtainen tilanne/tarve:

Tulee kokoajan tilauksia, joita perutaan paljon koska ei pystytä pitämään kiinni toimitusajoista. yli 9000 tuotetta ei pystytä oikein manuaalisesti päivittämään käsin joten tein pienenpienen (36 riviä) function joka selkeyttää Woocommercen stock statusta huomattavasti.

Jos saldo on:

  • &lt; 0 = Tilaustuote alla lukee Nkpl jälkitoimituksessa
@Niq1982
Niq1982 / README.md
Last active September 16, 2021 14:13
Force reinstall everything with WP CLI

Use WP CLI to reinstall everything when there's a possibility that the site is hacked and could include some malicious code. Also be sure that you download/deploy fresh copies of plugins and themes that aren't hosted in the WordPress repositories

Download and install WP fresh core

Use --skip-content to not download default themes/plugins.

Dont do this if wp installation is any way customised, bedrock etc
wp core download --force --skip-content
@Niq1982
Niq1982 / cloudSettings
Last active September 27, 2021 08:49
Visual Studio Code Settings Sync Gist
{"lastUpload":"2021-09-27T08:49:19.837Z","extensionVersion":"v3.4.3"}
@Niq1982
Niq1982 / flywheel-local-xdebug-vscode.md
Last active December 17, 2022 09:33
WordPress debugging with XDebug on Flywheel Local & VSCode

Flywheel Local configuration

Flywheel Local has XDebug installed by default if you choose “Custom” instead of “Preferred” when setting up a new local environment. If you don’t know which your current site is running, you can detect it by going to ”Site Setup” tab. If you can change the PHP version there, you have the “Custom” environment running. If not, just export your site, import it back and choose “Custom”.

Now that we have the right environment, remember what PHP version you are running, open the right PHP settings file (for example /Local Sites/my_site/conf/php/7.0.3/php.ini) and add these lines in the [Xdebug] section:

xdebug.remote_enable=1
xdebug.remote_autostart=1

Save the php.ini and restart your site container in Flywheel to apply new settings.

@Niq1982
Niq1982 / example.php
Last active February 10, 2023 18:00
Add focal point to WordPress featured image with WP Smart Crop
<?php
// This requires the WP Smart Crop plugin
// Get the thumbnail ID
$thumbnail_id = get_post_thumbnail_id( get_the_ID() );
// Check if the crop is enabled on the thumbnail and get the dimensions
$crop_dimensions = get_post_meta( $thumbnail_id, '_wpsmartcrop_enabled', true ) ? get_post_meta( $thumbnail_id, '_wpsmartcrop_image_focus', true ) : [];
// Add percentage to dimensions and reverse array (top comes first in array)