Using fields

The Beans Fields API component offers a range of fields which can be used in the WordPress admin as Options, Post Meta, Term Meta or WP Customizer Options.

Options Fields

Fields can be attached to an existing option page by hooking a callback function to admin_init and using beans_register_options().

add_action( 'admin_init', 'my_function_name' );

function my_function_name() {

 $fields = array(
     array(
     'id' => 'field_id',
     'label' => 'Field label',
     'type' => 'text',
     'default' => ''
   ),
    array(
      'id' => 'field_id',
     'label' => 'Field label',
     'type' => 'textarea',
     'default' => ''
   ),
    // ...
  );

  beans_register_options( $fields, 'menu_slug', 'section_id', array( 'title' => 'Metabox title' ) );

}

Fields values can easily be retrieved in the front-end using the WordPress core get_option() function.

get_option( 'field_id', 'default_value' );

Post Meta Fields

Fields can be attached to one or multiple post types by hooking a callback function to admin_init and using beans_register_post_meta().

add_action( 'admin_init', 'my_function_name' );

function my_function_name() {

  $fields = array(
     array(
     'id' => 'field_id',
     'label' => 'Field label',
     'type' => 'text',
     'default' => ''
   ),
    array(
      'id' => 'field_id',
     'label' => 'Field label',
     'type' => 'textarea',
     'default' => ''
   ),
    // ...
  );

  beans_register_post_meta( $fields, array( 'post_type' ), 'section_id', array( 'title' => 'Metabox title' ) );

}

Fields values can easily be retrieved in the front-end using beans_get_post_meta() which is a shortcut of WordPress core get_post_meta() function.

beans_get_post_meta( 'field_id', 'default_value' );

Term Meta Fields

Fields can be attached to one or multiple taxonomies by hooking a callback function to admin_init and using beans_register_term_meta().

add_action( 'admin_init', 'my_function_name' );

function my_function_name() {

 $fields = array(
     array(
     'id' => 'field_id',
     'label' => 'Field label',
     'type' => 'text',
     'default' => ''
   ),
    array(
      'id' => 'field_id',
     'label' => 'Field label',
     'type' => 'textarea',
     'default' => ''
   ),
    // ...
  );

  beans_register_term_meta( $fields, array( 'taxonomy' ), 'section_id' );

}

Fields values can easily be retrieved in the front-end using beans_get_term_meta().

beans_get_term_meta( 'field_id', 'default_value' );

Note: Fields attached to a taxonomy are only visible on the edit term page.

Customizer Fields

Fields can be attached to WordPress theme customizer by hooking a callback function to customize_register and using beans_register_wp_customize_options().

add_action( 'customize_register', 'my_function_name' );

function my_function_name() {

 $fields = array(
     array(
     'id' => 'field_id',
     'label' => 'Field label',
     'type' => 'text',
     'default' => ''
   ),
    array(
      'id' => 'field_id',
     'label' => 'Field label',
     'type' => 'textarea',
     'default' => ''
   ),
    // ...
  );

  beans_register_wp_customize_options( $fields, 'section_id', array( 'title' => 'Controller title' ) );

}

Fields values can easily be retrieved in the front-end using the WordPress core get_theme_mod() function.

get_theme_mod( 'field_id', 'default_value' );