Skip to content

Instantly share code, notes, and snippets.

View alinademi's full-sized avatar

Ali Demi alinademi

  • Vancouver
View GitHub Profile
<?php
/**
* A function that takes an array of HTML attribute
* key/value pairs and returns a string of HTML attributes.
**/
function unwrap_html_attrs($attrs)
{
$attributes = '';
@lgladdy
lgladdy / acf-shortcode-query-loop-support.php
Last active December 11, 2023 19:54
This code sample enables support for the ACF shortcode inside a query loop by overriding the default shortcode block renderer with one which will inject the correct Post ID into the shortcode parameters
<?php
add_filter( 'render_block_core/shortcode', 'test_acf_shortcode_render', 10, 3);
function test_acf_shortcode_render( $content, $parsed_block, $block ) {
$content = preg_replace_callback( '/\[acf\s.*?\]/', 'acf_inject_query_loop_post_ID', $content );
return $content;
}
function acf_inject_query_loop_post_ID( $match ) {
return str_replace( '[acf', '[acf post_id="' . get_the_ID() . '"', $match[0] );
{
"title": "JSON schema for WordPress blocks",
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"//": {
"reference": "https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/",
"attributesReference": "https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/",
"contextReference": "https://developer.wordpress.org/block-editor/reference-guides/block-api/block-context/",
"supportsReference": "https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/",
"registerReference": "https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/#example-optional"
@joseph-farruggio
joseph-farruggio / get_parent_block_field.php
Last active January 22, 2023 23:12
Returns a specified field from the parent ACF block.
<?php
/**
* Returns a specified ACF field from a parent block
* @param string $child_id The ID of the child ACF block
* @param string $child_name The name of the child ACF block
* @param string $parent_name The name of the parent ACF block
* @param string $field The name of the parent's custom field to return
* @param boolean $return_first If $field is an array, optionally return the first item.
*/
function get_field_from_parent($child_id, $child_name, $parent_name, $field, $return_first = false) {
@kingkool68
kingkool68 / simple-google-apis.php
Created January 20, 2022 22:05
Interacting with Google API's using their PHP libraries is a nightmare. Skip the headache by authorizing a Guzzle client and make raw HTTP requests instead!
<?php
$key_file_path = __DIR__ . '/service-account-credentials.json';
$google_client = new \Google_Client();
$google_client->setAuthConfig( $key_file_path );
// Set the scopes of whatever you need access to
// See https://developers.google.com/identity/protocols/oauth2/scopes
$google_client->setScopes( array( 'https://www.googleapis.com/auth/analytics.readonly' ) );
$http_client = $client->authorize();
@webdevsuperfast
webdevsuperfast / zsh.md
Last active July 26, 2024 15:53
Install ZSH using Homebrew and set as default shell in Linux/WSL

Install ZSH via HomeBrew

brew install zsh

Add ZSH to /etc/shells

echo $(which zsh) | sudo tee -a /etc/shells

Set ZSH as default user shell

sudo chsh -s $(which zsh) $USER

Execute Shell or just restart terminal to take effect

@yratof
yratof / scripts.php
Created September 1, 2021 12:08
Add scripts to customizer for wordpress
<?php
/* Customiser script */
add_action( 'customize_register', 'custom_editor' );
function custom_editor( $wp_customize ) {
// Analytics section
$wp_customize->add_section('analytics_section', array(
'title' => __( 'Analytics', 'tuesday' ),
'description' => __( 'Enable tracking and analytics by placing your script tags in the correct location. <small><strong>Note:</strong> All scripts must be self-containing &lt;script&gt;&lt;/script&gt;, otherwise they will just print the code onto the website.</small>', 'tuesday' ),

Setting Up WordPress Coding Standards with VSCode

Install WordPress Coding Standards (WPCS)

First, make sure you have Composer installed in your system.

In order to use the WPCS you'll need the PHP_CodeSniffer (PHPCS) and the WPCS rules (sniffs) installed.

You can install PHP_CodeSniffer globally using Composer by running this command:

@pdclark
pdclark / pd-blocks-example-oop.php
Last active July 15, 2023 23:26
Short example OOP PHP-rendered WordPress blocks with attributes.
<?php
/**
* Plugin Name: PD Blocks — Example OOP with attributes
* Description: Single-file OOP PHP-rendered WordPress blocks with 5 example blocks.
* Author: Paul David Clark
* Author URI: https://pd.cm
* Plugin URI: https://pd.cm/oop-blocks
* Version: 30
*
* @package pd
@gziolo
gziolo / dev-note-5.8-block-editor.md
Last active March 7, 2023 00:50
Block Editor filter changes in WordPress 5.8

Block Editor Changes

We have reached the first WordPress core release where the post editor is no longer the only screen that uses the block editor. In the middle of the development process, we found out that several WordPress hooks defined on the server depended on the $post object that isn’t present on the updated screen that lets users edit widgets using blocks. Therefore, we decided to deprecate some of the existing filters and introduce their context-aware replacements. This way, we ensure that we can continue iteratively enabling the block-based paradigm on different screens like the navigation editor screen by leveraging the new WP_Block_Editor_Context class that will receive more capabilities over time. There are also new methods introduced that allow code reuse for the functionality that needs to be shared between the screen that uses the block editor.

Related Trac ticket: #52920.

Class

WP_Block_Editor_Context