• Resolved Adam Czajczyk

    (@ambiantepl)


    Hello Everyone!

    I’m working on a plugin that supports certain shortcode. Let’s say it’s: [my_shortcode]. Now as I can only have one handler function and a function may return (return “something”) value only once and then terminates I’ve got this problem.

    Assume pseudocode:

    function my_shortcode_handler($attr) {
    
        return '<p id="msg">Hello world!</p>";
    
        /... do something complicated .../
    
        <script>
           var m = getElementById("msg");
           m.innerHTML = 'Goodbye world!';
        </script>
        }
    
        add_shortcode('my_shortcode', 'my_shortcode_handler');

    What I need is to immediatly output some text (visible in post) in place of shortcode but then let shortcode handler function do some work and when it’s finished change already outputted text to something else.

    The problem is:
    1) Obviously I can’t use “echo” inside plugin
    2) I also (contrary to the pseudocode above) cannot use “return” on the beggining of the function in order not to terminate it
    3) I need to change already outputted text, so I guess JS would be an answer her but I might as well be wrong…

    Anyway, I’m pretty sure there’s a solution, however I can find it. Do you have any ideas?

    Thanks in advance,
    A.

Viewing 1 replies (of 1 total)
  • Thread Starter Adam Czajczyk

    (@ambiantepl)

    Found it.

    Just for future reference: the solution is:

    function my_change_shortcode($content) {
       preg_match_all('~\[my_shortcode.*\]~i', $content, $matches); //find all ocurrences of [my_shortcode] (even with attributes) within content
       /...
           all found shortcodes are in $matches array so here we may modifie them ass needed, like add make "[may_shortcode]<p id="msg">Hello World!</p>"
    
       end put it back into $content
    
       .../
      return $content
    }
    add_filter('the_content','my_change_shortcode')

    after that normal shortcode handle goes and to change “<p id=’msg’>Hello World!</p> message the JS from inside the shortcode handler from my first post works just fine.

Viewing 1 replies (of 1 total)

The topic ‘Change shortcode handler output with JS?’ is closed to new replies.