This article demonstrates creating a settings UI for a custom WordPress block that interacts with an external API. The block displays football rankings, and the settings allow users to filter the data by country, league, and season.
Previous articles covered fetching and rendering API data in the block editor and on the front-end. This article focuses on integrating a settings panel within the WordPress block editor's control panel (the right-hand sidebar). This panel allows users to customize the data displayed by the block.
The process involves leveraging WordPress's InspectorControls
and core components like PanelBody
and ComboboxControl
to build a user-friendly interface. The API architecture is outlined, highlighting the relationships between countries, leagues, seasons, and rankings data.
The key steps include:
Fetching Country Data: The article shows how to fetch a list of countries from the RapidAPI using useEffect
and fetch
, storing the results in state.
Implementing InspectorControls
: The InspectorControls
component is imported and used to house the custom settings UI.
Creating a Custom Component (LeagueSettings.js
): A separate component manages the settings UI, improving code organization. This component uses PanelBody
to group settings and ComboboxControl
for country, league, and season selection. The ComboboxControl
allows for searching within the options.
Handling Data Changes: Functions like handleCountryChange
, handleLeagueChange
, and handleSeasonChange
update the component's state based on user selections, dynamically filtering the API data.
Fetching Data on Submit: A "Fetch Data" button triggers an API call using the selected settings, updating the displayed rankings.
The article provides code snippets illustrating these steps, including the conversion of API data into a format suitable for ComboboxControl
. It also addresses potential error handling for cases where data might be missing from the API.
While the current implementation allows for dynamic data fetching, the settings are not yet persistent. The next article will cover saving these settings to make the user selections permanent.
The above is the detailed content of Creating a Settings UI for a Custom WordPress Block. For more information, please follow other related articles on the PHP Chinese website!