Jump to Navigation Jump to Main Content Jump to Footer
Home » Docs » Features » Utility and helper functions

Utility and helper functions

Chisel theme provides a focused set of helpers (PHP, Twig and Javascript) to speed up development, keep templates clean, and standardize output.

Twig helpers (from inc/WP/Twig.php)

  • get_nav_menu(name) Source: Chisel\WP\Twig::get_nav_menu() Returns a Timber menu object.
  • timber_set_product(post) Source**:** WoocommerceHelpers::timber_set_product() Ensures global $product is set correctly in loops.
  • post_classes(classes, prefix=’c-post–‘) Source**:** Chisel\WP\Twig::post_classes() Transforms WP post classes into ITCSS/BEM style.
  • slider_prepare_params(params) Source**:** Chisel\WP\Twig::slider_prepare_params() Converts a config array (incl. ACF slider settings) into data-* attributes and a data map.
  • get_responsive_image(image_id, size=’medium’, attrs={}) Source: ****ImageHelpers::get_responsive_image() Outputs responsive <img> with proper dimensions to reduce CLS.
  • comments_template() Source: CommentsHelpers::comments_template() Outputs comments pattern if supported/open.
  • bem(name, …modifiers) Source: ThemeHelpers::bem() Generates BEM modifiers from booleans, strings, or keyed values.
  • breadcrumbs() Source: YoastHelpers::breadcrumbs() Renders Yoast breadcrumbs (if plugin active).
  • get_icon(args) Source: Chisel\WP\Components::get_icon() Renders an SVG/icon by name and options.

PHP helpers (from inc/Helper/)

  • AssetsHelpers (inc/Helper/AssetsHelpers.php)
    • get_final_handle($handle) Returns namespaced asset handle; appends fast-refresh in dev hot mode.
  • AjaxHelpers (inc/Helper/AjaxHelpers.php)
    • get_ajax_endpoint_url() Full REST URL for theme AJAX (namespace/base from AjaxController).
    • ajax_json_decode($value) Safe JSON decode for POSTed strings.
  • CacheHelpers (inc/Helper/CacheHelpers.php)
    • expiry($custom = null) Returns theme’s cache expiry (0 if cache_everything is off).
    • clear_environment_cache() Clears Twig loader cache.
  • DataHelpers (inc/Helper/DataHelpers.php)
    • json_encode_for_data_attribute($data) JSON for embedding into data-* HTML attributes (escaped).
    • object_to_array($obj) Convert object → array via JSON.
  • ImageHelpers (inc/Helper/ImageHelpers.php)
    • get_image_url($name, $is_icon=false) URL to theme asset in /assets/images|icons/.
    • get_responsive_image($id, $size=’medium’, $attrs=[]) Responsive image HTML, with optional width/height override to prevent layout shifts.
  • ThemeHelpers (inc/Helper/ThemeHelpers.php)
    • is_dev_env(), is_fast_refresh()
    • get_theme_version(), get_theme_name()
    • bem($name, …$modifiers) Same behavior as Twig version
    • get_colors_palette($type) Returns palettes from theme.json for ACF or TinyMCE
    • get_login_page_logo_data() Array compatible with WP login CSS
  • YoastHelpers (inc/Helper/YoastHelpers.php)
    • is_yoast_active()
    • breadcrumbs() HTML, empty if not active/front page
  • WoocommerceHelpers (inc/Helper/WoocommerceHelpers.php)
    • is_woocommerce_active()
    • timber_set_product($post) Fixes loop product context
    • get_products_grid_classnames($products, $has_sidebar) Returns o-grid classes capped at 4 cols
  • BlocksHelpers (inc/Helper/BlocksHelpers.php)
    • get_block_object_classnames($blockName) e.g., c-block c-block--chisel c-block--slider
    • render_twig_file($blockName, $context) Render a block’s render.twig
    • acf_block_render($block, $content, $is_preview, $post_id) Main ACF block renderer, builds context (fields, wrapper_attributes, etc.), applies filters, renders block Twig
    • get_block_inline_css($blocksUrl, $blockName) Fetch built CSS (from script.css) for inlining
  • AcfHelpers (inc/Helper/AcfHelpers.php)
    • get_field($selector, $postId=false, $format=true, $escape=false) Safe wrapper (returns false if ACF missing)
    • update_field($selector, $value, $postId=false)
  • CommentsHelpers (inc/Helper/CommentsHelpers.php)
    • comments_template() Returns comments block pattern when comments are supported and open
  • GravityFormsHelpers (inc/Helper/GravityFormsHelpers.php)
    • is_gf_active()
    • get_forms_list() id => title map, useful for ACF select population
    • get_form($id, $showTitle=false, $showDesc=false, $showInactive=false, $fieldValues=null, $ajax=true, $tabindex=0, $_echo=false) Renders gravity form programmatically (by id)

Javascript helpers

  • ajaxRequest (src/scripts/modules/utils.js) Signature: ajaxRequest(action, ajaxData = {}, ajaxParams = {}, ajaxHeaders = {}) => Promise<ResponseJSON> Purpose: Thin wrapper over fetch for authenticated REST AJAX calls to theme endpoints. Behavior: Builds endpoint from localized chiselScripts.ajax.url and action (e.g., /wp-json/chisel/v2/ajax/load-more) Security context: The URL and nonce come from PHP localization in ChiselWPAssets::set_properties() under chiselScripts.ajax. Response: Matches theme endpoints (e.g., { success: true|false, data, message? }), see inc/WP/AjaxEnpoints.php.
  • load-more.js (src/scripts/modules/load-more.js) Purpose: Attaches a “Load more” behavior to a listing using the utils.ajaxRequest('load-more', …) endpoint. DOM hooks:
  • Container to append items: .js-load-more-container
  • Wrapper: .js-load-more (requires dataset: data-post-type, data-per-page, data-max-page)
  • Button: .js-load-more-button (adds .is-loading class while fetching) State: Starts at page: 2, stops when page >= maxPage and removes wrapper. Works with server template rendering via Twig item templates (see AjaxEnpoints::load_more()).
  • slider.js (src/scripts/modules/slider.js) Purpose: Initializes Swiper instances for elements with .js-slider. Dynamically imports Swiper (CSS + JS). DOM hooks: Slider root: .js-slider (expects .swiper-slide children) Data attributes (selected) on .js-slider:
  • data-type (‘default’), data-arrows (‘yes’|’no’), data-dots (‘yes’|’no’), data-dots-dynamic (‘0’ or number)
  • data-autoplay (‘yes’|’no’), data-autoplay-timeout (ms), data-loop (‘yes’|’no’), data-speed (ms)
  • data-slides-per-view, data-space-between, data-direction (‘horizontal’|’vertical’), data-auto-height (‘yes’|’no’), data-center (‘yes’|’no’)
  • data-scrollbar (‘yes’|’no’)
  • data-breakpoints (JSON), data-args (JSON) to override/extend Swiper params
  • Thumbnails: data-thumbnails (count), optional data-thumbs-slider-params (JSON), data-thumbs-module-params (JSON) Params / Args callback: Setting a custom type for a slider allows you to create a custom params callback to define parameters specific to your slider, e.g. for products slider:

productsSliderParams() { this.params = { ...this.params, spaceBetween: 20, };

Features:

  • Arrows, pagination (bullets with optional dynamic bullets), scrollbar, loop, autoplay, breakpoints, arbitrary Swiper args.
  • Thumbnails support clones slider and wires via Thumbs module. Each slide should have data-thumbnail-url and an <img>; the script replaces attributes and sets width/height to avoid CLS.

Do you like Chisel?

Give it a star on GitHub!