WordPress Update Handler
Updates existing WordPress posts and pages in the local installation using native WordPress functions with selective field updates and taxonomy management.
Architecture
Base Class: Extends PublishHandler (@since v0.2.1)
Inherited Functionality:
- Engine data retrieval for source_url matching
- Standardized response formatting
- Centralized logging and error handling
Requirements: Requires source_url from engine data (provided by fetch handlers or AI tools)
Local WordPress Integration
wp_update_post: Uses WordPress’s native wp_update_post() function for content modification.
URL-based Identification: Extracts post ID from source URLs using url_to_postid() function.
Selective Updates: Only updates specified fields, leaving other post data unchanged.
Required Parameters
Tool Call Parameters:
source_url: WordPress post/page URL (required for post identification, provided by centralized engine data via datamachine_engine_data filter)
Optional Update Parameters:
title: New post title (updatespost_title)content: New post content (updatespost_content)category: Category assignmenttags: Tag assignment (comma-separated)- Custom taxonomy parameters
Usage Examples
Basic Content Update:
// Note: source_url is automatically provided via centralized datamachine_engine_data filter
// from the fetch handler that originally retrieved the content
$parameters = [
'source_url' => 'https://site.com/existing-post/', // From engine data
'content' => 'Updated post content with <strong>HTML formatting</strong>.'
];
$tool_def = [
'handler_config' => [] // No configuration required
];
$result = $handler->handle_tool_call($parameters, $tool_def);
Full Post Update:
$parameters = [
'source_url' => 'https://site.com/existing-post/',
'title' => 'Updated Post Title',
'content' => 'Completely refreshed content.',
'category' => 'Updated Category',
'tags' => 'new tag, updated tag',
'custom_taxonomy' => 'custom term'
];
Post Identification Process
- URL Validation: Validates that
source_urlparameter is provided - Post ID Extraction: Uses
url_to_postid()to convert URL to post ID - Post Existence Check: Verifies that post exists using
get_post() - Update Authorization: Inherits WordPress permission checks from
wp_update_post()
Content Processing
Selective Updates: Only fields with provided parameters are updated:
- Title updates only if
titleparameter provided - Content updates only if
contentparameter provided - Taxonomies updated based on parameter availability
Content Sanitization: Applies WordPress security functions:
sanitize_text_field()for titleswp_kses_post()for contentwp_unslash()for proper encoding
Tool Call Response
Success Response:
[
'success' => true,
'data' => [
'updated_id' => 123,
'post_url' => 'https://site.com/post-permalink',
'modifications' => ['title', 'content', 'category'],
'taxonomy_results' => [
'category' => ['success' => true, 'assigned_terms' => ['Updated Category']],
'tags' => ['success' => true, 'assigned_terms' => ['new tag', 'updated tag']]
]
],
'tool_name' => 'wordpress_update'
]
No Updates Response:
[
'success' => true,
'data' => [
'updated_id' => 123,
'post_url' => 'https://site.com/post-permalink',
'modifications' => [],
'message' => 'No updates applied - post unchanged'
],
'tool_name' => 'wordpress_update'
]
Error Response:
[
'success' => false,
'error' => 'Error description',
'tool_name' => 'wordpress_update'
]
Taxonomy Management
Core Taxonomies:
category: Updates post categoriestags: Updates post tags (comma-separated string)
Dynamic Taxonomies: Automatically processes additional string parameters as potential taxonomy terms.
Assignment Logic:
- Validates taxonomy exists for post type
- Creates terms if they don’t exist (where supported)
- Replaces existing terms with new assignments
- Returns detailed success/error status
Update Validation
Pre-Update Checks:
- Post existence validation
- URL-to-ID conversion validation
- Parameter presence validation
Update Execution:
- Uses WordPress native functions for security
- Maintains post metadata and relationships
- Preserves unmodified fields
Error Handling
URL Errors:
- Missing
source_urlparameter - Invalid URLs that don’t resolve to post IDs
- Non-existent post references
WordPress Errors:
wp_update_post()failures- Permission denied errors
- Database connection issues
Taxonomy Errors:
- Invalid taxonomy references
- Term assignment failures
- Non-existent custom taxonomies
Security Features
Input Sanitization: All input sanitized using WordPress security functions.
Permission Inheritance: Relies on WordPress’s built-in capability checks in wp_update_post().
Content Filtering: Uses wp_kses_post() to maintain safe HTML while blocking dangerous content.
Workflow Integration
Source URL Requirement: Requires source URL from centralized engine data via datamachine_engine_data filter for post identification, making it suitable for content update workflows.
ToolResultFinder Integration (@since v0.2.0): UpdateStep uses the ToolResultFinder utility class for locating handler tool execution results in data packets.
Tool Result Search Pattern:
use DataMachineEngineAIToolResultFinder;
// UpdateStep.php
$tool_result_entry = ToolResultFinder::findHandlerResult($data, $handler_slug);
if ($tool_result_entry) {
// AI successfully executed handler tool
return $this->create_update_entry_from_tool_result(
$tool_result_entry,
$data,
$handler_slug,
$flow_step_id
);
}
// AI did not execute handler tool - fail cleanly
do_action('datamachine_log', 'error', 'UpdateStep: AI did not execute handler tool');
return [];
Search Logic:
- Searches data packets for
type= ‘tool_result’ or ‘ai_handler_complete’ - Matches
metadata.handler_toolagainst handler slug (e.g., ‘wordpress_update’) - Returns first matching entry or null if no match found
- Centralizes search logic eliminating code duplication
Benefits:
- Universal search utility shared across all update handlers
- Consistent tool result detection across step types
- Simplified update handler implementation
- Centralized maintenance for search improvements
Metadata Preservation: Maintains existing post metadata, publication dates, and author information.
Selective Modification: Allows targeted updates without affecting other post aspects.
Logging: Detailed debug logging for URL resolution, update operations, taxonomy assignments, and error conditions.