Notes about WordPress Gutenberg themes development.

  • Programmatically managing content

    Read more

    Creating

    $args = array(
    	'post_type'	=> 'eco__cpt_ressource',
    	'post_title'	=> get_the_title( $attachment_id ),
    	'post_status'	=> 'publish',
    	'post_author'	=> get_current_user_id(),
    	'post_date'	=> get_the_date( 'Y-m-d H:i:s', $attachment_id )
    );
    
    
    $new_postid = wp_insert_post( $args );

    See: https://developer.wordpress.org/reference/functions/wp_insert_post/

    Editing:

    See:

    • https://developer.wordpress.org/reference/functions/wp_update_post/
    • https://developer.wordpress.org/reference/functions/wp_set_object_terms/

    Deleting

    wp_delete_post( $post_id );

    See: https://developer.wordpress.org/reference/functions/wp_delete_post/

  • Menu: edit items

    Read more

    To edit individual items of a menu

    add_filter( 'wp_nav_menu_objects', 'dev__add_smiley', 11, 2 );
    
    function dev__add_smiley( $items, $args ) {
    
    	foreach ( $items as &$item ) {
    
    		// add class
    		array_push($item->classes, 'dw-submenu__parent');
    			
    		$item->title = ':D ' . $item->title;
    
    		// $item->title .= '</a>' . $subelement; note: close the link
    
    		}
    
    	}
    
    	return $items;
    
    }

    Sources:

    • https://developer.wordpress.org/reference/hooks/wp_nav_menu_items/
    • https://developer.wordpress.org/reference/hooks/wp_nav_menu_objects/
  • Parse args

    Read more

    Merge an array into a predefined one. Convenient to handle default configurations.

    wp_parse_args( string|array|object $args, array $defaults = array() ): array

    $args = wp_parse_args(
    
    	array(
    		'relation' => 'AND',
    		array(
    			'taxonomy' => 'product_type',
    			'field'    => 'slug',
    			'terms'    => 'variable',
    			'operator' => 'IN'
    		),
    	),
    
    	$query->get( 'tax_query' )
    
    );
    
    $query->set( 'tax_query',  $args );

    Source: https://developer.wordpress.org/reference/functions/wp_parse_args/

  • Register navigation menu

    Read more

    To register a menu :

    add_action( 'init', 'dev__register_menu' );
    
    function dev__register_menu() {
    
    	register_nav_menus(array(
    		'primary' => __( 'Primary Navigation', 'dev' ),
    		'secondary' => __( 'Secondary Navigation', 'dev' ),
    	));
    }
  • Block rendering filter

    Read more
    add_filter( 'render_block', 'dev__paragraph_add_block', 10, 2 );
    
    function dev__paragraph_add_block( $block_content, $block ){
    
    	if ( 'core/paragraph' === $block['blockName'] ){
    		$block_content = new WP_HTML_Tag_Processor( $block_content );
            $block_content->next_tag(); /* first tag should always be ul or ol */
            $block_content->add_class( 'wp-block-paragraph' );
            $block_content->get_updated_html();
    	}
    
    	return $block_content;
    
    }