0

I am trying to add an image field that lets the user choose an image from the media library. Adding text fields worked fine, but now that I am adding an image field it is not working. What should I specify as input for an image field that only allows images from the media library?

<p>
    <label>Picture:</label><br />
    <input type="file" name="picture" value="<?= @$custom["cafe_image"][0] ?>" class="width99" accept="image/*" />
</p>

The above code does not work correctly (it creates a file selection button), so do I just put something else in the place of 'file'? The rest of the function is below:

function add_cafe_meta_boxes() {
    add_meta_box("cafe_contact_meta", "Contact Details", "add_contact_details_cafe_meta_box", "cafes", "normal", "low");
}
function add_contact_details_cafe_meta_box()
{
    global $post;
    $custom = get_post_custom( $post->ID );

    ?>
    <style>.width99 {width:99%;}</style>
    <p>
        <label>Address:</label><br />
        <textarea rows="5" name="address" class="width99"><?= @$custom["address"][0] ?></textarea>
    </p>
    <p>
        <label>Website:</label><br />
        <input type="text" name="website" value="<?= @$custom["website"][0] ?>" class="width99" />
    </p>
    <p>
        <label>Phone:</label><br />
        <input type="text" name="phone" value="<?= @$custom["phone"][0] ?>" class="width99" />
    </p>
        <p>
        <label>Picture:</label><br />
        <input type="file" name="picture" value="<?= @$custom["cafe_image"][0] ?>" class="width99" accept="image/*" />
        </p>
    <?php
}

add_action( 'admin_init', 'add_cafe_meta_boxes' );
5
-1

My solution works but it is a cop-out. I went to CMB2 for adding fields and it works beautifully.

Some people might specifically be looking for an answer to this, so I know this is not helpful for that, but switch to CMB2 if you can.

1

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.