Skip to content

Instantly share code, notes, and snippets.

View kierzniak's full-sized avatar

Piotr Kierzniewski kierzniak

View GitHub Profile
@kierzniak
kierzniak / functions.php
Last active February 26, 2019 19:29
Migrate passwords from old password hash algorithm to WordPress algorithm.
<?php
/**
* Migrate passwords from old password hash algorithm to WordPress algorithm.
*
* When your client has existing and working application with database of users
* and you want to import them to your brand new WordPress application, you can
* not import passwords because they are probably hashed using different algorithm.
*
* Solution for that is to save old user passwords in meta column `_old_password`
* and compare user typed password using old hashing algorithm with meta column
@kierzniak
kierzniak / functions.php
Last active February 5, 2020 09:57
Function to resolve assets url with attached content hash
<?php
/**
* Function to resolve assets url with attached content hash.
*
* Browser cache mechanism is used to store locally once downloaded assets. This
* improves website performance and saves network bandwidth. It may be also creating
* a problem when a user visits your website and do not see the newest changes in assets
* because the browser is serving an old file from cache.
*
@kierzniak
kierzniak / 01-plugin.php
Last active September 22, 2022 15:23
Simple dependency injection example in WordPress plugin/theme code
<?php
/**
* Plugin bootsrap file.
*
* @author Motivast motivast.com
* @copyright 2018 - present, Motivast
*
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GPL-2.0-or-later
*
@kierzniak
kierzniak / functions.php
Last active February 26, 2019 19:30
Remove assets from Swift booster to refresh merged css file
<?php
/**
* Remove assets from Swift booster to refresh merged css file
*
* Swift performance lite is keeping some local assets source in booster which
* prevent from refreshing e.g. css files which may change frequently like in
* Elemetor.
*
* @author Motivast motivast.com
@kierzniak
kierzniak / functions.php
Last active July 24, 2018 06:21
Get category related posts published before given post
<?php
/**
* Class provided to get category related posts published before given post
*
* @author Motivast motivast.com
* @copyright 2018 - present, Motivast
*
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GPL-2.0-or-later
*
@kierzniak
kierzniak / functions.php
Created June 21, 2018 11:34
Export WooCommerce orders to XML file
<?php
/**
* Export WooCommerce orders to xml file
*
* @return void|string Return nothing or xml file
*/
function motivast_export_xml_with_woocommerce_orders() {
/**
* Check for credentials or IP address to allow download
@kierzniak
kierzniak / functions.php
Last active June 15, 2018 10:57
Modify raw SQL created by WP Query to order posts by custom wp_posts table column
<?php
/**
* Make sample fake query to show working example
*/
function motivast_wp_query() {
new WP_Query(array(
/**
* This line will not modify your query but we can use it to
@kierzniak
kierzniak / functions.php
Last active May 25, 2018 15:15
Require basic authentication for all pages except home page
<?php
/**
* Require basic authentication for all pages except home page
*
* @return void
*/
function motivast_require_auth() {
define('AUTH_USER', 'admin');
@kierzniak
kierzniak / plugin.php
Last active May 11, 2018 07:35
Provide custom download link for password protected files
<?php
/**
* Provide custom download link for password protected files
*/
/**
* This probably should not be constant but for sake of example
* leave it like this;
*/
@kierzniak
kierzniak / functions.php
Created May 2, 2018 21:34
Sanitize filename on WordPress upload to not brake links with UTF-8 characters
<?php
/**
* Sanitize filename to not brake links with UTF-8 characters
*
* WordPress allow to upload files with names containing UTF-8 characters. Some
* browsers do not handle properly UTF-8 characters in url which causes 404 errors.
* This filter will remove UTF-8 characters from filename before saving it.
*
* @see https://core.trac.wordpress.org/ticket/22363 Bug request
*