Skip to content

Commit

Permalink
- support custom metaboxes by allowing user to input their own ID
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusforsberg committed Sep 17, 2018
1 parent c3af2a4 commit f01c3ee
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
42 changes: 31 additions & 11 deletions acf-native-field-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,44 @@ function render_field_settings($field) {
'instructions' => __('The native WordPress field to move into this placeholder.', 'acf-native-fields'),
'type' => 'select',
'name' => 'native_field',
'required' => 1,
// TODO: Implement backend and frontend functionality for custom native fields (hooks)
'choices' => array(
'content' => __('Content Editor', 'acf-native-fields'),
'excerpt' => __('Excerpt', 'acf-native-fields'),
'featured_image' => __('Featured Image', 'acf-native-fields'),
'yoast_seo' => __('Yoast SEO', 'acf-native-fields'),
'publish_box' => __('Publish Box', 'acf-native-fields'),
'permalink' => __('Permalink', 'acf-native-fields'),
'discussion' => __('Discussion', 'acf-native-fields'),
'trackbacks' => __('Trackbacks', 'acf-native-fields'),
'format' => __('Format', 'acf-native-fields'),
'page_attributes'=> __('Page Attributes', 'acf-native-fields'),
'content' => __('Content Editor', 'acf-native-fields'),
'excerpt' => __('Excerpt', 'acf-native-fields'),
'featured_image' => __('Featured Image', 'acf-native-fields'),
'yoast_seo' => __('Yoast SEO', 'acf-native-fields'),
'publish_box' => __('Publish Box', 'acf-native-fields'),
'permalink' => __('Permalink', 'acf-native-fields'),
'discussion' => __('Discussion', 'acf-native-fields'),
'trackbacks' => __('Trackbacks', 'acf-native-fields'),
'format' => __('Format', 'acf-native-fields'),
'page_attributes' => __('Page Attributes', 'acf-native-fields'),
'custom' => __('Custom', 'acf-native-fields'),
),
));

acf_render_field_setting($field, array(
'label' => __('Custom Meta Box ID', 'acf-native-fields'),
'instructions' => __('The ID of the custom metabox to target.', 'acf-native-fields'),
'type' => 'text',
'name' => 'metabox_id',
'prefix' => '#',
'required' => 1,
'conditional_logic' => array(
array(
array(
'field' => 'native_field',
'operator' => '==',
'value' => 'custom',
),
),
),
));
}

function render_field($field) {?>
<div class="acf-native-field" data-native-field="<?php echo $field['native_field']; ?>">
<div class="acf-native-field" data-native-field="<?php echo esc_attr($field['native_field']); ?>"<?php echo (!empty($field['metabox_id']) ? ' data-metabox-id="' . esc_attr($field['metabox_id']) . '"' : ''); ?>>
<?php _e('Loading...', 'acf-native-fields'); ?>
</div><?php
}
Expand Down
9 changes: 8 additions & 1 deletion js/acf-native-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

// First try to find a built-in method to run for this type of native field
if(typeof ACF_Native_Fields['moveNativeField_' + native_field_type] === 'function') {
native_field_placeholder.append(ACF_Native_Fields['moveNativeField_' + native_field_type]());
native_field_placeholder.append(ACF_Native_Fields['moveNativeField_' + native_field_type](native_field_placeholder));
// TODO: Allow custom callback code to be added in field group settings and executed here?
}
// If none exists, see if a custom one has been passed, and exists
Expand Down Expand Up @@ -145,6 +145,13 @@
moveNativeField_page_attributes: function() {
return ACF_Native_Fields.getNativeFieldElement('#pageparentdiv');
},

/**
* ACF Native Field type: custom
*/
moveNativeField_custom: function (native_field_placeholder) {
return ACF_Native_Fields.getNativeFieldElement('#' + native_field_placeholder.data('metabox-id'));
},
};

$(document).ready(ACF_Native_Fields.init);
Expand Down

0 comments on commit f01c3ee

Please sign in to comment.