Skip to content

Instantly share code, notes, and snippets.

View bfintal's full-sized avatar
🎯
Focusing

Benjamin Intal bfintal

🎯
Focusing
View GitHub Profile
@bfintal
bfintal / auto-block-recovery.js
Created September 29, 2021 12:54
Encountering lots of broken blocks / block errors in WordPress Gutenberg? Paste this code in your browser developer console while editing your post to recover all the blocks at once.
var recursivelyRecoverInvalidBlockList = blocks => {
const _blocks = [ ...blocks ]
let recoveryCalled = false
const recursivelyRecoverBlocks = willRecoverBlocks => {
willRecoverBlocks.forEach( _block => {
if ( isInvalid( _block ) ) {
recoveryCalled = true
const newBlock = recoverBlock( _block )
for ( const key in newBlock ) {
_block[ key ] = newBlock[ key ]
@bfintal
bfintal / query.sql
Created February 25, 2020 05:19
Remove duplicate entries in MySQL table when you have no unique IDs. Change `mytable` to the name of your table.
CREATE TABLE `temp` like `mytable`;
INSERT `temp` SELECT DISTINCT * FROM `mytable`;
DROP TABLE `mytable`;
ALTER TABLE `temp` RENAME `mytable`;
import { registerPlugin } from '@wordpress/plugins'
import { useEffect } from '@wordpress/element'
const doSomething = () => {
useEffect( () => {
// Do whatever when the block editor is initialized.
}, [] )
return null
}
import domReady from '@wordpress/dom-ready'
domReady( () => {
if ( window._wpLoadBlockEditor ) {
window._wpLoadBlockEditor.then( function() {
// Do whatever when the block editor is initialized.
} )
}
} )
add_action( 'wp_head', function () { ?>
<script>
window.addEventListener( 'scroll', function() {
const limit = document.body.classList.contains( 'home' ) ? 300 : 50
if ( ( window.pageYOffset || document.body.scrollTop ) > limit ) {
if ( ! document.body.classList.contains( 'is-scrolling' ) ) {
document.body.classList.add( 'is-scrolling' )
}
} else {
if ( document.body.classList.contains( 'is-scrolling' ) ) {
@bfintal
bfintal / functions.php
Created November 12, 2019 02:44
Stackable child theme
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( 'parent-style' ),
wp_get_theme()->get( 'Version' )
);
}
@bfintal
bfintal / functions.php
Created September 30, 2019 09:51
Add this in your theme's functions.php to disable blog post image size generation in Stackable
<?php
// Disable blog post image size generation in Stackable.
remove_action( 'after_setup_theme', 'stackable_blog_posts_image_sizes' );
?>
.ugb-image-upload-has-placeholder[data-is-placeholder-visible="true"] {
display: flex !important;
}
setTimeout( function() {
var ttes = document.querySelectorAll( '.tte_wrapper' );
Array.prototype.forEach.call( ttes, function( el ) {
var typeAttr = el.getAttribute( 'data-effect' );
// Make the element visible and remove original text.
// Original text is for SEO.
var mid = el.querySelector( '.tte_mid' );
mid.innerHTML = '';
mid.style.opacity = '';
@bfintal
bfintal / cheatsheet.js
Last active April 11, 2024 00:22
Gutenberg Cheat Sheet
// Get the data of a block
wp.data.select( 'core/block-editor' ).getBlocks()[0]
// Update attributes of another block
// wp.data.dispatch( 'core/editor' ).updateBlockAttributes( clientID, attributes )
wp.data.dispatch( 'core/block-editor' ).updateBlockAttributes( '10d88a6d-95d6-4e07-8293-5f59c83a26c0', { heading: 'New Heading' } )
// Get currently selected block.
wp.data.select( 'core/block-editor' ).getBlockSelectionStart()