WordPress Notes

Enabling to show private posts in parent dropdown / hierarchic CPT.

The the following code gets this done.

function show_private_posts_in_parent_dropdown( $args, $post ) {
        $args['post_status'] = array( 'publish', 'private' );
    return $args;
}
add_filter( 'page_attributes_dropdown_pages_args', 'show_private_posts_in_parent_dropdown', 10, 2 );
// also show private posts in the parent dropdown in quick edit
add_filter( 'quick_edit_dropdown_pages_args', 'show_private_posts_in_parent_dropdown', 10, 2 );

Getting Values from Parent Block.

on edit.js.

const parentBlock = useSelect(
		(select) => {
			const { getBlockParents, getBlock } = select("core/block-editor");
			const parentClientIds = getBlockParents(clientId);

			for (const parentClientId of parentClientIds) {
				const parentBlock = getBlock(parentClientId);
				if (parentBlock && parentBlock.name === "form-builder/form-builder") {
					return parentBlock;
				}
			}

			return null;
		},
		[clientId]
	);

	// Get the parent block's attributes
	const parentAttributes = parentBlock ? parentBlock.attributes : {};

	// Get the cpt_name attribute from the parent block
	const cptName = parentAttributes.selectedCpt;

	useEffect(() => {
		console.log("Parent CPT Name:", cptName);
		// Do something with the cpt_name attribute
	}, [cptName]);

How to include the JS Files only when a particular block is on the page.

// Register the assets
function ias_bave_quote_my_block_assets() {
	wp_register_style('ias-brave-quote-generator', plugin_dir_url(__FILE__) . 'vue/dist/assets/index.css');
	// Enqueue JS file without 'type=module' and 'crossorigin' initially
	wp_register_script('ias-brave-quote-generator', plugin_dir_url(__FILE__) . 'vue/dist/assets/index.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'ias_bave_quote_my_block_assets');

// Conditionally enqueue assets if the block is loaded
function ias_bave_quote_enqueue_my_block_assets() {
    if (is_singular() && has_block('shishir/brave-quote-generator')) {
       	// Enqueue CSS file
		wp_enqueue_style('ias-brave-quote-generator', plugin_dir_url(__FILE__) . 'vue/dist/assets/index.css');
		// Enqueue JS file without 'type=module' and 'crossorigin' initially
		wp_enqueue_script('ias-brave-quote-generator', plugin_dir_url(__FILE__) . 'vue/dist/assets/index.js', array(), null, true);
    }
}
add_action('wp_enqueue_scripts', 'ias_bave_quote_enqueue_my_block_assets');