• rmsgreig

    (@rmsgreig)


    I have created a shortcode that lists fixtures in a sporting competition from a json api, for each of the fixtures in this list I would like to use buttons to call other shortcodes that will provide different updates to the fixture such as goals scored etc. What would be the best way to do this?
    What I have so far in my test code is

     $return.= '<input value="Show Cards" type="submit" name="cards"/>';
    
     }
    
      $return .= '</ul>';
    
            return $return;
    
       if(isset($_POST['cards']))  
         
     {
         function fixture_cards()
         {
       echo do_shortcode('[card_updates fixture="' . $fixtureID . '" event="card"]');
         }
     }

    am I going about this the correct way?
    Thanks in advance for any help

    • This topic was modified 7 years ago by rmsgreig.
Viewing 14 replies - 1 through 14 (of 14 total)
  • Jack

    (@jdabber)

    That’s how I would do it. The best way to run another shortcode is with do_shortcode(). It’s not that unusual to have to build a somewhat cursory function like this when it comes to shortcodes. So don’t doubt yourself, you’ve got the right idea.

    I will say you should be aware of escaping any user input from the $_POST if you’re using that as part of the shortcode generation. Safety first

    Thread Starter rmsgreig

    (@rmsgreig)

    thanks, it doesn’t seem to be working though am I missing something?

    Moderator bcworkz

    (@bcworkz)

    Yes. Don’t immediately echo the return of do_shortcode(). Run the return through do_shortcode() again. do_shortcode() only “sees” the outermost shortcode in the passed content. It does not see internal shortcodes in the expanded content. do_shortcode() is not a recursive function, it needs to be called each time for however many levels shortcodes are nested.

    Thread Starter rmsgreig

    (@rmsgreig)

    Could you give me an example of how I would do this do you mean add the shortcodes to my $return or have different returns for each button?
    My first shortcode is in the template file and my other shortcodes are in the functions file – should these all be in the same place?
    Sorry if this sounds confusing, I’m relatively knew to wordpress – so very much learning on the job.
    Thanks for your help

    Moderator bcworkz

    (@bcworkz)

    I’m going to assume the snippet you provided is your “outer” shortcode handler function. Even if not, maybe you can discern enough of my example to apply the concept elsewhere.

         // more code happens before we get here
         $return .= '<input value="Show Cards" type="submit" name="cards"/>';
      }
      $return .= '</ul>';
      if(isset($_POST['cards']))  
      {
         $return .= do_shortcode('[card_updates fixture="' . $fixtureID . '" event="card"]');
      }
      return $return;
    }
    Thread Starter rmsgreig

    (@rmsgreig)

    yes this is from my outer shortcode handler function, I have updated the code but still nothing seems to be happening, could it be the input buttons themselves? Can’t figure out how I could set up a debug to figure out why it’s not working here is my extended code:

    • This reply was modified 6 years, 12 months ago by rmsgreig.
    • This reply was modified 6 years, 12 months ago by Jan Dembowski.
    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    @rmsgreig Can you post that code using http://pastebin.com/ instead? Your code was redacted because it broke the formatting here.

    Moderator bcworkz

    (@bcworkz)

    There’s probably a number of ways to debug shortcode handlers. I often use an essentially blank page template upon which I place test snippets. By requesting a page based on this template, my code runs and i can see whatever debug output there is.

    From such a template, call the handler function by name directly. You can add debug code to the function that echoes out data. In this context echoing is fine. You of course would not leave any echo statements in production code.

    Thread Starter rmsgreig

    (@rmsgreig)

    Hi I have pasted my code to here: https://pastebin.com/YsG9U84c
    I have a debug plugin set up in the wordpress site but it’s not showing anything when I try and use the buttons?

    Moderator bcworkz

    (@bcworkz)

    Is your main shortcode working correctly except for the inner shortcodes?

    If you inserted any of those inner shortcodes by themselves in post content, do they work correctly?

    Have you tried removing one of the if isset($_POST[*]) conditionals to eliminate any issues with getting submitted data passed?

    The basic concept is sound. To be sure, I did a quickie proof of concept:

    function footag_func( $atts ) {
    	$content = '<div>';
    	$content .= 'This is footag shortcode expansion<br>';
    	$content .= do_shortcode('[inner]');
    	return $content . '</div>';
    }
    add_shortcode( 'footag', 'footag_func' );
    
    function inner_func( $atts ) {
    	return '<div>Inner Shortcode Expansion</div>';
    }
    add_shortcode( 'inner', 'inner_func' );

    After adding “[footag]” to post content, the resulting output was this:
    <div>This is footag shortcode expansion<br><div>Inner Shortcode Expansion</div></div>

    If that did not work for some reason, I could put this on a mostly empty page template:
    echo footag_func();

    I did not use attributes. If I had, to pass attributes, pass an associative array with key => value pairs representing the shortcode attributes used. Use your debugger to setup breakpoints and inspect variable values to ensure all are as they should be. Eventually you will find something that is not right. Determine the cause and correct it. That may fix it, or there may be other problems. Rinse and repeat until it does work.

    Thread Starter rmsgreig

    (@rmsgreig)

    It looks like it is the if isset($_post[*] that is causing the issues, i took one of the shortcodes out and it displays fine – any ideas how I would fix this/get around it?

    Moderator bcworkz

    (@bcworkz)

    It would depend on why there are never any matching $_POST elements. Are you sure the requests are POST and not GET?. If either are possible, check $_REQUEST, which contains the same data regardless of request type. There can be a slight security risk with $_REQUEST, but if the server is properly configured so cookie values cannot override form data, then it’s fine.

    One reason for form or other passed data not being available is they are stripped in redirects. If that’s the case, the data needs to be passed through other means. Cookies, transients, or session variables are your main options.

    BTW, it doesn’t matter that much, but you might consider using a switch/case structure instead of a long series of if/elseif/else statements.

    Thread Starter rmsgreig

    (@rmsgreig)

    I have it working! Thank you so much for your help šŸ™‚ — I was missing the <form> </form> and then it was a case of making sure each button has a unique name which I did by adding the ID to the name and using a variable in the isset.

    • This reply was modified 6 years, 11 months ago by rmsgreig.
    Moderator bcworkz

    (@bcworkz)

    Great news! You’re most welcome šŸ™‚

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘How to call shortcodes in buttons in shortcode?’ is closed to new replies.