A fully object-oriented WordPress plugin that connects to an external Python FastAPI backend, fetches product data, displays it using a shortcode, and allows adding new products through the WordPress admin.
This plugin is meant as a clean, well-structured example of integrating WordPress with a remote API using modern coding standards.
✔ Fetch all products (GET /products)
- Displays all available products from the Python API.
✔ Fetch single product by ID (GET /product/{id})
- You can fetch and display one specific product using:
[python_product id="10"]
✔ Add new products (POST /product/add)
- Admin-side form allows adding new products to the API.
✔ Shortcodes included
- Display all products:
[python_products] - Limit products:
[python_products limit="5"] - Display a single product:
[python_product id="7"]
✔ Admin settings page
- Located at: Settings → Python API Demo
- Allows configuration of:
- API Base URL
- API Key
- Enable/Disable caching
- Cache duration
- Add product form (POST request)
✔ Optional caching (WordPress transients)
- Reduces API calls
- Cached for X seconds (configurable)
- Cache automatically clears on new product POST
✔ Full OOP architecture
- The plugin uses a loader class and clean separation of concerns.
python-api-demo/
├── python-api-demo.php # Main plugin loader
├── includes/
│ ├── class-plugin-loader.php # Initializes plugin classes
│ ├── class-api-client.php # Performs GET/POST requests to Python API
│ ├── class-admin-page.php # Settings page (API URL, token, caching)
│ ├── class-products-shortcode.php # Shortcode: all products + limit
│ ├── class-product-shortcode.php # Shortcode: single product by ID
│ ├── class-product-form.php # Add product via POST to API
│ └── helpers.php # Sanitization, utility helpers
├── assets/
│ └── admin.css
└── README.md
-
Download the plugin or clone the repository.
-
Upload to your WordPress installation under:
wp-content/plugins/ -
Activate the plugin.
-
Open: Settings → Python API Demo
-
Configure:
- API Base URL (e.g
http://127.0.0.1:8000/) - API Key
- Cache settings
- API Base URL (e.g
-
Add one of the shortcodes to your page/post.
[python_products]
With limit:
[python_products limit="3"]
[python_product id="10"]
If product does not exist → shows a styled "Product not found" message.
All API requests use:
Headers:
X-API-KEY: YOUR_API_KEY
Content-Type: application/json
GET {API_URL}/products
PHP example (inside class-api-client.php):
$response = wp_remote_get(
trailingslashit( $base_url ) . 'products',
[ 'headers' => $this->get_headers() ]
);GET {API_URL}/product/{id}
Shortcode example:
[python_product id="5"]
Endpoint:
POST {API_URL}/product/add
Sent JSON structure:
{
"name": "Keyboard Pro",
"price": 89.99,
"image": "https://example.com/kb.jpg",
"stock": 10
}PHP example:
$response = wp_remote_post(
trailingslashit( $base_url ) . 'product/add',
[
'headers' => $this->get_headers(),
'body' => json_encode( $data ),
]
);Located in: ⚙ Settings → Python API Demo
It contains:
✔ API Base URL
✔ API Key
✔ Caching Options
- enable / disable
- cache lifetime (seconds)
✔ Add New Product Form
Fields:
- Name
- Price
- Image URL
- In Stock (1/0)
The form sends POST request directly to Python API and clears cache.
- Initializes all components in correct order.
- Handles communication with Python API:
- GET all products
- GET single product
- POST new product
- Caching layer
- Headers & error handling
- Renders list of products via shortcode.
- Renders a single product by ID.
- Creates settings page and stores options.
- Handles:
- form UI
- nonce validation
- POST request wrapper
- success/error messages
The plugin uses strict WP security standards:
✔ wp_nonce_field()
✔ sanitize_text_field()
✔ sanitize_url()
✔ esc_html() and esc_attr() when printing
✔ API key stored in WordPress options
✔ No direct file access
- Wrong API URL
- API is down
- API key missing
- Cache contains stale results
- Token mismatch
- Missing header
- Incorrect ID
- Product removed
Happens when:
- Python server returns non-JSON
- App crashed or returns HTML error
- Port conflict (already in use)
Pull requests are welcome. Follow:
- WordPress Coding Standards
- OOP architecture
- Proper escaping & sanitization
MIT License. You are free to use, modify and distribute.