简体   繁体   中英

Wordpress: best practice to insert a block if mixed html/php into a post

I'm trying to insert a block of mixed html/php code into a Wordpress post. It is filled by custom field values from ACF Pro. It's supposed to be part of the post, so I try to avoid editing template files.

The block looks contains roughly the following:

<?php if (get_field('infobox_anzeigen') == 1): ?>
    <div class="touristische-informationen">

    <h2>Informationen</h2>

      <!-- Start Allgemein -->
      <?php if( have_rows('ti_allg')): ?>

      <h3>Übersicht</h3>
      <div class="ti-div">

        <?php if (have_rows('ti_allg_bezeichnung')): ?>
          <p><b>Name: <?php the_field('ti_allg_bezeichnung_name') ?> (<?php the_field('ti_allg_typ'); ?>)</b></p>
            <?php if (get_field('ti_allg_bezeichnung_kanji')): ?>
              <p>Kanji: <?php the_field('ti_allg_bezeichnung_kanji'); ?> | <?php the_field('ti_allg_bezeichnung_kana'); ?> | <i><?php the_field('ti_allg_bezeichnung_hepburn'); ?></i></p>
            <?php endif; ?>
        <?php endif; ?>

        <?php if (get_field('ti_allg_besucht')): ?>
          <p>Besucht am: <?php the_field('ti_allg_besucht'); ?></p>
        <?php endif; ?>

...imagine the end of this code.

What's certainly working are those "insert php snippets" plugins, which I'd like to avoid for now. I tried making a custom shortcode via custom plugin, but because of the mixed code nature, I ran into issues with echo and output buffering etc. Although that would be probably the best solution for my use case as this would allow me to still add text before and after the block.

How would you tackle this problem? Using a insert php plugin? Following the custom shortcode approach? Something entirely else?

The order I'd consider tackling these is:

  1. Modifying single.php , whichever template is responsible for this, or a custom functions file ( functions.php , a /mu-plugins/ file, or plugin, etc.)
  2. Using custom shortcodes.
  3. Using a PHP inserter plugin. Either one that lets you use [php]echo 'test';[/php] type tags or inserted snippets via shortcodes.

Note that 3 doesn't make sense if this block of code is repeated even a few times, and 2 doesn't make sense if it's repeated on the majority of posts unless you need granular control of where it's inserted.

Honestly the best practice here is to drop that into single.php wherever it goes, or filter/hook it in using something like functions.php . "It's supposed to be part of the post" doesn't mean you can't modify template files. Just hook it in the right place.

If it's supposed to be in between content somewhere, then you'll probably need to consider using a shortcode or mergetag to tell it exactly where to insert this code. If it goes before/after the content (but still in the content area) modifying the template is a-okay and very straight forward and probably the "best practice" in that case.

Edit: Here's an example of appending the code using output buffer and the_content filter:

<?php
    add_filter( 'the_content', 'so_51196107_after_content' );
    function so_51196107_after_content( $content ){
        if( is_singular( 'post' ) ):
            if( get_field('infobox_anzeigen') == 1 ): ob_start(); ?>
                <div class="touristische-informationen">
                    <h2>Informationen</h2>
                    <?php if( have_rows( 'ti_allg' ) ): ?>
                        <h3>Übersicht</h3>
                        <div class="ti-div">
                            <?php if( have_rows( 'ti_allg_bezeichnung' ) ): ?>
                                <p><b>Name: <?php the_field( 'ti_allg_bezeichnung_name' ) ?> (<?php the_field( 'ti_allg_typ' ); ?>)</b></p>
                                <?php if( get_field( 'ti_allg_bezeichnung_kanji' ) ): ?>
                                    <p>Kanji: <?php the_field( 'ti_allg_bezeichnung_kanji' ); ?> | <?php the_field( 'ti_allg_bezeichnung_kana' ); ?> | <i><?php the_field( 'ti_allg_bezeichnung_hepburn' ); ?></i></p>
                                <?php endif; ?>
                            <?php endif; ?>
                            <?php if( get_field( 'ti_allg_besucht' ) ): ?>
                                <p>Besucht am: <?php the_field('ti_allg_besucht'); ?></p>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>
            <?php
                $infobox  = ob_get_clean();
                $content .= $infobox;
                endif;
        endif;

        return $content;
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM