I'm trying to pull the alt tag from an image from an xml node titled <description>. Here is the content of the node <img src="xxx.png" alt="WHAT I WANT TO GRAB">. I've tried to create a php function to extract that but it's not working. Where am I going wrong? For reference here is the xml feed.
What I'm using to set Alt tag in the import:
[get_alt_tags_from_xml({description[1]})]
What I'm using for a function:
function get_alt_tags_from_xml($content) {
//The content
$html = file_get_html($content);
//Run on all images
foreach($html->find('img') as $element)
echo $element->alt . ', ';
}
Function
Consider using
DOMDocumentandDOMXPath. Load html and findimgtags with xpath, then extract all attributes. It would be better to usePHP_EOLinstead of,to join the values.The function should support extracting both
srcandaltattributes to be able to provide two lists of the same size representing the images. WP All Import will use that information as an image data source and for alternative text field population.Template
The
content_encodeditem node from Simplifyingthemarket feed is preferable as it contains more images thandescriptiontag.Under
Imagessection of WP All Import's Edit Template page:Download images hosted elsewhereshould be selected and contain the source list:Enter image URL one per line, or separate them with amust be blank.Scan through post content and import images wrapped in <img> tagsmust be enabled.SEO & Advanced Options->Set Alt Text(s)should be selected and provide the alternative text list:Enter one per line, or separate them with amust be blank.Once these settings are saved the import can be started.
Test
The attribute extraction function can be tested outside of an import: