Bricks 中文文档:bricks/frontend/render_data

标签TAGS: 

📎 原始资料  |  下载原始 JSON 文件 (bricks-frontend-render_data.json) 前言 Bricks 1.4 引入了 bricks/inputs/validate_field 钩子,允许你在 Bricks 提交表单之前验证单个字段。 用法 将代码加入子主题的 functions.php: add_filter(‘bricks/frontend/render_data’, function($content, $post, $area) { // Iterate over each heading tag $content = preg_r…

前言

Bricks 1.4 引入了 bricks/inputs/validate_field 钩子,允许你在 Bricks 提交表单之前验证单个字段。

用法

将代码加入子主题的 functions.php

add_filter('bricks/frontend/render_data', function($content, $post, $area) {
  // Iterate over each heading tag
  $content = preg_replace_callback(
    '/((.*?)(<\/h[1-6]>)/i',
    function($matches) {
      // Add 'id' attribute if it doesn't exist
      if (strpos($matches[2], 'id=') === false) {
        // Use heading's content as the ID
        $matches[0] = $matches[1] . ' id="' . sanitize_title($matches[3])
        . '">' . $matches[3] . $matches[4];
      }


      // Return the (potentially) modified heading tag
      return $matches[0];
    },
    $content // Content to modify
  );


  // Return modified content
  return $content;
}, 10, 3);

示例代码

筛选返回值

add_filter('bricks/frontend/render_data', function($content, $post, $area) {
  // Iterate over each heading tag
  $content = preg_replace_callback(
    '/((.*?)(<\/h[1-6]>)/i',
    function($matches) {
      // Add 'id' attribute if it doesn't exist
      if (strpos($matches[2], 'id=') === false) {
        // Use heading's content as the ID
        $matches[0] = $matches[1] . ' id="' . sanitize_title($matches[3])
        . '">' . $matches[3] . $matches[4];
      }


      // Return the (potentially) modified heading tag
      return $matches[0];
    },
    $content // Content to modify
  );


  // Return modified content
  return $content;
}, 10, 3);

补充说明

更多细节可参考 官方文档