0

In my plugin, I´ve added shortcodes that display data from the database and adds some buttons. When buttons are pushed, more data is displayed at the end of the page. This works perfectly when no attributes are added in the shortcode, but when they are, all the text disappears after the buttons are pushed.

Why does addition of attributes seem to erase my shortcode-info?

This is what it looks like:

    function my_plugin(){   
        print "HTML-button + hidden_value"; 
        if ( isset( $_POST["button_is_pressed"] ) ) {
            print  do_shortcode($the_shortcode + attributes based on hidden value);
        }   
    }

    add_shortcode('my_shortcode', 'my_shortcode_function');
    function my_shortcode_function(){
         print "A lot of database data + button";

         if ( isset( $_POST["shortcode_button_is_pressed"] ) ) {
             print  "Even more database data";
         }  
    }

So, the info displayed by the shortcode disappears when I add attributes to the shortcode. If no attributes are added, "A lot of database data" + "Even more database data" will be displayed without problems. Why this difference with attributes?

This was added later: Now when I substituted the "print" with Return of a string (not shown), it seems totally logical that my code doesn´t work, because a Return statement exits the shortcode function.

But how do I display extra information when a shortcode button is pressed? At that point, execution of the shortcode is already finished.

3
  • 2
    Welcome to WordPress Development. Please add the relevant code to your question.
    – birgire
    Dec 26, 2015 at 23:04
  • How are you adding attributes? In your code you are not allowing for any attributes.
    – thebigtine
    Dec 27, 2015 at 10:46
  • I used the following that pulls in the right value: $atts = shortcode_atts( array( 'the_value' => '0'), $atts); $transferred_value = $atts['the_value'] ;
    – Pontus
    Dec 27, 2015 at 11:39

1 Answer 1

2

Shortcodes should return their HTML, not output it. You need to reorgenize your shorcode code to do that.

You get some correct output just by luck now, if you were testing with actual content you would have seen that your code outputs at the wrong place.

3
  • Ok, thank you! I obviously managed without output buffering, but I understand that´s what I should use. Is that right?
    – Pontus
    Dec 27, 2015 at 11:38
  • Not exactly, you basically need to "return" where you have "print", Dec 27, 2015 at 11:44
  • Unfortunately, returning a string didn´t solve my problem. The "Even more database data" won´t be printed in the case I use attributes in the shortcode.
    – Pontus
    Dec 28, 2015 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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