Home › Forums › Backend Issues (wp-admin) › Supply a foreach to ACF Select Field
So I have the form
bit of a WP Core Widgets class that I am working on and I’m stuck on wanting to supply a list that’s already populated to a Advanced Custom Field ‘select’ field.
Instead of lists
, how can I supply and save the drop-down results to acf_lists
?
// This is the backend of the widgets
public function form($instance)
{
// Grab the widget ID
$widget_id = $this->id;
$acf_lists = get_field('mailchimp_list', $widget_id) ? get_field('mailchimp_list',$widget_id) : '';
var_dump($acf_lists);
// Grab the MailChimp_API getters and setters
$api = mailchimpSF_get_api();
// If there is no API, return
if (!$api) {
return;
}
// If there is an API Key, proceed
if ($api) {
$lists = $api->get(
'lists',
100,
[
'fields' => 'lists.id,lists.name,lists.email_type_option'
]
);
$lists = $lists['lists'];
// If there are no lists present
if (count($lists) == 0) {
?>
<span class='error_msg'>
<?php
echo sprintf(
esc_html("Uh-oh, you don't have any lists defined! Please visit %s, login, and setup a list before using this tool!"),
"<a href='http://www.mailchimp.com/'>MailChimp</a>"
);
?>
</span>
<?php
} else {
?>
<table class="mc-list-select">
<tr class="mc-list-row">
<td>
<select name="mc_list_id">
<option value=""> — <?= 'Select A List' ?>
—
</option>
<?php
foreach ($lists as $list) {
$option = get_option('mc_list_id');
?>
<option value="<?= $list['id'] ?>"<?= selected($list['id'],
$option); ?>><?= $list['name'] ?></option>
<?php
}
?>
</select>
</td>
<td>
<input type="hidden" name="mcsf_action"
value="update_mc_list_id"/>
<input type="submit" name="Submit"
value="<?= 'Update List' ?>" class="button"/>
</td>
</tr>
</table>
<?php
}
}
}
Here is what the widget looks like:
[![enter image description here][1]][1]
How can I supply the current foreach that I have into the $acf_lists variable? I’m able to loop through and supply it to a standard drop-down but I’m unsure how I can supply it to the ACF dropdown.
This is my ACF Mailchimp list select field:
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/M7FNX.png
[2]: https://i.stack.imgur.com/0Xukh.png
The topic ‘Supply a foreach to ACF Select Field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.