Code Snippets
Here are a few useful code snippets to use with Cwicly
Dynamic menu (Polylang example)
/**
* Modify a specific - or not - Cwicly Menu block menu ID based on various conditions.
* Function using the `render_block_data` filter-hook.
*
* @param array $parsed_block The block being rendered.
* @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content.
* @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
*
* @return array
*/
function cc_render_block_data_filter($parsed_block, $source_block, $parent_block)
{
// Make sure that we are only modifying the Cwicly Menu block and that it has attributes.
if ($parsed_block['blockName'] === 'cwicly/menu' && isset($parsed_block['attrs'])) {
// Set conditions to true to target all Cwicly Menu blocks
$conditions = true;
/*
// Optional: target specific menu with block ID
if (isset($parsed_block['attrs']['id']) && $parsed_block['attrs']['id'] === 'menu-c413s33f') {
$conditions = true;
} else {
$conditions = false;
}
*/
if ($conditions) {
// Default language slug
$lang_slug = '';
// Get current language slug if Polylang is installed
if (function_exists('pll_current_language')) {
$lang_slug = pll_current_language('slug');
} else {
$lang_slug = '';
}
// Set menu ID for each language slug (or default)
if ($lang_slug === 'en') {
$parsed_block['attrs']['menuSelected'] = "10"; // Menu ID
} else if ($lang_slug === 'fr') {
$parsed_block['attrs']['menuSelected'] = "11"; // Menu ID
} else {
// Default menu ID
$parsed_block['attrs']['menuSelected'] = "10"; // Menu ID
}
}
}
return $parsed_block;
}
add_filter('render_block_data', 'cc_render_block_data_filter', 10, 3);Classic editor block <p> tags missing
Classic block image styles
Add menu item to Cwicly menu
Modify WooCommerce default number of products per page
Loading animation
Last updated