Skip to content

Instantly share code, notes, and snippets.

View lightningspirit's full-sized avatar
🏠
Working from home

Vitor Carvalho lightningspirit

🏠
Working from home
View GitHub Profile
@lightningspirit
lightningspirit / buildQueryString.ts
Created October 29, 2022 09:31
A function that takes an object and outputs a query string. Booleans are mapped into 1 and 0. Supports arrays of values.
type ValueType =
| string
| number
| boolean
| null
| undefined
| (string | number | boolean | null | undefined)[]
function mapBooleans(key: string, value: ValueType) {
switch (value) {
@lightningspirit
lightningspirit / README.md
Created September 20, 2022 00:37
Add excerpt to page

WordPress Page Excerpt

Add excerpt field to page

Usage

  1. Download this ZIP
  2. Upload to plugins
  3. Activate it
@lightningspirit
lightningspirit / README.md
Created September 20, 2022 00:28
Includes a `readingTime` field in seconds for every `Post` object response.

WPGraphQL Post Reading Time

Includes a readingTime field in seconds for every Post object response.

Usage

  1. Download this ZIP
  2. Upload to plugins
  3. Activate it
@lightningspirit
lightningspirit / useFSM.tsx
Created September 6, 2022 13:39
Preact Hook Finite State Machine
import { useCallback, useEffect, useMemo, useState } from "preact/hooks"
class StateTransitionError extends Error {}
function useFSM<T extends string>(
transitions: Record<T, T[]>,
options: {
throwOnTransitionError?: boolean
} = { throwOnTransitionError: true },
) {
@lightningspirit
lightningspirit / README.md
Created August 22, 2022 00:30
Includes a WPGraphQL JWT token in every posts preview link

WPGraphQL JWT in Preview Link

Includes a WPGraphQL JWT token in every posts preview link. That can be used for an external application (e.g. Next.js) to include in the authorization header when contacting back the WordPress instance using the WPGraphQL.

Usage

  1. Install both WPGraphQL and WPGraphQL JWT Authentication
  2. Download this ZIP
  3. Upload to plugins
  4. Activate it
@lightningspirit
lightningspirit / README.md
Last active April 21, 2024 08:47
Intercepts WordPress posts and terms changes and pings an external service

WordPress Ping Updates

This plugin intercepts WordPress posts and terms changes and pings an external service.

Usage

  1. Download this ZIP
  2. Add define('PING_UPDATES_URL', 'https://my-service.com/api'); to wp-config.php or ping_updates_url option in wp_options
  3. Optionally, add define('ENABLE_PING_UPDATES_ON_TERMS_POSTS', true); if you want to ping each associated post when a term is changed
  4. Add an option ping_updates_authz_token in wp_options where the value is the token to include in the Authorization header
  5. Upload to plugins of your WordPress
@lightningspirit
lightningspirit / README.md
Last active August 22, 2022 00:07
Fix WordPress REST base to use real URL

WordPress REST base on Site URL

If you change the site URL option to use a different one from your installed directory, the /wp-json/ is base changes too. This plugin fixes that.

Usage

  1. Download a ZIP of this plugin
  2. Upload to WordPress
@lightningspirit
lightningspirit / README.md
Last active June 22, 2022 18:50
WPGraphQL search by field with regular expressions support

WPGraphQL search by field with regular expressions support

Adds a where field to WPGraphQL content nodes (post, page and custom post types) to search by field using regexp or like. Supported fields are name, title, author, excerpt, status and mime_type.

Usage

  1. Install WPGraphQL
  2. Download or copy the wpgraphql-search-field.php file
  3. Place it under wp-content/mu-plugins
  4. Done
@lightningspirit
lightningspirit / README.md
Last active September 26, 2022 15:50
WPGraphQL add field format plain text

WPGraphQL add field format plain text

This small plugin adds a new format to retrieve excerpt or title in text plain format instead of rendered.

Usage

  1. Install WPGraphQL
  2. Download or copy the wpgraphql-field-format-text-plain.php file
  3. Place it under wp-content/mu-plugins
  4. Done
[{
"id": "year",
"type": "year",
"label": "Em que ano ficou desempregado/a",
"min": 2008
},
{
"id": "month",
"type": "select",
"label": "E em que mês",