0

I'm trying to extend my client's page backend with Visual Composer extended plugin. I've been following instructions given here: http://kb.wpbakery.com/index.php?title=Visual_Composer_tutorial.

The plugin shows at the WP backend and the fields I have created like this are shown:

array(
    "type" => "textfield",
    "holder" => "div",
    "class" => "",
    "param_name" => "fourth_quote",
    "value" => __("", 'vc_extend'),
    "description" => __('Fourth testimonial quote', 'vc_extend')
)

However, I don't understand how I'm supposed to access the 'fourth_quote' later on:

public function renderMyBartag( $atts, $content = null) {
  extract( shortcode_atts( array(
    'faa' => 'something',
    'color' => '#FF0000'
  ), $atts ) );

  $content = wpb_js_remove_wpautop($content, true); // fix unclosed/unwanted paragraph tags in $content

  $output = '<div>{$fourth_quote}</div>';
  error_log(date('[ d.m.Y H:i:s ] ') . $output . PHP_EOL, 3, "my-errors.log");
  return $output;
}

This, however doesn't output anything even there is content stored.

How do I access the content user have created at the backend so I'd be able to render the page based on that? How do I get the variables?

Jaakko Karhu
  • 2,298
  • 4
  • 28
  • 41

1 Answers1

1

From http://kb.wpbakery.com/index.php?title=Visual_Composer_tutorial:

This list represents shortcode tag as base and params list which will be editable with settings form inside js_composer constructor.

You must add the fourth_quote attribute to the shortcode.
For example:

public function renderMyBartag( $atts, $content = null) {

    # Also, avoid using extract()
    # http://stackoverflow.com/questions/829407/what-is-so-wrong-with-extract
    # http://codex.wordpress.org/Shortcode_API

    $a = shortcode_atts( array(
        'faa'          => 'something',
        'color'        => '#FF0000',
        'fourth_quote' => false, // just a default value
    ), $atts );

    $content = wpb_js_remove_wpautop($content, true);

    $output = $a['fourth_quote'];

    error_log(date('[ d.m.Y H:i:s ] ') . $output . PHP_EOL, 3, "my-errors.log");

    return $output;
}
Kaloyan
  • 7,262
  • 4
  • 32
  • 47
  • Thank you for your help, now I got it working. I'm still not sure if I fully understand the logic. I read that 'content' is treated specially. if I have "'content' => false", or "'content' => 'content'" in the array, a[$content] won't output anything. But if I have content => $content in the array, then later I can output it. Why is it treated differently. And actually I can have "'var' => 'anything'" in the array and it still works. I don't understand. – Jaakko Karhu Nov 15 '14 at 17:52
  • 1
    It seems that when you use "content" as a variable name the Visual Composer is passing it to the shortcode's `$content` variable and that's why you can't use `$a['content']`. Shortcode's `$content` variable handles the input between the shortcode tags. For example in `[my_shortcode] hello world [/my_shortcode]` the `$content` will be "hello world". – Kaloyan Nov 16 '14 at 11:31