milenbogoev

Register 404 Condition in Breakdance builder

Feb 12th, 2024
1,870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.27 KB | None | 0 0
  1. add_action("breakdance_register_template_types_and_conditions", function () {
  2.     \Breakdance\ConditionsAPI\register([
  3.         "supports" => ["element_display", "templating"],
  4.         "slug" => "is-404-condition", // Ensure this slug is unique
  5.         "label" => "404 Page",
  6.         "category" => "WordPress",
  7.         "operands" => ["is", "is not"],
  8.  
  9.         "values" => function () {
  10.             // Provide only the relevant, functional option
  11.             return [
  12.                 [
  13.                     "label" => "Page Status",
  14.                     "items" => [
  15.                         // This single option indicates the condition of being a 404 page
  16.                         ["text" => "404", "value" => "true"],
  17.                     ],
  18.                 ],
  19.             ];
  20.         },
  21.  
  22.         "allowMultiselect" => false, // Multiselect doesn't apply to this boolean condition
  23.  
  24.         "callback" => function (string $operand, $value) {
  25.             $is_404 = is_404(); // Determine if the current page is a 404
  26.             if ($operand === "is") {
  27.                 return $is_404 === ($value === "true");
  28.             } elseif ($operand === "is not") {
  29.                 return !$is_404 === ($value === "true");
  30.             }
  31.             return false;
  32.         },
  33.     ]);
  34. });
Tags: Breakdance
Advertisement
Add Comment
Please, Sign In to add comment