0

I want to know how wordpress displaying shortcode inserted in post content, so I can replicate what it does and implement it to my plugin.

Here is an example:

I want to show shortcode inserted between an html codes like this:

    <div class="akismet_activate">
    <div class="aa_a">A</div>
    <div onclick="document.akismet_activate.submit();" class="aa_button_container">

[the_shortcode id="4"]

<div class="aa_button_border">
    <div class="aa_button">Activate your Akismet account</div>
    </div>
    </div>
    <div class="aa_description"><strong>Almost done</strong> - [the_shortcode id="4"] activate your account and say goodbye to comment spam</div>
    </div>

I've done several ways, but its only execute the shortcodes and ignoring the html goodies.

What can I do to make the executed shortcode displayed between html codes like that example?

2 Answers 2

0

Here is the solution I come up with after 30 minutes researching:

function wpse_175564() {

  // The html goodies with shortcodes
       $htmlcodes = '<div class="akismet_activate">
                <div class="aa_a">A</div>
                <div onclick="document.akismet_activate.submit();" class="aa_button_container">

            [the_shortcode id="4"]

            <div class="aa_button_border">
                <div class="aa_button">Activate your Akismet account</div>
                </div>
                </div>
                <div class="aa_description"><strong>Almost done</strong>
     - [the_shortcode id="4"] activate your account and say goodbye to comment spam</div>
                </div>';
         // End of HTML Goodies

               // Let's get the shortcode inside the string and execute it!

                $pattern = get_shortcode_regex();
  if (preg_match_all( '/'. $pattern .'/s', $htmlcodes, $matches ) && array_key_exists( 2, $matches )) {

       foreach($matches[0] as $i => $v){
          $htmlcodes = preg_replace($v , do_shortcode($v), $htmlcodes);
          $htmlcodes =  str_replace(array('[',']'), '', $htmlcodes);
         }


  } 

 return $htmlcodes;

}

Not sure whether that's the best solution or not, but it works...!

0

If you want to insert a shortcode directly into the php template, you have to use do_shortcode().

For example:

<?php echo do_shortcode( '[the_shortcode id="4"]' ); ?>

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.