Skip to content

Instantly share code, notes, and snippets.

View proweb's full-sized avatar

Sergey Mochalov proweb

View GitHub Profile
@bobmonsour
bobmonsour / getDescription.js
Created April 13, 2023 04:45
An Eleventy filter that extracts the meta description from within the <head> element of a web page
// getDescription - given a url, this Eleventy filter extracts the meta
// description from within the <head> element of a web page using the cheerio
// library.
//
// The full html content of the page is fetched using the eleventy-fetch plugin.
// If you have a lot of links from which you want to extract descriptions, the
// initial build time will be slow. However, the plugin will cache the content
// for a duration of your choosing (in this example, it's set to 1 day).
//
// The description is extracted from the <meta> element with the name attribute
@aguilar1181
aguilar1181 / wp-preload.php
Last active February 11, 2023 20:50
add to WordPress themes functions.php to preload assets without a plugin
<?php
/**
* Preloading WordPress assets without a plugin.
* Change function name to something more unique.
* Priority is set to 0 to include as high in the <head> DOM as possible. Change priority if needed.
* Use WordPress is_page() to target specific page or is_front_page() to target ONLY homepage as in Option #1
* If asset needs to be preloaded globally use Option #2.
* For multiple assets clone the link tag instead of the entire echo block.
* Change 'as' attribute accordingly. Values for 'as' can be found here https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-as.
* Use abolute paths. For external assets paste the url, for internal assets use WordPress functions to construct URL.
@KevinBatdorf
KevinBatdorf / router.php
Last active December 26, 2025 12:01
A simple router for WordPress REST API - Laravel-like
<?php
defined( 'ABSPATH' ) or die;
namespace Kevin;
class Router extends \WP_REST_Controller
{
protected static $instance = null;
public function getHandler($namespace, $endpoint, $callback) {
\register_rest_route(
@KevinBatdorf
KevinBatdorf / uploadImage.ts
Last active December 26, 2025 12:01
Upload an image to the WordPress media library using the ImageData JavaScript object type
import apiFetch from '@wordpress/api-fetch'
// This is also set up to clone fields from an existing image.
// Just remove imageToClone and supply the missing fields on your own.
export const uploadImage = async (image: ImageData, imageToClone: WpImage): Promise<WpImage | undefined> => {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
const ctx = canvas.getContext('2d')
if (!ctx) return
import _ from 'lodash';
import PropTypes from 'prop-types';
import React, { Fragment } from 'react';
import { BlockControls } from '@wordpress/block-editor';
import { isUnmodifiedDefaultBlock } from '@wordpress/blocks';
import {
ToolbarButton,
ToolbarGroup,
} from '@wordpress/components';
@homu9
homu9 / gist:7d68b59aeb24c6f02455b776263041a2
Created April 13, 2022 19:54
Bulk update plugins for runcloud
#!/bin/bash
# Change directory to /home
cd /home
# Iterate through all users directories
for user in * ; do
# For every webapp under the user directory
# Count wp-config files to make sure its Wordpess
export const isValidJson = (str) => {
try {
JSON.parse(str)
return true
} catch (e) {
return false
}
}
@HardeepAsrani
HardeepAsrani / useSettings.js
Last active December 15, 2023 22:00
useSettings.js
/**
* WordPress dependencies.
*/
import api from '@wordpress/api';
import { __ } from '@wordpress/i18n';
import { dispatch } from '@wordpress/data';
import {
@taotiwordpress
taotiwordpress / twig-bag-of-tools.md
Last active February 11, 2026 17:18
Twig Cheatsheet #twig #timber #wordpress #cheatsheet #images #wp-query #wp_query #the_loop

Timber Bag of Tools Cheat Sheet

Useful Variables

Remember, there are 'TWIG' tools and then there are 'Timber' tools.

The second is WordPress specific.

Twig-based Variables

@HardeepAsrani
HardeepAsrani / useMeta.js
Last active December 15, 2023 22:00
useMeta.js
/**
* WordPress dependencies.
*/
import { useDispatch, useSelect } from '@wordpress/data';
/**
* useMeta Hook.
*
* useMeta hook to get/update WordPress' meta fields.
*