[SOLVED] – INVALID_DOCUMENT – “text” was “” should be not (null or an empty string) (keyPath components->1->components->5->text)

Development,Projects,Snippets,WordPress,WordPress Filters

There has been an error with the API:
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->5->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->10->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->11->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->14->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->17->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->20->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->23->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->29->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->30->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->33->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->36->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->39->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->42->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->43->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->46->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->49->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->52->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->58->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->59->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->61->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->63->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->65->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->67->text)
INVALID_DOCUMENT - "text" was "" should be not (null or an empty string) (keyPath components->1->components->68->text)

That was the error code that greeted me when I attempted to publish a WordPress post to the Apple News feed using the Publish to Apple News plugin by Alley. Beauty, isn’t it?

Alley is a digital agency and their plugin is recommended by Apple for news publishers seeking a WordPress integration. Alley looks to be a digital agency and they likely developed this plugin for their own need. Support would be great, but I get that maintaining open source isn’t always exciting and they should be prioritizing their clients. Hopefully this post can help some of you.

The scourge of this error is widespread. At least 24 GitHub issues reported with varying reports of resolution and recurrence. A promising thread from the WordPress plugin dungeon support forum that ends in directing folks back to GitHub. It’s a known error that folks have been encountering… for about four years according to at least one myth.

For my project, the issue was the dreaded empty p tags (<p>&nbsp;</p>) along with some other tags that contained some still unknown whitespace character. First I’ll show you how I fixed my issue, then I’ll show you how I tracked it down and how you can figure out what’s stopping your posts from publishing successfully. Together, hopefully we can put an end to this ridiculous error once and for all!

The Solution

Solving my issue was actually quite simple. I just added this to my functions.php file (or wherever you put your theme functions):

function apple_news_remove_empty_p_tags($content, $post_id) {
$new_content = preg_replace('/<span[^>]*>[\s\040\t\r\n\W]*<\/span>/', '', $content);
$new_content = preg_replace('/<span[^>]*>&nbsp;*<\/span>/', '', $new_content);
$new_content = preg_replace('/<span[^>]*> <\/span>/', '', $new_content);
$new_content = preg_replace('/<p[^>]*>[\s\040\t\r\n\W]*<\/p>/', '', $new_content);
$new_content = preg_replace('/<p[^>]*>&nbsp;*<\/p>/', '', $new_content);
$new_content = preg_replace('/<p[^>]*> <\/p>/', '', $new_content);
return $new_content;
}
add_filter('apple_news_exporter_content', __NAMESPACE__.'\\apple_news_remove_empty_p_tags', 10, 2);

If My Fix Doesn’t Work For You

I’m not going to lie, if my fix above doesn’t work then this could get ugly. First, click the Apple News item in your admin and view the list of articles. Navigate to the one you encountered the error message on and click Download—make sure you’re not just downloading a random article, it needs to match up with the post you tried to publish. Download the JSON file and open it up in a good looking JSON viewer like VSCode or Code Beautify. If your JSON doesn’t even validate then you’ve got your work cut out for you. So, make sure your JSON validates.

Find the key/value pair that match the path of the error message.

My first error path was keyPath components->1->components->5->text

So that’s the topmost components key, then the second element in that key (index “1” but indices start at 0, so it’s the second element), then within that element we want the components key again, finally the sixth element within that key, and within that element we want the text property. Whew. Something in that key should be suspicious. It may be an &nbsp; or an empty tag. Whatever it is, hopefully you can use my filter function above and adjust it as needed to get the publish function to succeed. If you can’t then feel free to post a comment and have your post JSON ready to view on a Gist or pastebin.

Happy publishing!


Leave a Comment