Change Automatic Platform Optimization for WordPress setting

[Automatic Platform Optimization for WordPress](https://developers.cloudflare.com/automatic-platform-optimization/) serves your WordPress site from Cloudflare's edge network and caches third-party fonts.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Change Automatic Platform Optimization for WordPress setting
8
 * [Automatic Platform Optimization for WordPress](https://developers.cloudflare.com/automatic-platform-optimization/)
9
serves your WordPress site from Cloudflare's edge network and caches
10
third-party fonts.
11

12
 */
13
export async function main(
14
  auth: Cloudflare,
15
  zone_identifier: string,
16
  body: {
17
    value: {
18
      cache_by_device_type: boolean;
19
      cf: boolean;
20
      enabled: boolean;
21
      hostnames: string[];
22
      wordpress: boolean;
23
      wp_plugin: boolean;
24
      [k: string]: unknown;
25
    };
26
    [k: string]: unknown;
27
  }
28
) {
29
  const url = new URL(
30
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/settings/automatic_platform_optimization`
31
  );
32

33
  const response = await fetch(url, {
34
    method: "PATCH",
35
    headers: {
36
      "X-AUTH-EMAIL": auth.email,
37
      "X-AUTH-KEY": auth.key,
38
      "Content-Type": "application/json",
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49