The AJAX Suggestion System provides real-time, autocomplete-based search functionality for linking translations between posts and taxonomy terms in the WordPress multisite network. When the autocomplete feature is enabled in plugin settings, administrators can type to search for existing content in other language blogs rather than selecting from dropdown lists.
This page covers the AJAX endpoints, request handling, and response formatting. For information about the meta box UI that uses these suggestions, see Post and Page Meta Boxes. For taxonomy linking interfaces, see Taxonomy Management Interface.
The suggestion system consists of two AJAX endpoints that handle autocompletion for different content types:
Sources: includes/MslsMetaBox.php34-69 includes/MslsPostTag.php23-72
The MslsMetaBox::suggest() method handles AJAX requests for post/page/custom post type suggestions.
| Parameter | Source | Purpose | Sanitization |
|---|---|---|---|
blog_id | POST | Target blog ID to search | FILTER_SANITIZE_NUMBER_INT |
post_type | POST | Post type to search | FILTER_SANITIZE_FULL_SPECIAL_CHARS |
s | POST | Search term or post ID | FILTER_SANITIZE_FULL_SPECIAL_CHARS |
Sources: includes/MslsMetaBox.php34-69 includes/MslsMetaBox.php77-103
The method performs these operations:
Input Validation: Checks for blog_id parameter using MslsRequest::has_var() includes/MslsMetaBox.php37
Blog Context Switch: Calls switch_to_blog() to query the target language blog includes/MslsMetaBox.php38
Query Construction: Builds args array with:
post_status: All non-internal post statuses via get_post_stati() includes/MslsMetaBox.php41posts_per_page: Limited to 10 results includes/MslsMetaBox.php42post_type: Sanitized from POST data includes/MslsMetaBox.php46-48s or include: Search term or direct post ID includes/MslsMetaBox.php51-59Numeric Detection: If search value is numeric, assumes it's a post ID and uses include parameter instead of s includes/MslsMetaBox.php59
Result Collection: Delegates to get_suggested_fields() which iterates posts and builds JSON includes/MslsMetaBox.php62
Context Restoration: Calls restore_current_blog() to return to original blog includes/MslsMetaBox.php64
Response: Sends JSON via wp_die() without escaping (raw JSON output) includes/MslsMetaBox.php68
Sources: includes/MslsMetaBox.php34-103
The MslsPostTag::suggest() method handles AJAX requests for taxonomy term suggestions (categories, tags, custom taxonomies).
| Parameter | Source | Purpose | Sanitization |
|---|---|---|---|
blog_id | POST | Target blog ID to search | FILTER_SANITIZE_NUMBER_INT |
post_type | POST | Taxonomy name (note: parameter name is reused) | FILTER_SANITIZE_FULL_SPECIAL_CHARS |
s | POST | Search term | FILTER_SANITIZE_FULL_SPECIAL_CHARS |
Sources: includes/MslsPostTag.php23-72
The term suggestion method follows a similar pattern but with taxonomy-specific logic:
Blog Context: Switches to target blog if blog_id parameter exists includes/MslsPostTag.php26-29
Query Arguments: Builds get_terms() args:
taxonomy: Taxonomy name from post_type field includes/MslsPostTag.php31-33orderby: name, order: ASC includes/MslsPostTag.php34-35number: Limited to 10 results includes/MslsPostTag.php36hide_empty: 0 to show all terms includes/MslsPostTag.php37search: Optional search term includes/MslsPostTag.php40-44Filter Hook: Applies msls_post_tag_suggest_args filter includes/MslsPostTag.php53
Term Processing: Iterates through results:
msls_post_tag_suggest_term filter includes/MslsPostTag.php62WP_Term instance includes/MslsPostTag.php64term_id and name to JSON includes/MslsPostTag.php65Response: Encodes and sends JSON without escaping includes/MslsPostTag.php71
Sources: includes/MslsPostTag.php23-72
The suggestion system uses the MslsRequest utility class for secure input handling.
Sources: includes/MslsRequest.php24-55 includes/MslsFields.php
The MslsFields class defines constants for field names used in AJAX requests:
FIELD_BLOG_ID: Target blog identifierFIELD_POST_TYPE: Content type or taxonomy nameFIELD_S: Search term parameterEach field has a configuration in MslsFields::CONFIG mapping to:
Sources: includes/MslsRequest.php14-22 includes/MslsMetaBox.php37-59 includes/MslsPostTag.php26-44
Sources: includes/MslsRequest.php24-55
Both suggestion endpoints return JSON arrays where each entry represents a suggested item.
The structure is a simple object mapping:
The MslsJson class provides the response building interface:
| Method | Purpose | Parameters |
|---|---|---|
add() | Add item to response | $id (int), $value (string) |
encode() | Generate JSON string | None |
Example usage from code:
Sources: includes/MslsMetaBox.php35 includes/MslsPostTag.php24 includes/MslsMetaBox.php97 includes/MslsPostTag.php65
The suggestion endpoints are called from the admin UI when autocomplete is enabled.
Sources: includes/MslsMetaBox.php285-349
The render_input() method outputs:
Hidden action field: <input type="hidden" name="msls_action" id="msls_action" value="suggest_posts"/> includes/MslsMetaBox.php331
Hidden post type field: <input type="hidden" name="msls_post_type" id="msls_post_type" value="[post_type]"/> includes/MslsMetaBox.php331-333
Text input fields for each language blog with class msls_title that JavaScript hooks into includes/MslsMetaBox.php317
Hidden ID fields to store selected post IDs includes/MslsMetaBox.php317
The taxonomy interface works similarly through MslsPostTag::the_input():
Hidden fields specify taxonomy and action type includes/MslsPostTag.php100-101 includes/MslsPostTag.php127-129
Text inputs with class msls_title for autocomplete includes/MslsPostTag.php103-105 includes/MslsPostTag.php137-139
Action value: suggest_terms instead of suggest_posts includes/MslsPostTag.php101 includes/MslsPostTag.php129
Sources: includes/MslsPostTag.php94-211
The suggestion system provides several filter hooks for customization.
Modifies the get_posts() query arguments before execution.
Location: includes/MslsMetaBox.php85
Parameters:
$args (array): Query arguments with keys like post_status, posts_per_page, post_type, s, includeExample use case: Restrict suggestions to specific post statuses or add custom query parameters.
Filters each WP_Post object before adding to JSON response.
Location: includes/MslsMetaBox.php95
Parameters:
$post (WP_Post): Post objectExample use case: Modify post title, exclude specific posts, or add custom data.
Modifies the get_terms() query arguments before execution.
Location: includes/MslsPostTag.php53
Parameters:
$args (array): Term query arguments with keys like taxonomy, orderby, order, number, hide_empty, searchExample use case: Change sorting, limit by parent term, or add custom taxonomy query parameters.
Filters each term object before adding to JSON response.
Location: includes/MslsPostTag.php62
Parameters:
$term (int|string|WP_Term): Term object or IDExample use case: Exclude specific terms, modify term names, or filter by custom taxonomy metadata.
Sources: includes/MslsMetaBox.php78-103 includes/MslsPostTag.php46-67
Both endpoints limit results to prevent performance issues:
posts_per_page parameter includes/MslsMetaBox.php42number parameter includes/MslsPostTag.php36The suggestion system uses switch_to_blog() and restore_current_blog() for cross-blog queries:
Implications:
Sources: includes/MslsMetaBox.php38 includes/MslsMetaBox.php64 includes/MslsPostTag.php27-28 includes/MslsPostTag.php68
The post suggestion endpoint calls wp_reset_postdata() to clean up the global $post object after iteration includes/MslsMetaBox.php100
Sources: includes/MslsMetaBox.php100
The suggestion system includes unit tests that mock WordPress functions and verify behavior.
Post Suggestions tests/phpunit/TestMslsMetaBox.php54-76:
switch_to_blog(), restore_current_blog(), get_posts()Empty Results tests/phpunit/TestMslsMetaBox.php78-86:
wp_reset_postdata() is calledRefresh this wiki
This wiki was recently refreshed. Please wait 7 days to refresh again.