SResok
2/8/2018 - 1:02 PM

Visual composer custom field shortcode

Visual composer custom field shortcode

[veld meta="meta_key"]
[veld meta="meta_key" post_id=1]
[veld before="<div class="test-before">" meta="meta_key" after="</div>" post_id=1]
add_shortcode('veld', 'shortcode_veld');
function shortcode_veld($atts){
    extract(shortcode_atts(array(
        'meta' => NULL,
        'post_id' => NULL,
        'before' => NULL,
        'after' => NULL
    ), $atts));
    if($meta === NULL) return;
    $field = esc_attr($meta);
    global $post;
    $post_id = (NULL === $post_id) ? $post->ID : $post_id;

    $output = '';

    if($before != NULL){
        $output .= $before;
    }

    $output .= get_post_meta($post_id, $field, true);

    if($after != NULL){
        $output .= $after;
    }
    return $output;
}