Custom Widget form function common elements

I am writing a set of custom widgets, all of which share a common 18 lines of code in the form() function, which looks like this:

<div class="common-section">
    <input type="hidden" class="inputbox" name="something" />
    <?php
        $choices = [
            'first'  => array('red','orange','blue'),
            'second' => array('red','white','blue'),
            'third'  => array('green','black','white')
        ];
    ?>
    <?php foreach($choices as $colorset => $colors) : ?>
        <div class="container">
            <div class="colorset"><?=$colorset?></div>
            <?php foreach($colors as $color) : ?>
                <div class="color-swatch" style="background-color:<?=$color?>;"></div>
            <?php endforeach; ?>
    </div>
    <?php endforeach; ?>
</div>

What is the best way to take this code and include it in each widget that uses it?

I was thinking about using a require or include but wanted to ask the community if there is a better way to accomplish this “The WordPress Way”.

0

Leave a Comment