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?

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...!

| improve this answer | |
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"]' ); ?>
| improve this answer | |

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.