Aidan Knight
Aidan Knight

Reputation: 263

Predefined values for user selection in custom post type (metadata vs taxonomy)

I'm building a plugin that allows visitors to submit software configurations to share with others. They input several bits of info (their name, the software and the machine) and then upload their XML profile, which is ultimately converted into a custom post type.

As of right now, I am storing everything they input like their name, the software, the machine type, etc. as metadata. I want to have predefined options for software/machine types though, allowing them to choose from these options when submitting.

enter image description here

What would be a good way to achieve this in Wordpress? Should I just keep these as pre-defined values in a select box via the form, then save the data as text in metadata or is there a better alternative?

function slicer_profile_form()
{
    echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post" enctype="multipart/form-data">';

    echo '<p>';
    echo 'Your Name<br />';
    echo '<input type="text" name="slicer-profile-author" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["slicer-profile-author"] ) ? esc_attr( $_POST["slicer-profile-author"] ) : '' ) . '" size="48" />';
    echo '</p>';

    echo '<p>';
    echo 'Profile Name<br />';
    echo '<input type="text" name="slicer-profile-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["slicer-profile-name"] ) ? esc_attr( $_POST["slicer-profile-name"] ) : '' ) . '" size="48" />';
    echo '</p>';

    echo '<p>';
    echo 'Profile Description<br />';
    echo '<textarea name="slicer-profile-description" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["slicer-profile-description"] ) ? esc_attr( $_POST["slicer-profile-description"] ) : '' ) . '" rows="4"></textarea>';
    echo '</p>';

    echo '<p>';
    echo '3D Printer Model<br />';
    echo '<select name="slicer-profile-model">';
    echo '<option value="a8">Anet A8</option>';
    echo '<option value="cr10">Creality CR-10</option>';
    echo '<option value="mini">Monoprice Select Mini</option>';
    echo '<option value="makerselect">Monoprice Maker Select</option>';
    echo '<option value="ultimate">Monoprice Ultimate</option>';
    echo '<option value="prusamk2">Prusa MK2/MK2S/MK3</option>';
    echo '</select>';
    echo '</p>';

    echo '<p>';
    echo 'Slicer Software<br />';
    echo '<select name="slicer-profile-software">';
    echo '<option value="cura">Cura</option>';
    echo '<option value="s3d">Simplify3D</option>';
    echo '<option value="slic3r">Slic3r</option>';
    echo '</select>';
    echo '</p>';

    echo '<p>';
    echo 'Slicer Profile<br />';
    echo '<input type="file" name="slicer-profile" accept=".fff,.ini,.curaprofile">';
    echo '</p>';

    echo '<p><input type="submit" name="slicer-profile-submitted" value="Submit"/></p>';
    echo '</form>';
}

Upvotes: 0

Views: 69

Answers (1)

Xhynk
Xhynk

Reputation: 13890

If you want to group items together, use a taxonomy. Aside from that being the literal definition of the word, it makes it easy to pull in all posts for the same software and keep those grouped. That's what a Taxonomy excels at.

If you just have a more overall CPT, that just need to have a bit of arbitrary information attached to them, that's what Custom Fields excel at. This is mainly for arbitrary information that's not categorically relatable, like Price, or Event Start Date, or Facebook Group/Page URL.

It sounds like you would be better suited with a taxonomy/term relationship for Software and Machine Type, though ultimately it's up to you. You can query posts based on custom fields, but categorically definable information is better suited for a taxonomy.

As an unrelated aside, is there any particular reason you're using an echo statement per line instead of just closing your PHP tag and echoing the few PHP variables you have inside standard HTML?

function slicer_profile_form(){ ?>
    <form action="<?= esc_url( $_SERVER['REQUEST_URI'] ); ?>" method="post" enctype="multipart/form-data">
        <label>
            Your Name<br>
            <input type="text" name="slicer-profile-author" pattern="[a-zA-Z0-9 ]+" value="<?= ( isset( $_POST["slicer-profile-author"] ) ? esc_attr( $_POST["slicer-profile-author"] ) : '' ); ?>" size="48" />
        </label>
        <label>
            Profile Name<br>
            <input type="text" name="slicer-profile-name" pattern="[a-zA-Z0-9 ]+" value="<?= ( isset( $_POST["slicer-profile-name"] ) ? esc_attr( $_POST["slicer-profile-name"] ) : '' ); ?>" size="48" />
        </label>
        <label>
            Profile Description<br>
            <textarea name="slicer-profile-description" pattern="[a-zA-Z0-9 ]+" value="<?= ( isset( $_POST["slicer-profile-description"] ) ? esc_attr( $_POST["slicer-profile-description"] ) : '' ); ?>" rows="4"></textarea>
        </label>
        <label>
            3D Printer Model<br>
            <select name="slicer-profile-model">
                <option value="a8">Anet A8</option>
                <option value="cr10">Creality CR-10</option>
                <option value="mini">Monoprice Select Mini</option>
                <option value="makerselect">Monoprice Maker Select</option>
                <option value="ultimate">Monoprice Ultimate</option>
                <option value="prusamk2">Prusa MK2/MK2S/MK3</option>
            </select>
        </label>
        <label>
            Slicer Software<br>
            <select name="slicer-profile-software">
                <option value="cura">Cura</option>
                <option value="s3d">Simplify3D</option>
                <option value="slic3r">Slic3r</option>
            </select>
        </label>
        <label>
            Slicer Profile<br>
            <input type="file" name="slicer-profile" accept=".fff,.ini,.curaprofile">
        </label>
        <p>
            <input type="submit" name="slicer-profile-submitted" value="Submit" />
        </p>
    </form>
<?php } ?>

Upvotes: 1

Related Questions