FavoriteLoadingAdd to favorites

Meta Box – WordPress Custom Fields Framework

The code below relates to the meta box plugin. And the code below relates to 2 custom post types. Players and Coaches. And then adding meta fields to those custom post types.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Custom Post 1
register_post_type( 'coaches',
array(
'labels' => array(
'name' => __( 'Coaches' ),
'singular_name' => __( 'Coach' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail')
)
);
register_taxonomy_for_object_type( 'category', 'coaches' );
// Custom Post 2
register_post_type( 'players',
array(
'labels' => array(
'name' => __( 'Players' ),
'singular_name' => __( 'Player' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail')
)
);
register_taxonomy_for_object_type( 'category', 'players' );
// Meta Fields
function add_meta_fields( $meta_boxes ) {
$meta_boxes[] = array(
'id' => 'twitter_url',
'title' => 'Twitter Url' ,
'post_types' => array( 'players','coaches' ),
'context' => 'normal',
'autosave' => false,
'fields' => array(
array(
'id' => 'twitter_url',
'type' => 'url',
'name' => 'URL',
),
),
);
$meta_boxes[] = array(
'id' => 'instagram_url',
'title' => 'Instagram Url' ,
'post_types' => array( 'players','coaches' ),
'context' => 'normal',
'autosave' => false,
'fields' => array(
array(
'id' => 'instagram_url',
'type' => 'url',
'name' => 'URL',
),
),
);
$meta_boxes[] = array(
'id' => 'facebook_url',
'title' => 'Facebook Url' ,
'post_types' => array( 'players','coaches' ),
'context' => 'normal',
'autosave' => false,
'fields' => array(
array(
'id' => 'facebook_url',
'type' => 'url',
'name' => 'URL',
),
),
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'add_meta_fields' );
// Custom Post 1 register_post_type( 'coaches', array( 'labels' => array( 'name' => __( 'Coaches' ), 'singular_name' => __( 'Coach' ) ), 'public' => true, 'has_archive' => true, 'supports' => array('title', 'editor', 'thumbnail') ) ); register_taxonomy_for_object_type( 'category', 'coaches' ); // Custom Post 2 register_post_type( 'players', array( 'labels' => array( 'name' => __( 'Players' ), 'singular_name' => __( 'Player' ) ), 'public' => true, 'has_archive' => true, 'supports' => array('title', 'editor', 'thumbnail') ) ); register_taxonomy_for_object_type( 'category', 'players' ); // Meta Fields function add_meta_fields( $meta_boxes ) { $meta_boxes[] = array( 'id' => 'twitter_url', 'title' => 'Twitter Url' , 'post_types' => array( 'players','coaches' ), 'context' => 'normal', 'autosave' => false, 'fields' => array( array( 'id' => 'twitter_url', 'type' => 'url', 'name' => 'URL', ), ), ); $meta_boxes[] = array( 'id' => 'instagram_url', 'title' => 'Instagram Url' , 'post_types' => array( 'players','coaches' ), 'context' => 'normal', 'autosave' => false, 'fields' => array( array( 'id' => 'instagram_url', 'type' => 'url', 'name' => 'URL', ), ), ); $meta_boxes[] = array( 'id' => 'facebook_url', 'title' => 'Facebook Url' , 'post_types' => array( 'players','coaches' ), 'context' => 'normal', 'autosave' => false, 'fields' => array( array( 'id' => 'facebook_url', 'type' => 'url', 'name' => 'URL', ), ), ); return $meta_boxes; } add_filter( 'rwmb_meta_boxes', 'add_meta_fields' );
// Custom Post 1
      register_post_type( 'coaches',
          array(
              'labels' => array(
                  'name' => __( 'Coaches' ),
                  'singular_name' => __( 'Coach' )
              ),
          'public' => true,
          'has_archive' => true,
          'supports' => array('title', 'editor', 'thumbnail')
          )
      );
    register_taxonomy_for_object_type( 'category', 'coaches' );
// Custom Post 2
      register_post_type( 'players',
          array(
              'labels' => array(
                  'name' => __( 'Players' ),
                  'singular_name' => __( 'Player' )
              ),
            'public' => true,
            'has_archive' => true,
            'supports' => array('title', 'editor', 'thumbnail')
          )
      );
    register_taxonomy_for_object_type( 'category', 'players' );

// Meta Fields
  function add_meta_fields( $meta_boxes ) {
    $meta_boxes[] = array(
      'id' => 'twitter_url',
      'title' => 'Twitter Url' ,
      'post_types' => array( 'players','coaches' ),
      'context' => 'normal',
      'autosave' => false,
      'fields' => array(
        array(
          'id' => 'twitter_url',
          'type' => 'url',
          'name' => 'URL',
        ),
      ),
    );
    $meta_boxes[] = array(
      'id' => 'instagram_url',
      'title' => 'Instagram Url' ,
      'post_types' => array( 'players','coaches' ),
      'context' => 'normal',
      'autosave' => false,
      'fields' => array(
        array(
          'id' => 'instagram_url',
          'type' => 'url',
          'name' => 'URL',
        ),
      ),
    );
    $meta_boxes[] = array(
      'id' => 'facebook_url',
      'title' => 'Facebook Url' ,
      'post_types' => array( 'players','coaches' ),
      'context' => 'normal',
      'autosave' => false,
      'fields' => array(
        array(
          'id' => 'facebook_url',
          'type' => 'url',
          'name' => 'URL',
        ),
      ),
    );

    return $meta_boxes;
  }
  add_filter( 'rwmb_meta_boxes', 'add_meta_fields' );

Below is how to add a location/Address field

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Address
$meta_boxes[] = array(
'id' => 'address',
'title' => 'Address' ,
'post_types' => array( 'countries' ),
'context' => 'normal',
'autosave' => false,
'fields' => array(
array(
'id' => 'address',
'name' => 'Address',
'type' => 'text',
),
array(
'id' => 'map',
'name' => 'Location',
'type' => 'map',
// Default location: 'latitude,longitude[,zoom]' (zoom is optional)
'std' => '-6.233406,-35.049906,15',
// Address field ID
'address_field' => 'address',
// Google API key
'api_key' => 'AIzaSyBVKag0eBXXIjS0rvi6qUYZ9sScw5kNeXQ',
),
),
);
// Address $meta_boxes[] = array( 'id' => 'address', 'title' => 'Address' , 'post_types' => array( 'countries' ), 'context' => 'normal', 'autosave' => false, 'fields' => array( array( 'id' => 'address', 'name' => 'Address', 'type' => 'text', ), array( 'id' => 'map', 'name' => 'Location', 'type' => 'map', // Default location: 'latitude,longitude[,zoom]' (zoom is optional) 'std' => '-6.233406,-35.049906,15', // Address field ID 'address_field' => 'address', // Google API key 'api_key' => 'AIzaSyBVKag0eBXXIjS0rvi6qUYZ9sScw5kNeXQ', ), ), );
// Address
    $meta_boxes[] = array(
      'id' => 'address',
      'title' => 'Address' ,
      'post_types' => array( 'countries' ),
      'context' => 'normal',
      'autosave' => false,
      'fields' => array(
        array(
            'id'   => 'address',
            'name' => 'Address',
            'type' => 'text',
        ),
        array(
            'id'            => 'map',
            'name'          => 'Location',
            'type'          => 'map',

            // Default location: 'latitude,longitude[,zoom]' (zoom is optional)
            'std'           => '-6.233406,-35.049906,15',

            // Address field ID
            'address_field' => 'address',

            // Google API key
            'api_key'       => 'AIzaSyBVKag0eBXXIjS0rvi6qUYZ9sScw5kNeXQ',
        ),
      ),
    );

 

Pin It on Pinterest

Garth Baker
Favorite
Loading
en
af
sq
am
ar
hy
az
eu
be
bn
bs
bg
ca
ceb
ny
zh-CN
zh-TW
co
hr
cs
da
nl
en
eo
et
tl
fi
fr
fy
gl
ka
de
el
gu
ht
ha
haw
iw
hi
hmn
hu
is
ig
id
ga
it
ja
jw
kn
kk
km
ko
ku
ky
lo
la
lv
lt
lb
mk
mg
ms
ml
mt
mi
mr
mn
my
ne
no
ps
fa
pl
pt
pa
ro
ru
sm
gd
sr
st
sn
sd
si
sk
sl
so
es
su
sw
sv
tg
ta
te
th
tr
uk
ur
uz
vi
cy
xh
yi
yo
zu
Share This