Skip to content

Instantly share code, notes, and snippets.

@jrstaatsiii
Last active October 3, 2015 21:52
Show Gist options
  • Save jrstaatsiii/62fa508fabef735594fc to your computer and use it in GitHub Desktop.
Save jrstaatsiii/62fa508fabef735594fc to your computer and use it in GitHub Desktop.
Attempting to insert options into a content block, but only if that content block was selected.
<?php
/**
* Load default choices from options page into content blocks
*/
function acf_load_style_default_choices( $field ) {
// reset choices
$field['choices'] = array();
// if has rows
if( have_rows('additional_styles', 'option') ) {
// while has rows
while( have_rows('additional_styles', 'option') ) {
// instantiate row
the_row();
// vars
$value = get_sub_field('value');
$label = get_sub_field('label');
// get the options chosen from the checkboxes
$cb_options = get_sub_field('associated_content_blocks');
// get the current content block
$cb = get_row_layout();
// replace the spaces with underscores, make lowecase
$cb_formatted = strtolower(str_replace(' ', '_', $cb));
// check to see if the current content block is within the cb_options array
if ( in_array($cb_formatted, $cb_options) ) {
// add to options for that content block
$field['choices'][ $value ] = $label;
}
}
}
// return the field
return $field;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment