Skip to content

Instantly share code, notes, and snippets.

View pbrocks's full-sized avatar

Paul Barthmaier pbrocks

View GitHub Profile
@pbrocks
pbrocks / block.js
Last active May 8, 2021 19:14
A little more explanation about how to build a WP block with php.
/**
* Before registering the block in JavaScript, we want
* to deconstruct the necessary variables.
*/
const el = wp.element.createElement,
registerBlockType = wp.blocks.registerBlockType,
ServerSideRender = wp.components.ServerSideRender,
TextControl = wp.components.TextControl,
TextareaControl = wp.components.TextareaControl,
InspectorControls = wp.editor.InspectorControls;
@pbrocks
pbrocks / build-a-block-with-php.php
Last active April 27, 2019 14:55
Code to needed to build a WP block, at least the php portion. Good way to convert WP Shortcodes
<?php
defined( 'ABSPATH' ) || die( 'File cannot be accessed directly' );
function built_with_php_init() {
// Register our block editor script.
wp_enqueue_script(
'built-with-php',
plugins_url( 'block.js', __FILE__ ),
array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor' )
@pbrocks
pbrocks / wclancpa-2019-block-php-frontend.js
Created April 27, 2019 14:47
We can use PHP to deliver content on the frontend, but still need JS to create the block. Note that Save: method is null because PHP is delivering the content on the frontend.
/**
* Before registering the block in JavaScript, we want
* to deconstruct the necessary variables.
*/
const el = wp.element.createElement,
registerBlockType = wp.blocks.registerBlockType,
ServerSideRender = wp.components.ServerSideRender,
TextControl = wp.components.TextControl,
TextareaControl = wp.components.TextareaControl,
InspectorControls = wp.editor.InspectorControls;
@pbrocks
pbrocks / wclancpa-2019-block-php-frontend.js
Created April 27, 2019 14:46
We can use PHP to deliver content on the frontend, but still need JS to create the block. Note that Save
/**
* Before registering the block in JavaScript, we want
* to deconstruct the necessary variables.
*/
const el = wp.element.createElement,
registerBlockType = wp.blocks.registerBlockType,
ServerSideRender = wp.components.ServerSideRender,
TextControl = wp.components.TextControl,
TextareaControl = wp.components.TextareaControl,
InspectorControls = wp.editor.InspectorControls;
@pbrocks
pbrocks / wclancpa-2019-block-access-filter.php
Created April 27, 2019 14:13
Restrict Access filters for WordPress Blocks
<?php
function my_restrict_post_filter( $use_block_editor, $post ) {
$author = get_userdata( $post->post_author );
if ( 'pento' === $author->user_login ) {
return (bool) random_int( 0, 1 );
}
return $use_block_editor;
}
@pbrocks
pbrocks / wclancpa-2019-block-categories.js
Created April 27, 2019 11:44
Code snippet to extend Category Block in WP Editor
// svg code
import lancpaIcon from './lancpaIcon';
// alter the icon slot
wp.blocks.updateCategory( 'wclancpa-2019', {
icon: lancpaIcon
} );
@pbrocks
pbrocks / higher-order-component.js
Created April 27, 2019 11:34
From the Handbook, creating a higher order component is a best practice for the WordPress abstraction layer of React.
const { createHigherOrderComponent } = wp.compose;
const withClientIdClassName = createHigherOrderComponent( ( BlockListBlock ) => {
return ( props ) => {
return <BlockListBlock { ...props } className={ "block-" + props.clientId } />;
};
}, 'withClientIdClassName' );
wp.hooks.addFilter( 'editor.BlockListBlock', 'my-plugin/with-client-id-class-name', withClientIdClassName );
@pbrocks
pbrocks / wclancpa-2019-block-categories.php
Created April 27, 2019 02:26
PHP snippet to add a panel to the WP Block Inserter.
<?php
/**
* Adding a block category creates a Panel
*/
function create_wclancpa_2019_panel( $categories, $post ) {
return array_merge(
$categories,
array(
array(
'slug' => 'wclancpa-2019',
@pbrocks
pbrocks / wclancpa-2019-imageblockstyle.js
Last active November 17, 2021 20:22
The core image block in WordPress does not ship with its own style, but you can easily add styles with the Block Style filter, thereby enabling you to target the block with custom CSS or JS. Here I am adding two, but only one will receive any treatment.
wp.blocks.registerBlockStyle( 'core/image', {
name: 'phader',
label: 'Philly Phader'
} );
@pbrocks
pbrocks / redirect-to-login-if-not-logged-in.php
Created December 24, 2018 02:19
Redirect to login if not logged in or if not PMPro membership page.
<?php
/**
* Redirect to login or homepage if user is logged out or not a member
* Add this code to your active theme's functions.php file.
*/
function my_template_redirect() {
global $current_user;
$okay_pages = array(
pmpro_getOption( 'billing_page_id' ),