Skip to content

Instantly share code, notes, and snippets.

@gemmadlou
Last active October 17, 2016 14:18
Show Gist options
  • Select an option

  • Save gemmadlou/24570e470b77d02004e66283b4c91321 to your computer and use it in GitHub Desktop.

Select an option

Save gemmadlou/24570e470b77d02004e66283b4c91321 to your computer and use it in GitHub Desktop.
ACF Importing Children XML Into Custom Fields
<page>
<post>
<Panel>
<Name>Gemma</Name>
<PanelImage>
http://unsplash.it/400/500
</PanelImage>
<ID>1</ID>
</Panel>
<Panel>
<Name>Walle</Name>
<PanelImage>
http://unsplash.it/400/500
</PanelImage>
<ID>1</ID>
</Panel>
</post>
<post>
<Panel>
<Name>Finnick</Name>
<PanelImage>
http://unsplash.it/400/500
</PanelImage>
<ID>2</ID>
</Panel>
<Panel>
<Name>Emily</Name>
<PanelImage>
http://unsplash.it/400/500
</PanelImage>
<ID>2</ID>
</Panel>
</post>
</page>
<?php
add_action('pmxi_update_post_meta', 'update_panels', 10, 3);
function update_panels($pid, $m_key, $m_value) {
if ( $m_key == 'import_unprocessed') {
$arr = preg_split('/\n|\r\n?/', $m_value);
update_post_meta($pid, 'test', count($arr));
for ($i = 0; $i < (count($arr) - 1); $i++) {
$col = explode(',', $arr[$i]);
update_post_meta($pid, 'name_'.$i, $col[0]);
update_post_meta($pid, 'image_'.$i, $col[1]);
}
}
}
// Alternative Example For Flexible Content
add_action('pmxi_update_post_meta', 'update_panels', 10, 3);
function update_panels($pid, $m_key, $m_value) {
$updatable_actions = [];
if ( $m_key == 'import_panels') {
$panelarr = [];
$arr = explode('***LINE***', $m_value);
update_post_meta($pid, 'test', count($arr));
for ($i = 0; $i < (count($arr) - 1); $i++) {
$col = explode('***,***', $arr[$i]);
$panelarr[] = 'field_btm_panel';
array_push($updatable_actions, [ 'key' => 'field_btm_content_layout_'.$i.'_field_btm_panel_image', 'value' => $col[0] ]);
array_push($updatable_actions, [ 'key' => '_field_btm_content_layout_'.$i.'_field_btm_panel_image', 'value' => 'field_btm_panel_image' ]);
array_push($updatable_actions, [ 'key' => 'field_btm_content_layout_'.$i.'_field_btm_panel_content', 'value' => $col[1] ]);
array_push($updatable_actions, [ 'key' => '_field_btm_content_layout_'.$i.'_field_btm_panel_content', 'value' => 'field_btm_panel_content' ]);
}
array_push($updatable_actions, [ 'key' => 'field_btm_content_layout', 'value' => serialize($panelarr) ]);
array_push($updatable_actions, [ 'key' => '_field_btm_content_layout', 'value' => 'field_btm_content_layout' ]);
}
foreach ($updatable_actions as $action) {
update_post_meta($pid, $action['key'], $action['value']);
}
}

Add the following to a custom field that you use just for importing. You can call it whatever you like. Then, once the import is complete, you can hook into the pmxi_update_post_meta action - and process the imports as you like.

It is a bit reminiscent of what you would do in hackerrank.

[FOREACH({Panel})]
{Name},{PanelImage}
[ENDFOREACH]

[FOREACH({Panel})]
{Name}***,***{PanelImage}***LINE***
[ENDFOREACH]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment