1

Basically, I have this line of code:

<?php echo do_shortcode('[O_U user_name="operator" 
blocked_message="This page is restricted for guests."] 
**Content Goes here** [/O_U]'); ?>

Now, inside Content Goes here I use regular HTML to create my content. But, now, I need to add some php inside that HTML.

Right now, I need to populate the option for select tag with MySQL results.

I have a php code:

<?php
        $database_name = "rams";
        $mydb = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST);
        $mydb -> show_errors();

        //Populate languages
        $languages = $mydb -> get_results(
            'SELECT * FROM language_skills'
        );

        foreach ($languages as $language){
           //do some echo of html
        }
?>

How can I achieve a result here? I now, echo waits for a string there. I thought about heredoc and a custom function, but I can't seem to achieve the result.

UPDATE:

I managed to get my data. It's in an array. How I can show all the array results in above situation?

My array goes something like this:

<option value="$value">$content</option>

It shows exactly what I want when I use:

[shortcode].$array[index].[/shortcode]

How can I add all the indexes? I tried for each but it doesn't work in between shortcodes

ANSWER:

I used implode to change array to a string and code now works as i want.

1

You can not put PHP functions inside your shortcode.

However, you do not need to. You can just use your foreach-loop to create the html and store it in a variable, for example $shortcodehtml.

So, afterwards, you can call

echo do_shortcode('[O_U user_name="operator" blocked_message="This page is restricted for guests."]' . $shortcodehtml . '[/O_U]');

The better version of that would be if you would create the output inside the shortcode function - from my point of view it is not necessary to pass the whole html to the shortcode - but this should work fine.

  • Can you please ellaborate the answer? You mean I should store foreach results in an array and then pass it as above? – theo Dec 30 '15 at 9:30
  • I can't seem to make it work. I've created an array of option tags and it displays when I use echo the array. but it doesn't work in above fashion – theo Dec 30 '15 at 9:48
1

It's always better to use the PHP stuff inside your function definition and not while executing your shortcode.

To add more to Fischi's answer, you can refer to below snippet.

function wptuts_first_shortcode($atts, $content=null){

    $return_variable = "";
    $database_name = "rams";
    $mydb = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST);
    $mydb -> show_errors();

    //Populate languages
    $languages = $mydb -> get_results(
        'SELECT * FROM language_skills'
    );

    foreach ($languages as $language){
       $return_variable = $return_variable."<br />".$language;
    }
    return $return_variable;
  }

add_shortcode($return_variable, 'wptuts_first_shortcode');

You can use the returned value in your shortcode display.

Refer the link for additional reference.

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.