Skip to content

Instantly share code, notes, and snippets.

View zgordon's full-sized avatar
🎓
Prepping a Course on Gatsby, GraphQL & WordPress

Zac Gordon zgordon

🎓
Prepping a Course on Gatsby, GraphQL & WordPress
View GitHub Profile
@zgordon
zgordon / register_sidebar-parameters.php
Last active January 7, 2024 05:16
Example of parameters for register_sidebar() function in WordPress
<?php
register_sidebar( [
'name' => esc_html__( 'Sidebar', 'themename' ),
'id' => 'main-sidebar',
'description' => esc_html__( 'Add widgets here.', 'themename' ),
'before_widget' => '<section class="widget">',
'class' => 'custom-widget-area',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
@zgordon
zgordon / gutenberg-color-pallete-theme.php
Last active January 7, 2024 05:16
Add theme support and set a color palette for all blocks
<?php
function mytheme_setup_theme_supported_features() {
add_theme_support( 'editor-color-palette',
'#556270',
'#4ECDC4',
'#C7F464',
'#FF6B6B',
'#C44D58',
);
@zgordon
zgordon / block-template.php
Last active January 7, 2024 05:16
Example of how to add block templates to post types in WordPress
<?php
function mytheme_block_templates( $args, $post_type ) {
// Only add template to 'post' post type
// Change for your post type: eg 'page', 'event', 'product'
if ( 'post' == $post_type ) {
// Optionally lock templates from further changes
// Change to 'insert' to allow adding other blocks, but lock defined blocks
@zgordon
zgordon / wp-nav-menu-parameters.php
Last active January 7, 2024 05:15
Demo of the possible menu parameters for wp_nav_menu
<?php
$args = [
// Location pickable in Customizer
'theme_location' => 'main-menu',
// Assigns a default menu to location
'menu' => 'Main Menu',
// Main wrapper around the ul of posts
'container' => 'div',
'container_class' => 'container-class',
'container_id' => 'container-id',
@zgordon
zgordon / enqueue-block-css.php
Last active October 11, 2023 13:50
This example shows how to use enqueue_block_assets to enqueue custom block CSS on the frontend of a theme and in the Gutenberg editor.
<?php
/**
* Enqueue block styles on frontend and in editor
*
*/
function mytheme_block_styles() {
wp_enqueue_style( 'mytheme-blocks', get_stylesheet_directory_uri() . '/css/blocks.css' );
@zgordon
zgordon / breadcrumbs-spacer-before.css
Created April 6, 2011 03:41
place a > before or after list items in your breadcrumbs
nav.breadcrumbs ul li::before {
content: ">";
margin-right: 10px;
}
/* Remove the > from before the first breadcrumb */
nav.breadcrumbs ul li:first-child::before {
content: "";
margin-left: 0;
}
@zgordon
zgordon / enqueue-block-js-and-css-theme.php
Last active September 27, 2022 09:27
Example code for a theme functions.php to enqueueing Gutenberg block CSS
<?php
/**
* Enqueue block editor CSS
*/
function jsforwpblocks_editor_scripts() {
// Make paths variables so we don't write em twice ;)
$editorStylePath = '/assets/css/blocks.editor.css';
@zgordon
zgordon / enqueue-block-js-and-css-plugin.php
Last active September 27, 2022 09:26
Example plugin file for enqueueing Gutenberg block JS and CSS
<?php
/**
* Enqueue block editor JavaScript and CSS
*/
function jsforwpblocks_editor_scripts() {
// Make paths variables so we don't write em twice ;)
$blockPath = '/assets/js/blocks.js';
$editorStylePath = '/assets/css/blocks.editor.css';
@zgordon
zgordon / webpack.config.js
Created September 14, 2018 16:16
A Gutenberg webpack configuration file
const path = require( 'path' );
const webpack = require( 'webpack' );
const ExtractTextPlugin = require( 'extract-text-webpack-plugin' );
// Set different CSS extraction for editor only and common block styles
const blocksCSSPlugin = new ExtractTextPlugin( {
filename: './assets/css/blocks.style.css',
} );
const editBlocksCSSPlugin = new ExtractTextPlugin( {
filename: './assets/css/blocks.editor.css',
@zgordon
zgordon / enqueue_block_editor_assets-theme-example.php
Last active January 3, 2022 04:06
Example of using enqueue_block_editor_assets in a WordPress Theme for hooking into the Guteneberg editor
<?php
/**
* Enqueue block-name main JavaScript file for the editor
*/
function js4wp_block_name_enqueue_scripts() {
// Make path a variable so we don't write it twice ;)
$blockPath = '/blocks/block-name/index.js';