Skip to content

Instantly share code, notes, and snippets.

View mahdiyazdani's full-sized avatar
:bowtie:
Still Rocking!

Mahdi Yazdani mahdiyazdani

:bowtie:
Still Rocking!
View GitHub Profile
@mahdiyazdani
mahdiyazdani / string-to-bool.php
Created October 26, 2020 13:53
Converting a string (e.g. 'yes' or 'no') to a boolean
<?php
/**
* Converts a string (e.g. 'yes' or 'no') to a bool.
*
* @since 1.0.0
* @param string $input String to convert.
* @return bool
*/
function prefix_string_to_bool( $input ) {
return is_bool( $input ) ? $input : ( 'yes' === $input || 1 === $input || 'true' === $input || 'TRUE' === $input || '1' === $input );
@mahdiyazdani
mahdiyazdani / get-next-array-key.php
Created October 26, 2020 13:52
Retrieve the next key in an array from given key
<?php
/**
* Retrieve the next key in an array from given key.
*
* @since 1.0.0
* @param array $arr List of items in a form of array.
* @param integer $key Key to search and find within the array.
* @return integer
*/
@mahdiyazdani
mahdiyazdani / wordpress-javascript-detection.php
Created October 21, 2020 12:53
Add a `js` class to the root `<html>` element when JavaScript is detected
<?php
/**
* Handles JavaScript detection.
* Adds a `js` class to the root `<html>` element when JavaScript is detected.
*
* @link https://github.com/WordPress/twentyseventeen/blob/master/functions.php#L239
* @return void
*/
function prefix_javascript_detection() {
echo "<script>(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);</script>\n";
@mahdiyazdani
mahdiyazdani / move-comment-field-to-bottom.php
Created October 21, 2020 12:50
Move the comment text field to the bottom
<?php
/**
* Move the comment text field to the bottom.
*
* @see https://developer.wordpress.org/reference/hooks/comment_form_fields/
* @param array $fields The comment fields.
* @return array
*/
function prefix_move_comment_field_to_bottom( $fields ) {
$comment_field = $fields['comment'];
@mahdiyazdani
mahdiyazdani / sanitize-html-classes.php
Created October 21, 2020 12:48
Sanitize multiple HTML classes in one pass
<?php
/**
* Sanitize multiple HTML classes in one pass.
*
* @param array $classes Classes to be sanitized.
* @param string $return_format The return format, 'input', 'string', or 'array'.
* @return array|string
*/
function prefix_sanitize_html_classes( $classes, $return_format = 'input' ) {
if ( 'input' === $return_format ) {
@mahdiyazdani
mahdiyazdani / fontawesome-array-of-objects.js
Created October 21, 2020 12:44
Array of objects - Fontawesome 5.13.0
const icons = [
{
className: 'fa fa-ad',
unicode: 'f641',
},
{
className: 'fa fa-address-book',
unicode: 'f2b9',
},
{
@mahdiyazdani
mahdiyazdani / woo-store-vacation-check.php
Created June 5, 2020 13:06
Determine whether the WooCommerce store is set to be closed or not
<?php
/**
* Determine whether the store is set to be closed or not!
* You can place PHP snippets at the bottom of your child theme `functions.php` file (before "?>" if you have it).
*
* @param object $wp_admin_bar
* @return void
*/
function prefix_is_shop_closed( $wp_admin_bar ) {
// Bail early, in case the plugin is not activated.
@mahdiyazdani
mahdiyazdani / remove-ds-store.shell
Created November 30, 2019 08:46
Recursively remove .DS_Store
$ cd to/your/directory
$ find . -name '.DS_Store' -type f -delete
@mahdiyazdani
mahdiyazdani / get-user-country-ip.txt
Created November 29, 2019 15:34
Get user’s IP and country code
http://ip-api.com/json/?fields=status,country,countryCode,query
@mahdiyazdani
mahdiyazdani / remove-storefront-copyright.php
Created October 2, 2019 19:14
Remove `Built with Storefront & WooCommerce` from Storefront theme footer