Awesome
CMB2: Field Link
Create a link field with some attributes. Very nice for styling links.
Properties ( fields )
Attributes included.
- href
- text
- class
- title
- rel
Usage
Create a metabox in the usual way.
Add a link field:
$cmb->add_field( [
'name' => __( 'Link Field', 'cmb2' ),
'desc' => __( 'Create a link with some attributes.', 'cmb2' ),
'id' => $prefix . 'link',
'type' => 'link',
//'repeatable' => true,
],
);
Accessing the link
Retrieve the post meta:
$link = get_post_meta( get_the_ID(), $prefix . 'link', true );
print_r( $link );
// Link Attributes
Array
(
[href] => https://wordpress.org/plugins/CMB2/
[text] => Download CMB2
[class] => awesome class
[rel] => nofollow
[title] => Click me to view CMB2
)
You can build your link like:
print '<a href="' . $link['href'] . '" class="' . $link['class'] . '" rel="' . $link['rel'] . '" title="' . $link['title'] . '">' . $link['text'] . '</a>';
It's a good idea to test for each property before just trying to print.