一键导入
php-to-react-data
Patterns for passing data from PHP to React via wp_localize_script and REST API. Use when sharing config or data between PHP and React components.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Patterns for passing data from PHP to React via wp_localize_script and REST API. Use when sharing config or data between PHP and React components.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
Adds changelog entries to readme.txt following keepachangelog format. Use when updating the Unreleased section or documenting changes for a release.
Reproducibly capture in-product UI screenshots (admin popovers, settings teasers, dashboard widgets) used as embedded images in premium upsell teasers. Use whenever a teaser image needs refreshing after the underlying UI changes.
Guides implementation of structured action links on log events. Use when adding get_action_links() to a logger or migrating from get_log_row_details_output().
Enforces active voice for logger messages and the Event Details API. Use when writing a new logger class or modifying message arrays in getInfo().
How to design and regenerate marketing screenshots for the wordpress.org plugin page (banner-1544x500.png, screenshot-1.png, etc). Covers the event mix that converts, the reproducible WordPress Playground pipeline that bakes it, and every non-obvious gotcha from previous shoots. Use when refreshing screenshot-1.png, banners, or any image showing the Simple History event log.
Guidance for writing and running tests in Simple History. Covers which framework to use, how to run existing tests, and how to create new ones (including the codegen recording workflow).
基于 SOC 职业分类
| name | php-to-react-data |
| description | Patterns for passing data from PHP to React via wp_localize_script and REST API. Use when sharing config or data between PHP and React components. |
| allowed-tools | Read, Grep, Glob |
Simple History uses two main approaches to pass server-side data to React components. Choose based on when and where the data is needed.
wp_localize_script (Global Variable)Data is available immediately on page load as window.variableName.
PHP (in the module that enqueues scripts):
wp_localize_script(
'simple_history_pro_scripts', // Script handle.
'simpleHistoryPremium', // JS global variable name.
[
'alertsPageUrl' => Helpers::get_settings_page_sub_tab_url( Alerts_Module::ALERTS_TAB_SLUG ),
]
);
JavaScript:
const url = window.simpleHistoryPremium?.alertsPageUrl ?? '';
| Variable | Script | Contains |
|---|---|---|
simpleHistoryScriptVars | simple_history_script | Confirmation strings |
simpleHistoryCommandPalette | simple-history-command-palette | historyPageUrl |
simpleHistoryAdminBar | simple_history_admin_bar_scripts | adminPageUrl, viewSettingsUrl, currentUserCanViewHistory, currentPostId, currentPostTitle |
simpleHistoryPremium | simple_history_pro_scripts (premium add-on only) | alertsPageUrl and other premium-specific config |
simpleHistory + context (camelCase), e.g. simpleHistoryPremiumalertsPageUrlData flows through the React component tree via context: API response -> EventsSearchFilters -> state setters -> EventsSettingsContext -> useEventsSettings().
EventsSettingsProviderPHP — hook into the filter in class-wp-rest-searchoptions-controller.php:
add_filter( 'simple_history/search_options_data', [ $this, 'add_my_data' ] );
public function add_my_data( $data ) {
$data['my_feature_url'] = Helpers::get_settings_page_sub_tab_url( 'my_tab' );
return $data;
}
JavaScript — plumb through the data pipeline:
EventsGui.jsx: Add state: const [myUrl, setMyUrl] = useState();EventsGui.jsx: Add to eventsSettingsValue useMemo and pass setMyUrl to EventsSearchFiltersEventsSearchFilters.jsx: Extract from response: setMyUrl(searchOptionsResponse.my_feature_url)const { myUrl } = useEventsSettings();Key values from useEventsSettings():
mapsApiKeyhasExtendedSettingsAddOn, hasPremiumAddOnhasFailedLoginLimiteventsSettingsPageURL, eventsAdminPageURLalertsPageURL (populated by premium add-on only, undefined without it)userCanManageOptions| Criteria | wp_localize_script | Search Options API |
|---|---|---|
| Available at first render | Yes | No (async) |
| Works in FilteredComponent HOCs | Yes | No (outside provider) |
| Works in Fill/Slot children | Yes | Maybe (context issues) |
| Works inside EventsSettingsProvider | Yes | Yes |
| Extensible by other add-ons | No | Yes (via filter) |
| Avoids global variables | No | Yes |
wp_localize_script (immediate, no async issues)wp_localize_script on the premium script handlewp_localize_script is simpler and more reliable// WRONG: Building admin URLs in JavaScript.
const url = window.ajaxurl.replace('admin-ajax.php', '') + 'admin.php?page=...';
// WRONG: Fetching search-options manually when context is available.
apiFetch({ path: '/simple-history/v1/search-options' }).then(...);
// WRONG: Using wp_localize_script for large datasets.
// Use a REST endpoint instead.
// RIGHT: Use wp_localize_script for URLs, use context for feature data.
const url = window.simpleHistoryPremium?.alertsPageUrl ?? '';
const { hasPremiumAddOn } = useEventsSettings();