Skip to content

Instantly share code, notes, and snippets.

@sanraul
sanraul / smpl.page.js
Created September 16, 2021 02:51
Use JS fetch to load an entire page
/**
* In page loader:
*
* <script src="http://localhost/assets/js/smpl.page.js?page=http://localhost/b93e8d"></script>
*/
var pageURL = document.currentScript.src.match(/\?page=(.*)/)?.[1];
if(pageURL) {
fetch(pageURL)
.then(function (data) {
@sanraul
sanraul / no-cache.php
Created September 12, 2021 19:59
Prevent PHP response from being cached
<?php
# see: https://stackoverflow.com/questions/13640109/how-to-prevent-browser-cache-for-php-site
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Connection: close");
@sanraul
sanraul / hijacking_method.js
Last active March 14, 2022 18:09
Inspect XMLHttpRequest data (sent and received)
Array.prototype.count = function() {
console.log('count (1) ' + this.length);
}
Array.prototype.count = (function() {
var original = Array.prototype.count;
return function () {
console.log('count (2) ');
return original.apply(this, arguments);
@sanraul
sanraul / demo.css
Created August 21, 2021 15:02
Rotating element around item
.container {
width: 300px;
height: 300px;
background-color: #eee;
margin: 100px;
position: relative;
}
.fs-1 {
font-size: 48px
}
@sanraul
sanraul / day.class.js
Last active July 9, 2021 15:20
Day class to allow setting dates such us "tomorrow", "yesterday" or "move" by days from today.
class Day extends Date {
tomorrow() {
this.move(1)
return this;
}
yesterday() {
this.move(-1)
return this;
}
@sanraul
sanraul / bs5-breakpoint-detect-and-show.html
Created June 16, 2021 14:08
Display the current bootstrap break point
<div class="position-absolute d-inline-block top-0 start-0 bg-primary text-white px-3 py-1 m-2 rounded-pill">
<p class="d-sm-none mb-0">mobile</p>
<p class="d-none d-sm-block d-md-none mb-0">sm</p>
<p class="d-none d-md-block d-lg-none mb-0">md</p>
<p class="d-none d-lg-block d-xl-none mb-0">lg</p>
<p class="d-none d-xl-block d-xxl-none mb-0">xl</p>
<p class="d-none d-xxl-block mb-0">xxl</p>
</div>
@sanraul
sanraul / add_admin_bar_menu.php
Created May 28, 2021 21:34
Add custom menu item and submenu to the admin bar in Wordpress.
<?php
add_action('admin_bar_menu', 'add_toolbar_items', 100);
function add_toolbar_items($admin_bar)
{
$admin_bar->add_menu(array(
'id' => 'my-item',
'title' => 'My Item',
'href' => '/path/to/page/',
@sanraul
sanraul / README.md
Last active May 2, 2021 20:00
Borrow Wordpress hooks (add_filter, add_action, apply_filters...). When implementing this code, you should be able to use Wordpress hooks functionality in your own application.

Wordpress Hooks as Standalone

Borrow Wordpress hooks (add_filter, add_action, apply_filters...). When implementing this code, you should be able to use Wordpress hooks functionality in your own application.

Usage

  1. Add the files to your project
  2. Create filters or actions the same way you doing in Wordpress
  3. Apply filters or execute actions.
@sanraul
sanraul / wp_get_all_menus.php
Last active April 28, 2021 18:14
Get Wordpress menu data. You can get a menu by ID, name, slug of you can get all menus in an array.
<?php
/**
* Get all menus.
*/
function generate_menu_data( /* $options=null */ ) {
$data = [];
$menus = wp_get_nav_menus();
/*
* object(WP_Term)#1124 (10) {
* ["term_id"]=>
@sanraul
sanraul / wp-hide-install-plugin.php
Created April 23, 2021 19:10
How to Hide Specific plugin from installed plugin list n Wordpress
<?php
/**
* How to Hide Specific plugin from installed plugin list n Wordpress.
*
* @see https://webcusp.com/how-to-hide-specific-plugin-from-installed-plugin-list/
*/
function appc_secret_plugins() {
global $wp_list_table;