'POST', 'callback' => 'ycw_set_google_meta', 'permission_callback' => 'ycw_auth_check', ]); register_rest_route('ycw/v1', '/google/ga4', [ 'methods' => 'POST', 'callback' => 'ycw_set_ga4_id', 'permission_callback' => 'ycw_auth_check', ]); }); /** * Auth: allow Application Password / Basic Auth */ function ycw_auth_check() { return current_user_can('manage_options'); } /** * Save GSC META */ function ycw_set_google_meta($request) { $meta = sanitize_text_field($request->get_param('meta')); if (!$meta) { return new WP_Error('no_meta', 'Missing meta value', ['status' => 400]); } update_option('ycw_google_meta', $meta); return ['status' => 'ok']; } /** * Save GA4 Measurement ID */ function ycw_set_ga4_id($request) { $ga4 = sanitize_text_field($request->get_param('ga4')); if (!$ga4) { return new WP_Error('no_ga4', 'Missing GA4 ID', ['status' => 400]); } update_option('ycw_ga4_id', $ga4); return ['status' => 'ok']; } /** * ========== Output to
========== */ add_action('wp_head', function () { // GSC META if ($meta = get_option('ycw_google_meta')) { echo '' . PHP_EOL; } // GA4 if ($ga = get_option('ycw_ga4_id')) { ?>