|
|
--- |
|
|
title: Wp Agent |
|
|
emoji: π |
|
|
colorFrom: purple |
|
|
colorTo: indigo |
|
|
sdk: gradio |
|
|
sdk_version: 5.21.0 |
|
|
app_file: app.py |
|
|
pinned: false |
|
|
hf_oauth: true |
|
|
hf_oauth_scopes: |
|
|
- inference-api |
|
|
--- |
|
|
Step 1: Project Structure |
|
|
Your project should look like this: |
|
|
|
|
|
|
|
|
your-project/ |
|
|
βββ app.py |
|
|
βββ elementor-agent.py |
|
|
βββ requirements.txt |
|
|
βββ .env |
|
|
βββ .gitignore |
|
|
βββ README.md |
|
|
Step 2: Install Dependencies |
|
|
First, ensure you have Python installed. Then create a virtual environment and install dependencies: |
|
|
|
|
|
|
|
|
# Create a virtual environment (optional but recommended) |
|
|
python -m venv venv |
|
|
source venv/bin/activate # On Windows: venv\Scripts\activate |
|
|
|
|
|
# Install dependencies |
|
|
pip install flask requests python-dotenv |
|
|
Update requirements.txt with these packages: |
|
|
|
|
|
|
|
|
flask==2.3.2 |
|
|
requests==2.31.0 |
|
|
python-dotenv==1.0.0 |
|
|
Step 3: Configure .env |
|
|
Create a .env file to store sensitive credentials (e.g., WordPress login details). Add: |
|
|
|
|
|
|
|
|
WP_API_URL=https://your-wordpress-site.com/wp-json |
|
|
WP_USERNAME=your_username |
|
|
WP_PASSWORD=your_password |
|
|
WP_AUTH_TYPE=basic # Or 'oauth' if using OAuth |
|
|
Add .env to .gitignore to prevent it from being committed: |
|
|
|
|
|
|
|
|
.venv |
|
|
.env |
|
|
Step 4: Build elementor-agent.py |
|
|
This file will handle interactions with WordPress/Elementor. Here's a basic example: |
|
|
|
|
|
|
|
|
import requests |
|
|
from dotenv import load_dotenv |
|
|
import os |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
WP_API_URL = os.getenv("WP_API_URL") |
|
|
WP_USERNAME = os.getenv("WP_USERNAME") |
|
|
WP_PASSWORD = os.getenv("WP_PASSWORD") |
|
|
AUTH_TYPE = os.getenv("WP_AUTH_TYPE", "basic") |
|
|
|
|
|
def get_wp_posts(): |
|
|
"""Fetch all WordPress posts.""" |
|
|
url = f"{WP_API_URL}/wp/v2/posts" |
|
|
if AUTH_TYPE == "basic": |
|
|
response = requests.get(url, auth=(WP_USERNAME, WP_PASSWORD)) |
|
|
else: |
|
|
# Add OAuth logic here if needed |
|
|
pass |
|
|
return response.json() |
|
|
|
|
|
def create_elementor_page(title, content): |
|
|
"""Create a new Elementor page via WordPress REST API.""" |
|
|
url = f"{WP_API_URL}/wp/v2/pages" |
|
|
data = { |
|
|
"title": title, |
|
|
"content": content, |
|
|
"status": "publish", |
|
|
"template": "elementor" # Ensure Elementor template is set |
|
|
} |
|
|
if AUTH_TYPE == "basic": |
|
|
response = requests.post(url, json=data, auth=(WP_USERNAME, WP_PASSWORD)) |
|
|
else: |
|
|
# Add OAuth headers here |
|
|
pass |
|
|
return response.json() |
|
|
Step 5: Build app.py (Flask Server) |
|
|
Create a simple Flask app to expose endpoints for interacting with WordPress: |
|
|
|
|
|
|
|
|
from flask import Flask, request, jsonify |
|
|
from elementor_agent import create_elementor_page, get_wp_posts |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
@app.route('/create-page', methods=['POST']) |
|
|
def create_page(): |
|
|
data = request.json |
|
|
title = data.get('title') |
|
|
content = data.get('content') |
|
|
|
|
|
if not title or not content: |
|
|
return jsonify({"error": "Title and content are required"}), 400 |
|
|
|
|
|
response = create_elementor_page(title, content) |
|
|
return jsonify(response), 201 |
|
|
|
|
|
@app.route('/get-pages', methods=['GET']) |
|
|
def get_pages(): |
|
|
pages = get_wp_posts() |
|
|
return jsonify(pages), 200 |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(debug=True) |
|
|
Step 6: Run the App |
|
|
Start the Flask server: |
|
|
|
|
|
python app.py |
|
|
The app will run on http://localhost:5000. |
|
|
Test the endpoints using tools like curl or Postman: |
|
|
Create a page: |
|
|
|
|
|
curl -X POST http://localhost:5000/create-page \ |
|
|
-H "Content-Type: application/json" \ |
|
|
-d '{"title": "My New Page", "content": "<p>Hello from Elementor!</p>"}' |
|
|
Fetch pages: |
|
|
|
|
|
curl http://localhost:5000/get-pages |
|
|
Step 7: Enhancements & Next Steps |
|
|
Error Handling: Add try-except blocks in elementor-agent.py to handle API errors (e.g., 401 Unauthorized). |
|
|
OAuth Support: If your WordPress site uses OAuth, update elementor-agent.py to handle tokens. |
|
|
Database Integration: Add a database (e.g., SQLite) to store page metadata. |
|
|
Frontend: Build a simple HTML/CSS frontend to interact with the API. |
|
|
Security: Use HTTPS and validate all user inputs. |
|
|
Example Workflow |
|
|
A user sends a POST request to /create-page with page details. |
|
|
app.py calls create_elementor_page() from elementor-agent.py. |
|
|
The agent authenticates with WordPress and creates the page via the REST API. |
|
|
The response is returned to the user. |
|
|
Let me know if you want to dive deeper into any part of this setup! |
|
|
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference |
|
|
|