Skip to content

Instantly share code, notes, and snippets.

View kostiantyn-petlia's full-sized avatar

Kostiantyn Petlia kostiantyn-petlia

  • Ukraine
View GitHub Profile
@kostiantyn-petlia
kostiantyn-petlia / js_console_log()
Last active August 13, 2019 11:55
WordPress function for PHP debug output to JS console.log()
/**
* Output like the var_dump() in the JS console.log()
*/
if ( ! function_exists( 'js_console_log' ) ) {
function js_console_log( $x, $as_text = false ) {
echo '<div class="php-to-js-console-log" style="display: none!important;" data-as-text="' . esc_attr( (bool) $as_text ) . '" data-variable="' . htmlspecialchars( wp_json_encode( $x ) ) . '">' . htmlspecialchars( var_export( $x, true ) ) . '</div>';
}
if ( function_exists( 'js_console_log' ) ) {
add_action( 'wp_footer', function () {
@kostiantyn-petlia
kostiantyn-petlia / gist:1c71899bd7d68c0b388a77f58a06d593
Created April 23, 2019 13:53
WP/ACF - Load Google Map API if it needs
/**
* Load Google Map only if it needs (when we load ACF field type='google_map')
*/
add_action( 'acf/init', function () {
acf_update_setting( 'google_api_key', ( get_theme_mod( 'google_maps_api' ) ?: 'YoUrLiCeNsECoDePlAcEhErE' ) );
} );
add_action( 'wp_enqueue_scripts', function () {
wp_register_script( 'google-map', 'https://maps.googleapis.com/maps/api/js?key=' . acf_get_setting( 'google_api_key' ) . '&v=3.exp', null, false, true );
@kostiantyn-petlia
kostiantyn-petlia / sanitize_numbers.php
Created March 15, 2019 10:17
sanitize_numbers() & sanitize_absint()
/**
* Remove any chars except numbers
* '+1-012-123-45-678' => (string) '101212345678'
* 'user_15' => (string) '15'
*/
function sanitize_numbers( $string ) {
$string = is_numeric( $string ) ? (string) $string : $string;
return is_string( $string ) ? preg_replace( '/\D+/', '', $string ) : '';
}
@kostiantyn-petlia
kostiantyn-petlia / helpers-svg.php
Last active March 19, 2019 17:48
Functions for inline SVG with adding class (WP, ACF PRO Image)
//------------------------------------------------------------------------------
/**
* Convert file url to path
*
* @param string $url Link to file
*
* @return bool|mixed|string
*/
@kostiantyn-petlia
kostiantyn-petlia / tryParseJSON.js
Last active December 19, 2018 16:45
Save JSON parsing
// Save JSON parsing
function tryParseJSON(jsonString) {
try {
var o = JSON.parse(jsonString);
// Handle non-exception-throwing cases:
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
// but... JSON.parse(null) returns null, and typeof null === "object",
// so we must check for that, too. Thankfully, null is falsey, so this suffices:
if (o && typeof o === "object") {
return o;
@kostiantyn-petlia
kostiantyn-petlia / delete.php
Created November 27, 2018 13:49
Deleting all files from a folder using PHP
<?php
/**
* !!! BE CAREFULLY !!!
*
* Will be deleted all files in the current dir!
* Place this PHP-script file in the target folder & run like http://site.com/some-target-folder/delete.php
*
* Author: K
* Version: 2.0
*
<?php
/**
* Define & change the website WP_Roles
* Help: https://codex.wordpress.org/Roles_and_Capabilities
*/
class UserRoles {
/** Array of allowed roles (keys) with data array with 'label' text & 'caps' array.
* Set standard name and empty array for default WP role.
@kostiantyn-petlia
kostiantyn-petlia / helpers-write-log.php
Last active March 23, 2023 12:12
Logging function write_log() for debugging process
// -----------------------------------------------------------------------------
/**
* Logging to debug.log in DEBUG_LOG_DIR dir (default is the root site dir)
*
* Note: In the wp-config.php:
*
* you need define:
* 0) define( 'DEBUG_LOG', true )
*
@kostiantyn-petlia
kostiantyn-petlia / get_youtube_video_embed_url.php
Last active October 25, 2019 08:57
Youtube embed video URL with parameters or video ID from a common URL
/**
* Returns a Youtube video ID from URL
*
* @link https://regex101.com/
*
* @param string $url - target URL, like 'https://www.youtube.com/watch?v=C0DPdy98e4c' and similar
*
* @return bool|string - Youtube video ID, like 'C0DPdy98e4c', or false if no matches
*
* @author K
@kostiantyn-petlia
kostiantyn-petlia / max_int_divider.php
Last active October 4, 2017 13:02
Function: Maximum integer divider
// Find an maximum integer divider. Emxample: max_int_divider(18, 4) => 3; 18/3 = 6 (integer);
// Author - K
// Version 1.01
function max_int_divider( $value = null, $limit = null ) {
// Wrong params
if ( $value === null || ! is_int( $value ) || empty( $limit ) || ! is_int( $limit ) ) {
return false;
}