Control Block Patterns Shortcodes

Below, you’ll find a detailed guide on how to add the Control Block Patterns Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Control Block Patterns Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Control Block Patterns Plugin and the shortcodes it provides:

Plugin Icon
Build & Control Block Patterns – Boost up Gutenberg Editor

"Build & Control Block Patterns – Boost up Gutenberg Editor is a WordPress plugin designed to enhance your Gutenberg Editor experience. It lets you create, manage, and control block patterns effortlessly for streamlined content creation."

★★★★★ (2) Active Installs: 3000+ Tested with: 6.3.2 PHP Version: 5.6
Included Shortcodes:
  • [control_block_patterns]
  • [ctrlbp_meta]

Control Block Patterns [control_block_patterns] Shortcode

The Control Block Patterns shortcode is a powerful tool that allows users to render specific block patterns. It uses the ‘id’ or ‘title’ attribute to retrieve the desired pattern. If no ‘id’ or ‘title’ is provided, it returns nothing. The retrieved pattern is then processed and displayed using the ‘do_blocks’ function.

Shortcode: [control_block_patterns]

Parameters

Here is a list of all possible control_block_patterns shortcode parameters and attributes:

  • id – The unique identifier of the block pattern.
  • title – The title of the block pattern.

Examples and Usage

Basic example – Display a control block pattern by ID

[control_block_patterns id=1 /]

Advanced examples:

Example 1 – Display a control block pattern by title

[control_block_patterns title="My Pattern" /]

Example 2 – Display a control block pattern by referencing both ID and title. The pattern will first try to load by ID, but if not found, it will try to load by title.

[control_block_patterns id=1 title="My Pattern" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [control_block_patterns] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'control_block_patterns', [ $this, 'render' ] );

Shortcode PHP function:

                    function render($atts){
		
		$atts = shortcode_atts(
			[
				'id' => '',
				'title' => ''
			],
			$atts
		);

		$pattern_id = $atts['id'];
		unset( $atts['id'] );

		if( !empty($atts['title']) ){
			$pattern = get_page_by_title($atts['title'], OBJECT, 'ctrl_block_patterns');
		}elseif ( !empty($pattern_id) ) {
			$pattern = get_post($pattern_id);
		}else{
			$pattern = NULL;
		}
		

		if( empty( $pattern ) ) return;

		return \do_blocks($pattern->post_content);

	}
                    

Code file location:

control-block-patterns/control-block-patterns/classes/Patterns/Shortcode.php

Control Block Patterns [ctrlbp_meta] Shortcode

The Control Block Patterns plugin shortcode, ‘ctrlbp_meta’, allows you to fetch and display metadata from a specific object. This shortcode parses the attributes ‘id’, ‘object_id’, ‘attribute’, and ‘render_shortcodes’. It then checks if the ‘id’ attribute is empty. If not, it retrieves the value of the field specified by ‘id’ and ‘object_id’. The value can be processed further by other shortcodes if ‘render_shortcodes’ is set to ‘true’.

Shortcode: [ctrlbp_meta]

Parameters

Here is a list of all possible ctrlbp_meta shortcode parameters and attributes:

  • id – Unique identifier for the meta field.
  • object_id – The ID of the post or user object.
  • attribute – Optional attribute for additional information.
  • render_shortcodes – Controls if shortcodes within the meta field are processed.

Examples and Usage

Basic example – Showcases a simple usage of the shortcode by just referencing the ‘id’ attribute.

[ctrlbp_meta id='sample_id' /]

Advanced examples

Using the shortcode to display a specific object by referencing its ‘id’ and ‘object_id’. This can be useful when you want to target a specific object within a collection.

[ctrlbp_meta id='sample_id' object_id='object_1' /]

Using the shortcode to render other shortcodes within the value of the specified attribute. The ‘render_shortcodes’ attribute is set to ‘true’ to enable this functionality.

[ctrlbp_meta id='sample_id' render_shortcodes='true' /]

Using the shortcode to fetch and display a value from a specific attribute. Here, the ‘attribute’ attribute is used to specify the attribute from which the value should be fetched.

[ctrlbp_meta id='sample_id' attribute='attribute_1' /]

PHP Function Code

In case you have difficulties debugging what causing issues with [ctrlbp_meta] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'ctrlbp_meta', [ $this, 'register_shortcode' ] );

Shortcode PHP function:

                    function register_shortcode( $atts ) {
		$atts = wp_parse_args( $atts, [
			'id'                => '',
			'object_id'         => null,
			'attribute'         => '',
			'render_shortcodes' => 'true',
		] );
		Helpers_Array::change_key( $atts, 'post_id', 'object_id' );
		Helpers_Array::change_key( $atts, 'meta_key', 'id' );

		if ( empty( $atts['id'] ) ) {
			return '';
		}

		$field_id  = $atts['id'];
		$object_id = $atts['object_id'];
		unset( $atts['id'], $atts['object_id'] );

		$value = $this->get_value( $field_id, $object_id, $atts );
		$value = 'true' === $atts['render_shortcodes'] ? do_shortcode( $value ) : $value;

		return $value;
	}
                    

Code file location:

control-block-patterns/control-block-patterns/classes/Shortcode.php

Conclusion

Now that you’ve learned how to embed the Control Block Patterns Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *