Notes about WordPress Gutenberg themes development.

  • ACF field filter query

    Read more

    Order by ACF field

    $the_query = new WP_Query(array(
        'post_type'         => 'event',
        'posts_per_page'    => -1,
        'meta_key'          => 'featured',
        'orderby'           => 'meta_value',
        'order'             => 'DESC'
    ));

    Filter by ACF field

    $posts = get_posts(array(
        'posts_per_page'    => -1,
        'post_type'     => 'post',
        'meta_query'    => array(
            'relation'      => 'AND',
            array(
                'key'       => 'color',
                'value'     => array('red', 'orange'),
                'compare'   => 'IN',
            ),
            array(
                'key'       => 'featured',
                'value'     => '1',
                'compare'   => '=',
            ),
        ),
    ));

    Sources:

    • https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
    • https://www.advancedcustomfields.com/resources/order-posts-by-custom-fields/
  • Adding an option page

    Read more

    With ACF:

    add_action( 'acf/init', 'dev__option_page' );
    
    function dev__option_page(){
    
    	acf_add_options_page( array(
    		'page_title' 	=> 'Transfert Ressources',
    		'menu_title'	=> 'Transfert de Ressources',
    		'icon_url'		=> 'dashicons-update',
    		'menu_slug' 	=> 'ator',
    		'capability'	=> 'edit_posts',
    		'redirect'		=> false
    	));
    
    }

    See: https://www.advancedcustomfields.com/resources/acf_add_options_page/

    Without ACF: https://codex.wordpress.org/Creating_Options_Pages

  • block supports properties

    Read more

    To use the supports properties, first enable them in the block.json.

    {
    	"$schema": "https://schemas.wp.org/trunk/block.json",
    	"apiVersion": 3,
    	
     ...
    
    	"supports": {
    		"color": {
    			"text": true,
    			"background": true
    		}
    	},
    	"acf": {
    		"mode": "preview",
    		"renderTemplate": "./render.php"
    	}
    }

    Then in render.php :

    <?php
    
    $classes = array();
    
    if ( ! empty( $block['textColor'] ) ) {
    //	$classes = array_merge( $classes, explode( ' ', $block['className'] ) );
    	$classes[] = 'has-text-color';
    	$classes[] = 'has-' . $block['textColor'] . '-color';
    }
    
    echo '<div class="' . esc_attr( join( ' ', $classes ) ) . '"></div>';

    Or simply use the function get_block_wrapper_attributes( string[] $extra_attributes = array() ): string for e.g. to get the classes :

    preg_match( '/class="(.*?)"/', get_block_wrapper_attributes(), $matches);
    classes = $matches[1];

    Sources:

  • update_field

    Read more

    Prototype: update_field($selector, $value, [$post_id]);

    Note: to update a date field, the data has to be formatted first (even in case of copying the data from a date field).

    $date_from = get_field( 'old_field', $old_post_id );
    $date = DateTime::createFromFormat( 'd/m/Y', $date_from);
    $date = $date->format( 'Ymd' );
    
    update_field('new_field', $date, $new_post_id);

    Source : https://www.advancedcustomfields.com/resources/update_field/

  • InnerBlock

    Read more

    Interesting attributes : allowedBlocks, template, InnerBlocks.

    Example of a devblock/render.php

    <?php
    
    $classes_container = array( 'block__container' );
    $classes_innerblock = array( 'block__innerblock' );
    
    if ( !empty( $block['className'] ) ){
    	$classes_root = array_merge( $classes_root, explode( ' ', $block['className'] ) );
    }
    
    $allowed_blocks = array( 'namespace/blockname' );
    
    $template = array(
    	array(
    		'namespace/blockname',
    		array(
    			'className'		=> 'some-class'
    		)
    	)
    );
    
    ?>
    
    <div class="<?php echo join( ' ', $classes_container ); ?>">
    	<InnerBlocks
    		class="<?php echo join( ' ', $classes_innerblock ); ?>"
    		allowedBlocks="<?php echo esc_attr( wp_json_encode( $allowed_blocks ) ); ?>"
    		template="<?php echo esc_attr( wp_json_encode( $template ) ); ?>" />
    
    </div>