Home › Forums › General Issues › Adding fields in taxonomies via php
Hi, I started using acf via TGM Plugin activation and Im registering the fields in this way for multiple post types.
if( !defined('ABSPATH') ) exit;
if( !class_exists('Awpt_Metaboxes') ){
class Awpt_Metaboxes {
function __construct() {
add_action('admin_init', array($this,'video_metabox'));
add_filter( 'awptx_video_type' , array( $this, 'video_type' ), 20, 1);
add_filter( 'awptx_video_object' , array( $this, 'video_object' ), 20, 1 );
}
function video_type( $post_id ){
if( !isset( $post_id ) || get_post_type( $post_id ) != 'video' )
return;
// first check the File field.
$type = get_post_meta( $post_id, 'video_file', true ) ? true : false;
if( $type == true ){
return 'files';
}
return 'normal';
}
function video_object($post_id){
if( !isset( $post_id ) || get_post_type( $post_id ) != 'video' )
return;
$video_object = get_post_meta( $post_id, 'video_frame', true ) ? get_post_meta( $post_id, 'video_frame', true ) : null;
return get_post_meta( $post_id, 'video_url', true ) ? get_post_meta( $post_id, 'video_url', true ) : $video_object;
}
function video_metabox(){
global $wp_post_types;
$exclude_pt = array('revision','nav_menu_item','acf','attachment','deprecated_log','page');
$post_id = isset( $_REQUEST['post'] ) ? (int)$_REQUEST['post'] : null;
//$custom_template = isset( $_REQUEST['post'] ) ? (int)$_REQUEST['post'] : null;
$template_file = get_post_meta($post_id, '_wp_page_template', TRUE);
$post_type_array = array();
foreach ( $wp_post_types as $pt ) {
if( !empty( $pt->name ) && !in_array( $pt->name, $exclude_pt ) ){
$post_type_array[ $pt->name ] = $pt->label;
}
}
//$categories_page = get_pages( array('meta_key' => '_wp_page_template','meta_value' => 'template-categories.php') );
if ($template_file == 'template-categories.php' || $template_file == 'template-performers.php' || $template_file == 'template-photo-gallery.php' || $template_file == 'template-blog.php' || $template_file == 'template-channel-list.php' || $template_file == 'template-tags.php' || $template_file == 'template-home.php') {
$layouts = array(
'0side-cm' => __('No Sidebars'),
'1side-cl' => __('1 Sidebar - Content Left'),
'1side-cr' => __('1 Sidebar - Content Right'),
);
} else {
$layouts = array(
'0side-cm' => __('No Sidebars'),
'1side-cl' => __('1 Sidebar - Content Left'),
'1side-cr' => __('1 Sidebar - Content Right'),
'2side-cl' => __('2 Sidebars - Content Center'),
);
}
$page_fields = array (
'id' => 'acf_page_settings',
'title' => 'Layout Settings',
'fields' => array (
array (
'key' => 'field_535765',
'label' => __('Sidebar Position','awpt'),
'name' => 'sidebar_position',
'type' => 'select',
'instructions' => __('This key will give you possibilities to change sidebar position of this page.','awpt'),
'choices' => $layouts,
'default_value' => '1side-cr',
'allow_null' => 0,
'multiple' => 0,
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'acf_after_title',
'layout' => 'default',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
);
$gallery_fields = array (
'id' => 'acf_gallery_settings',
'title' => 'Awpt - Photo Gallery',
'fields' => array (
array (
'key' => 'field_53eb79f33936e678',
'label' => __('Upload your files here','easytube'),
'instructions' => __('Press and hold SHIFT to select multiple images in media library.','awpt'),
'name' => 'gallery_files',
'type' => 'gallery',
'conditional_logic' => array (
'status' => 1,
'allorany' => 'all',
),
'preview_size' => 'thumbnail',
'library' => 'all',
),
array (
'key' => 'sponsor_link_txt',
'label' => 'Sponsor Text',
'name' => 'sponsor_link_txt',
'type' => 'text',
'prefix' => '',
'required' => 0,
'placeholder' => 'Sponsor Text!',
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'key' => 'sponsor_link_url',
'label' => 'Sponsor URL',
'name' => 'sponsor_link_url',
'type' => 'text',
'prefix' => '',
'placeholder' => 'http://exapmple-url.com',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'key' => 'awpt_adv',
'label' => 'Horizontal AD Banner',
'instructions' => __('This ads code appears under the video.','easytube'),
'name' => 'awpt_adv',
'type' => 'textarea',
'prefix' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => 'awpt_adv_field',
'id' => '',
),
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'gallery',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'acf_after_title',
'layout' => 'default',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
);
$video_fields = array (
'id' => 'acf_video',
'title' => 'Video',
'fields' => array (
array (
'key' => 'field_53eb79f33936e',
'label' => __('Choose the Video type','easytube'),
'name' => 'video_type',
'type' => 'select',
'choices' => array (
'normal' => 'Link/iFrame Code',
'files' => 'Files',
),
'default_value' => isset( $_GET['post'] ) ? apply_filters( 'awptx_video_type' , $_GET['post']) : 'normal',
'allow_null' => 0,
'multiple' => 0,
),
array (
'key' => 'field_53eb7a453936f',
'label' => __('Enter the link(youtube,vimeo) or embed code here(other sources).','easytube'),
'name' => 'video_url',
'type' => 'textarea',
'instructions' => __('Here you can put multiple video object, one object/line.','easytube'),
'conditional_logic' => array (
'status' => 1,
'rules' => array (
array (
'field' => 'field_53eb79f33936e',
'operator' => '==',
'value' => 'normal',
),
),
'allorany' => 'all',
),
'default_value' => '',
'value' => isset( $_GET['post'] ) ? apply_filters( 'awptx_video_object' , $_GET['post']) : null,
'placeholder' => '',
'maxlength' => '',
'rows' => '',
'formatting' => 'none',
),
array (
'key' => 'field_53eb7aae942ae',
'label' => __('Upload your files here','easytube'),
'name' => 'video_file',
'type' => 'gallery',
'conditional_logic' => array (
'status' => 1,
'rules' => array (
array (
'field' => 'field_53eb79f33936e',
'operator' => '==',
'value' => 'files',
),
),
'allorany' => 'all',
),
'preview_size' => 'thumbnail',
'library' => 'all',
),
array (
'key' => 'video_description',
'label' => 'Video Description',
'instructions' => __('Write a description for this video.','easytube'),
'name' => 'video_description',
'type' => 'textarea',
'prefix' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => 'video_description_field',
'id' => '',
),
),
array (
'key' => 'sponsor_link_txt',
'label' => 'Sponsor Text',
'name' => 'sponsor_link_txt',
'type' => 'text',
'prefix' => '',
'required' => 0,
'placeholder' => 'Watch Full Movie!',
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'key' => 'sponsor_link_url',
'label' => 'Sponsor URL',
'name' => 'sponsor_link_url',
'type' => 'text',
'prefix' => '',
'placeholder' => 'http://exapmple-url.com',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'key' => 'video_duration',
'label' => 'Video Duration',
'name' => 'video_duration',
'type' => 'text',
'prefix' => '',
'required' => 0,
'conditional_logic' => 0,
'placeholder' => '03:45',
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'key' => 'awpt_adv',
'label' => 'Horizontal AD Banner',
'instructions' => __('This ads code appears under the video.','easytube'),
'name' => 'awpt_adv',
'type' => 'textarea',
'prefix' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => 'awpt_adv_field',
'id' => '',
),
),
array (
'key' => 'field_53eb7aae942aes89',
'label' => __('Video Thumbnails (Preview Images)','easytube'),
'instructions' => __('Use this gallery for thumbnails rotation.','easytube'),
'name' => 'image_rotator',
'type' => 'gallery',
'conditional_logic' => array (
'status' => 0,
'allorany' => 'all',
),
'preview_size' => 'thumbnail',
'library' => 'all',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'acf_after_title',
'layout' => 'default',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
);
$performer_fields = array (
'id' => 'acf_performer_settings',
'title' => 'Awpt - Performer Fields',
'fields' => array (
array (
'key' => 'sample_performer_key',
'label' => 'Horizontal AD Banner',
'instructions' => __('This ads code appears under the video.','easytube'),
'name' => 'sample_performer_key_124',
'type' => 'textarea',
'prefix' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => 'sample_performer_field',
'id' => '',
),
),
),
'location' => array (
array (
array (
'param' => 'taxonomy',
'operator' => '==',
'value' => 'performer',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'acf_after_title',
'layout' => 'default',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
);
if(function_exists("register_field_group")){
register_field_group( apply_filters( 'awpt_video_meta_fields_args' , $video_fields ) );
register_field_group( apply_filters( 'awpt_page_meta_fields_args' , $page_fields ) );
register_field_group( apply_filters( 'awpt_gallery_meta_fields_args' , $gallery_fields ) );
register_field_group( apply_filters( 'awpt_performer_meta_fields_args' , $performer_fields ) );
}
}
}
new Awpt_Metaboxes();
}
So, everything works fine except the performer fields I can’t get these fields in the taxonomy called performer, Im doing anything wrong here?
Would be great if you can guide on this class.The post types are working fine I can get all the fields
You need to alter the location rules of the field group, this part
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
'order_no' => 0,
'group_no' => 0,
),
),
),
The best advice I can give you is to set up a field group in the admin, set the location rules correctly and then export that field group to get an example of what the location rules need to be set to.
Additional problems, all groups must have a group key that starts with “group_” and I didn’t look at every one of your fields but field must have a field key that starts with “field_”.
Group “ID” is irrelevant when registering field groups through PHP.
You must be logged in to reply to this topic.
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
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.