Skip to content

Add a shortcode ​

A Mila shortcode is a registration file plus a view folder.

1. Registration and admin form ​

php
// functions/shortcodes/mila-quote.php
use Webhaki\Shortcode\Compilers\Shortcode;
use Webhaki\Shortcode\Forms\ShortcodeForm;
use Webhaki\Theme\Facades\Theme;

add_shortcode('mila-quote', __('Mila: Quote'), __('A large quote'), function (Shortcode $shortcode) {
    return Theme::partial('shortcodes.mila-quote.index', compact('shortcode'));
});

shortcode()->setAdminConfig('mila-quote', function (array $attributes) {
    $form = mila_shortcode_heading_fields(ShortcodeForm::createFromArray($attributes), false);

    return mila_shortcode_spacing_field($form);
});

Every .php file in functions/shortcodes/ is loaded automatically.

2. View ​

Create partials/shortcodes/mila-quote/index.blade.php. Read the fields from $shortcode, e.g. $shortcode->title.

3. Styles ​

Add the block's styles under assets/sass/mila/ and import them in _mila.scss. Rebuild with npm run prod.

Helpers ​

HelperWhat it does
mila_shortcode_heading_fields($form, $withButton = true)Adds Subtitle, Title, Description and (optionally) Button text / URL.
mila_shortcode_spacing_field($form, $default = 'default')Adds Vertical spacing (Default, Compact, None).
mila_parse_links($value)Turns Label | URL lines (or ;;-separated) into [['label' => …, 'url' => …]].
mila_filter_products($filters, $limit)Published, non-variation products matching filters (categories, collections…).
mila_product_has_price($product)false for products with no price, so "Add to bag" can be hidden.
mila_is_light_color($color)Whether a color is light (e.g. picks the light logo for white header text).