Skip to content

Instantly share code, notes, and snippets.

View Niq1982's full-sized avatar

Niku Hietanen Niq1982

View GitHub Profile
@Niq1982
Niq1982 / replace-original-image-with-scaled.php
Created July 17, 2024 10:44
Replace original images with scaled version if WordPress has deleted the originals
<?php
/**
* This script will copy the scaled images to the original image path in case the original
* image is missing. It will also regenerate the thumbnails for the original image.
*/
$ids = get_posts([
'post_type' => 'attachment',
'post_status' => 'any',
'posts_per_page' => -1,
@Niq1982
Niq1982 / README.md
Created February 26, 2024 09:11
Installing xdebug on ARM MacOS and VSCode

Install Xdebug

# Make sure you are using arm64 executable
> file `which php`
/opt/homebrew/bin/php: Mach-O 64-bit executable arm64

# Install
> arch -arm64 pecl install xdebug
@Niq1982
Niq1982 / card.php
Last active July 20, 2024 17:06
Load more to WordPress blog archive using AlpineJS and Axios
<div class="card">
<?php if ($args['link']) : ?>
<a href="<?php echo esc_url($args['link']); ?>" class="global-link">
</a>
<?php endif; ?>
<div class="card-image">
<?php wp_get_attachment_image($args['image']); ?>
</div>
@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 / 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 / footer.php
Last active April 16, 2021 13:08
Social icons functionality for WordPress
@Niq1982
Niq1982 / global.js
Last active July 28, 2023 11:32
Load Youtube videos using iFrame API
import ytPlayer from './ytplayer.js';
// Find players from DOM and load if found
const players = document.querySelectorAll('.youtube-player');
players.forEach(player => new ytPlayer(player, {
height: player.dataset.height ? player.dataset.height : 1280,
width: player.dataset.width ? player.dataset.width : 780,
autoplay: player.dataset.autoplay ? player.dataset.autoplay : false,
videoId: player.dataset.videoId ? player.dataset.videoId : false,
}));
@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)
@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"}