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" />
        $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; ?>
    <?php endforeach; ?>

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.