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 / render-once-useeffect.js
Created November 27, 2020 14:24
Focus an input control upon first render, using `useEffect` combined with the `useRef` hook
import React, { useEffect, useState, useRef } from "react";
import ReactDOM from "react-dom";
function App() {
// Store a reference to the input's DOM node
const inputRef = useRef();
const [value, setValue] = useState("");
useEffect(
() => {
@mahdiyazdani
mahdiyazdani / float-to-string.php
Created November 1, 2020 22:48
Convert a float to a string without locale formatting
<?php
/**
* Convert a float to a string without locale formatting which PHP adds when changing floats to strings.
*
* @param float $float Float value to format.
* @return string
*/
function prefix_float_to_string( $float ) {
if ( ! is_float( $float ) ) {
@mahdiyazdani
mahdiyazdani / string-to-array.php
Created November 1, 2020 22:47
Explode a string into an array by delimiter
<?php
/**
* Explode a string into an array by $delimiter and remove empty values.
*
* @param string $string String to convert.
* @param string $delimiter Delimiter, defaults to ','.
* @return array
*/
function prefix_string_to_array( $string, $delimiter = ',' ) {
@mahdiyazdani
mahdiyazdani / wpml-config.xml
Created November 1, 2020 17:09
WPML language configuration file
<wpml-config>
<custom-fields>
<custom-field action="copy">quantity</custom-field>
<custom-field action="translate">custom-title</custom-field>
</custom-fields>
<custom-types>
<custom-type translate="1">book</custom-type>
<custom-type translate="1">DVD</custom-type>
</custom-types>
<taxonomies>
@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',
},
{