Code Snippets

Here are a few useful code snippets to use with Cwicly

Dynamic menu (Polylang example)

While this is more geared towards Polylang, this can be applied to any type of condition.

Apply a specific menu ID with conditions to all Cwicly Menu blocks or targetted Menu blocks.

Make sure to change the Menu ID values to ones corresponding to menus on your installation.

/**
 * 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

If you don't have any other block on your post than one or multiple Classic Editor blocks, you might notice that no paragraph breaks are applied.

You can add the following code to your WordPress theme's functions.php file or use a custom snippet plugin to automatically apply <p> tags.

Classic block image styles

The styles for Classic Editor block images aren't retained in the Cwicly Theme. If you still use the Classic Editor and want to apply the default styling that comes with figures and images, add the following CSS snippet to a Cwicly global stylesheet.

Add menu item to Cwicly menu

Modify WooCommerce default number of products per page

A WooCommerce shop page can have up to 16 products by default. When Inheriting the Query from the URL, use the code snippet below to modify the number of products output for the query.

Loading animation

Last updated

Was this helpful?