Skip to content

Instantly share code, notes, and snippets.

@kjvdven
Created May 14, 2025 12:17
Show Gist options
  • Save kjvdven/a8a3cbf2c3f68dff50df55752dce4e54 to your computer and use it in GitHub Desktop.
Save kjvdven/a8a3cbf2c3f68dff50df55752dce4e54 to your computer and use it in GitHub Desktop.
Fix issue with MailPoet and ACF breaking the post template selection in the location rules.
add_filter("acf/location/rule_values/post_template", function ($choices) {
  // Remove MailPoet or any values that are not strings
  foreach ($choices as $key => $value) {
    // Remove keys/groups like 'mailpoet_email' containing WP_Block_Template objects
    if (
      is_array($value) &&
      !empty($value) &&
      isset($value["newsletter"]) &&
      is_object($value["newsletter"]) &&
      get_class($value["newsletter"]) === "WP_Block_Template"
    ) {
      unset($choices[$key]);
      continue;
    }
    // If it's not an array, but not a string, remove as well
    if (!is_string($value) && !is_array($value)) {
      unset($choices[$key]);
    }
    // If it's an array (subchoices like pages), check them:
    if (is_array($value)) {
      foreach ($value as $subkey => $subval) {
        if (!is_string($subval)) {
          unset($choices[$key][$subkey]);
        }
      }
      // If group ends up empty, remove group
      if (empty($choices[$key])) {
        unset($choices[$key]);
      }
    }
  }
  return $choices;
});
@kjvdven
Copy link
Author

kjvdven commented May 14, 2025

Fixes:

Fatal error: Uncaught Error: Object of class WP_Block_Template could not be converted to string in /app/web/wp/wp-includes/formatting.php on line 1096 Error: Object of class WP_Block_Template could not be converted to string in /app/web/wp/wp-includes/formatting.php on line 1096

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment