2

I am trying to use an existing shortcode for an accordion element in a custom template for my wordpress site. Basically, I need to insert PHP arrays inside this shortcode, like so:

<php do_shortcode('[vc_accordion]';>

PHP arrays here

<php do_shortcode('[/vc_accordion]';>

Does anyone know if a solution?

3
  • That's not how shortcodes work, you have to pass the entire shortcode into the function
    – Tom J Nowell
    Jul 4 '14 at 22:58
  • 2
    Right, but I'm looking for a solution that I can insert the rest of the PHP into the function. Jul 4 '14 at 23:08
  • Everything that you pass in to the shortcode has to be done via the shortcode string, I don't think a shortcode is what you should be using for this
    – Tom J Nowell
    Jul 4 '14 at 23:29
1

This is the code I would use:

$open_shortcode = '[vc_accordion]';
$shortcode_data = '';
$close_shortcode = '[/vc_accordion]';

$myarray = array(
    'tabs' => 
        array(
            'title' => 'Section 1',
            'content' => 'Any text here'
        ),
        array(
            'title' => 'Section 2',
            'content' => 'Any text here'
        )
);

foreach( $myarray['tabs'] as $tab ){
    $shortcode_data .= '[vc_accordion_tab title="' . $tab['title'] . '"]' . $tab['content'] . '[/vc_accordion_tab]';
}

echo do_shortcode($open_shortcode . $shortcode_data . $close_shortcode);

Assuming you want something similar to this:

[vc_accordion]
    [vc_accordion_tab title="Section 1"]Any text here[/vc_accordion_tab]
    [vc_accordion_tab title="Section 2"]Any text here[/vc_accordion_tab]
[/vc_accordion]

You provided very little detail which doesn't help us help you, but I believe this should do the trick.

0

Basically you could simply use a short code and parse the entered value as php by using eval().

That would probably be the answer to your question. Yet I have to ask why? Why do you want to do that.

You can extend your short code and pass variables just there. Parsing php is highly unsafe as there could be a security threat and is just bad practice. The array passed via short code is much better.

-1

This is Correct Format:

<?php echo do_shortcode( $content ) ?>

Example --> Your Short Code : [wp-like-locker] Now Inster :

<?php echo do_shortcode( [wp-like-locker] ) ?>

http://codex.wordpress.org/Function_Reference/do_shortcode

3
  • Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Jul 4 '14 at 23:09
  • @toscho Edit My Answer
    – HamedPC
    Jul 4 '14 at 23:36
  • Now it is invalid PHP. :) Please test your suggestion, and provide an explanation.
    – fuxia
    Jul 4 '14 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.