layers_custom_meta()
This filter allows you to add additional meta fields to the Layers meta box in standard posts. Each meta field references an input() type from the Layers framework. You can also create a custom meta box the standard way, and use Layers input() types.
Location: core/meta/config.php
Usage:
Themes:
1 |
add_filter( 'layers_custom_meta', 'yourtheme_custom_meta' ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
function yourtheme_custom_meta( $custom_meta ){ $custom_meta['post'] = array( 'title' => __(' Your Theme Options' , 'layerswp' ), 'description' => __( 'This is some helper text.' , 'layerswp' ), 'position' => 'normal', 'custom-meta' => array( 'media' => array( 'title' => __( 'Rich Media' , 'layerswp' ), 'description' => __( 'This is some helper text.' , 'layerswp' ), 'elements' => array( 'video-url' => array( 'label' => __( 'Video URL' , 'layerswp' ), 'description' => __( 'For use with <a href="' . esc_url( 'http://codex.wordpress.org/' ) . 'Embeds" target="_blank">oEmbed</a> supported media' , 'layerswp' ), 'type' => 'text', ), 'client-name' => array( 'label' => __( 'Client Name' , 'layerswp' ), 'description' => __( 'Add the link text' , 'layerswp' ), 'type' => 'text', ), 'client-url' => array( 'label' => __( 'Client URL' , 'layerswp' ), 'description' => __( 'Add the link url' , 'layerswp' ), 'type' => 'text', ) ) ), ) // End cutom-meta ); return $custom_meta; } |
Line 2: We define the post type here as ‘post’ to affect the existing meta box. If you are targeting a custom post type, this is where you would define it.
Line 3: The initial $custom_meta array defines the meta box Title, which you can change in the ‘title’ value. The next two keys, ‘description’ and ‘position’ should be left as-is.
‘custom-meta’ next defines the meta box content. Layers uses the ‘media’ key for this group of fields.
The first field, or ‘element’, is the ‘video_url’ field. This array should be carried over to preserve the oembed video field and featured video functionality of Layers.
When filtering $custom_meta to add fields to the existing Rich Media box in posts, it is important to preserve the video_url field so you don’t break feature video in posts on user’s existing setups.
Your ‘custom-meta’ arrays can be inserted after or before the ‘video_url’ array. In the example, we insert a new field 'client_name' and 'client_url' below the Video URL
1 2 3 4 5 6 7 8 9 10 11 12 |
'video-url' => array( 'label' => __( 'Video URL' , 'layerswp' ), 'type' => 'text', ), 'client_name' => array( 'label' => __( 'Client Name' , 'layerswp' ), 'type' => 'text', ), 'client_url' => array( 'label' => __( 'Client URL' , 'layerswp' ), 'type' => 'text', ) |
The 'type' of fields you can add correspond to types found in the input() reference.
Plugins:
The method is much the same, you just need to instance your filter and use a public function:
1 |
add_filter( 'layers_custom_meta' , array( $this , 'yourtheme_add_post_meta' ) ); |
1 2 3 4 5 |
public function yourtheme_add_post_meta( $custom_meta ){ // your meta array } |
Displaying on the Frontend
Use get_post_meta() as normal from inside the loop. Using our example above, the following demonstrates output of the client name value only:
1 2 3 4 5 |
$client-name = get_post_meta( get_the_ID(), 'client-name', true ); // Check if the custom field has a value before echoing. if ( ! empty( $client-name ) ) { ?> <div class="client-name"><?php echo $client-name; ?></div> <?php } |
In this example, we retrieve all post meta from this post, then use the key
1 2 3 4 5 6 7 8 9 10 11 12 |
$custom_meta = get_post_meta( get_the_ID(), 'layers', true ); <?php if( !empty( $post_meta ) ) { ?> <?php if( isset( $post_meta[ 'client_url' ] ) ) { ?> <label><?php _e( 'Client', 'layerswp' ); ?></label> <?php echo $post_meta[ 'client_url' ]; ?> <?php } ?> <?php if( isset( $post_meta[ 'client_name' ] ) ) { ?> <label><?php _e( 'Website', 'layerswp' ); ?></label> <?php echo $post_meta[ 'website' ]; ?> <?php } ?> <?php }?> |