14

I am trying to echo visual composer shortcodes onto a page.

I've tried both methods below, but they don't work:

functions.php:

Method 1

/*
 * add shortcode file
 */
function include_file($atts) {
    $a = shortcode_atts( array(
        'slug' => 'NULL',
    ), $atts );

    if($slug != 'NULL'){
        ob_start();
        get_template_part($a['slug']);
        return ob_get_clean();
    }
}
add_shortcode('include', 'include_file');

Method 2

function someshortocode_callback( $atts = array(), $content = null ) {

    $output = "[vc_section full_width=\"stretch_row\" css=\".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}\"][vc_row 0=\"\"][vc_column offset=\"vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6\"][/vc_column][/vc_row][/vc_section]";
    return $output;
}
add_shortcode('someshortocode', 'someshortocode_callback');

file_rendering_vc_shortcodes.php:

Method 1

<?php if ( is_plugin_active( 'js_composer/js_composer.php' ) ) {
    wc_print_notice('js_composer plugin ACTIVE', 'notice');
    echo do_shortcode('[include slug="vc_templates/shop-page"]');
}; ?>

Result

  • js_composer plugin ACTIVE
  • shortcode is on page with parentheses as is

Method 2

<?php $post = get_post();

if ( $post && preg_match( '/vc_row/', $post->post_content ) ) {
// Visual composer works on current page/post
    wc_print_notice('VC ON', 'notice');
    echo add_shortcode('someshortocode', 'someshortocode_callback');
} else {
    wc_print_notice('VC OFF', 'notice');
    //echo do_shortcode('[include slug="vc_templates/shop-page"]');
}; ?>

Result

  • VC OFF (obviously, since the vc_row in shortcode is not there)
  • shortcode is NOT on page

shop-page.php

<?php
/**
Template Name:  Shop Page in theme
Preview Image:  #
Descriptions:   #
 * [vc_row][vc_column][/vc_column][/vc_row]
 */
?>
[vc_section full_width="stretch_row" css=".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}"][vc_row 0=""][vc_column offset="vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6"][/vc_column][/vc_row][/vc_section]

Is it possible to render vc shortcodes on page, and if so, how is it done?

Jadeye
  • 3,551
  • 4
  • 47
  • 63

2 Answers2

2

Use the:

WPBMap::addAllMappedShortcodes();

then as usual do_shortcode($content);

In short, page builder due to performance doesn't register shortcodes unless it isn't required.

If your element is registered by vc_map or vc_lean_map then no need to use add_shortcode function, you can do everything just by using WPBMap::addAllMappedShortcodes(); it is will call an shortcode class callback during the rendering process and then shortcode template.

0

Regarding Method 2.

You have to use do_shortcode() in your shortcode function.

function someshortocode_callback( $atts = array(), $content = null ) {
    $output = '[vc_section full_width="stretch_row" css=".vc_custom_1499155244783{padding-top: 8vh !important;padding-bottom: 5vh !important;background-color: #f7f7f7 !important;}"][vc_row 0=""][vc_column offset="vc_col-lg-offset-3 vc_col-lg-6 vc_col-md-offset-3 vc_col-md-6"]column text[/vc_column][/vc_row][/vc_section]';

    return do_shortcode( $output );
}

add_shortcode( 'someshortocode', 'someshortocode_callback' );

Working example on my test site: http://test.kagg.eu/46083958-2/

Page contains only [someshortocode]. Code above is added to functions.php.

In your code for Method 2 there is another error: line

echo add_shortcode('someshortocode', 'someshortocode_callback');

cannot work, as add_shortcode() returns nothing. This code should be as follows:

<?php $post = get_post();

if ( $post && preg_match( '/vc_row/', $post->post_content ) ) {
// Visual composer works on current page/post
    wc_print_notice('VC ON', 'notice');
} else {
    wc_print_notice('VC OFF', 'notice');
    add_shortcode('someshortocode', 'someshortocode_callback');
    echo do_shortcode('[someshortocode]');
}; ?>
KAGG Design
  • 1,945
  • 8
  • 14
  • No, page contains usual text: [someshortcode]. Please see the picture: http://take.ms/lLMdL – KAGG Design Sep 10 '17 at 15:19
  • Code in my answer contains do_shortcode(). Please pay attention to line 4: return do_shortcode( $output ); – KAGG Design Sep 10 '17 at 15:20
  • I saw it all.... Just that I am trying to get it to work programmatically... Please read my question carefully. – Jadeye Sep 10 '17 at 16:15
  • I read your question carefully. In your Method 2 there are 2 errors: 1) you do not use do_shortcode() in someshortocode_callback, 2) line "echo add_shortcode('someshortocode', 'someshortocode_callback');" cannot work, as add_shortcode returns nothing. – KAGG Design Sep 10 '17 at 16:30
  • I have extended the answer with your code for Method 2 corrected. – KAGG Design Sep 10 '17 at 16:37
  • I fixed the errors you mentioned...still on page `echo do_shortcode('someshortocode')` & `echo do_shortcode('[someshortocode]');` yield nothing. And the main issue is how to programmatically `echo` the `shortcode` – Jadeye Sep 10 '17 at 16:52
  • I dont know on what page do you try to implement this code. I have created Minimal, Complete, and Verifiable example. An empty page is created with own Template: https://monosnap.com/file/yV2I0J6d4YyBmil2w1gYPoM4NCLcD8 Template file 46083958-3.php is as follows: http://take.ms/hciBL Result your can see on the page: http://test.kagg.eu/46083958-3/ Everything works. – KAGG Design Sep 10 '17 at 17:06
  • I am trying to implement this on `woocommerce` shop page – Jadeye Sep 10 '17 at 17:52
  • As you can see, my code works. Why it doesn't work on WooCommerce shop page, it is another question, for which it has to be provided the full code of your WooCommerce archive page. – KAGG Design Sep 10 '17 at 17:55