Skip to content

Instantly share code, notes, and snippets.

View otakupahp's full-sized avatar
🇪🇨
Latin Coder

Pablo Hernández (OtakuPahp) otakupahp

🇪🇨
Latin Coder
View GitHub Profile
@otakupahp
otakupahp / insert_element.php
Last active February 9, 2020 10:17
Insert an element before and after certain array key of an associative array
<?php
/**
* Insert an element before certain array key of an associative array.
* If no key is found,the element will be inserted at the end
*
* @since 1.0.0
*
* @param array $origin The original array where the element will be inserted
* @param mixed $element The element to insert
@otakupahp
otakupahp / .gitattributes
Last active February 16, 2021 21:51
Git attributes configuration for any new WP Plugin using PHPStorm
# fix EOL error for multiple environments for text files
*.* text eol=lf
# set image and fonts as binary
*.png binary
*.jpg binary
*.gif binary
*.pdf binary
*.eps binary
*.zip binary
@otakupahp
otakupahp / create_phar.php
Created February 13, 2020 15:37
Simple class to build a phar file from a given source directory
<?php
class BuildPhar
{
private $_sourceDirectory = null;
private $_stubFile = null;
private $_outputDirectory = null;
private $_pharFileName = null;
/**
@otakupahp
otakupahp / WP_Debug.php
Last active February 24, 2021 03:49
Enable WP Silent Debug mode
<?php
# Enable WP_DEBUG mode, logging to the /wp-content/debug.log file , without displaying on screen errors and warnings
define('WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
@otakupahp
otakupahp / jquery.js
Last active July 19, 2021 18:26
JQuery on ready
(function( $ ) {
'use strict';
// DOM ready
$(function() {
});
// Window loaded
$(window).on("load",function(){
@otakupahp
otakupahp / activate_plugin.php
Last active July 12, 2021 22:51
Simple function to check a plugin activation
<?php
/**
* @since 1.0.0
*
* @param $plugin_name
*/
public static function activate($plugin_name) {
# Initialize variables
@otakupahp
otakupahp / getQueryParam.js
Last active September 24, 2021 16:00
Work with query param from URL
/** Get a value from the Query params */
function otkGetQueryParam(name, url = window.location.href) {
let objectURL = new URL( url );
let searchParams = new URLSearchParams( objectURL.search );
return searchParams.get(name);
}
/** Set a value to the Query params */
function otkSetQueryParam(name, url = window.location) {
const urlObj = new URL(url);
@otakupahp
otakupahp / file.php
Last active February 21, 2021 15:57
WordPress Ajax done correctly
<?php
# After enquequing the JS add this localization to add the nonce and other variables
$params = [
'action' => 'your_action',
'wp_nonce' => wp_create_nonce( 'wp_ajax_nonce' ),
'error' => __('There was an error with the Ajax call. Please try again later'),
];
wp_localize_script( 'custom_ajax_params', 'wp_globals', $params );
@otakupahp
otakupahp / dynamic-shadow.js
Last active May 5, 2022 21:26
Add a shadow text with the desired content to a H2
// To add the text dynamically, use a class (overlay-title) in the h2 to catch the object and modify h2::before for "overlay-title-shadow"
(function( $ ) {
'use strict';
// Window loaded
$(window).on("load",function(){
const postTitleObj = $( '.overlay-title h2' );
if( 0 < postTitleObj.length ) {
postTitleObj.before( '<div class="overlay-title-shadow">' + postTitleObj.html() + '</div>' );
}
@otakupahp
otakupahp / autoloader.php
Last active August 24, 2022 10:09
Simple WordPress Autoloader
<?php
spl_autoload_register(function($required_file) {
# Transform file name from class based to file based
$fixed_name = strtolower( str_ireplace( '_', '-', $required_file ) );
$file_path = explode( '\\', $fixed_name );
$last_index = count( $file_path ) - 1;
$file_name = "class-{$file_path[$last_index]}.php";
# Get fully qualified path