1

good afternoon,

followed this page to successfully add a Metabox field to my admin page, and now i'm trying to make it useful for me : https://paulund.co.uk/create-custom-meta-boxes

i'm trying to make a simple recipe post for my page, and i want to be able to enter multiple ingredients into one field... ideally one per line. and then output that meta data as an array to use for/each processing to make a list on my page.

i've added the following to my functions.php :

// Lets get meta
    function add_embed_recipe_meta_box() {
        add_meta_box(
            'embed_recipe_meta_box',           // $id
            'Recipe Post meta box',            // $title
            'show_embed_recipe_meta_box',      // $callback
            'post',                            // $page
            'normal',                          // $context
            'high');                           // $priority
    }
    add_action('add_meta_boxes', 'add_embed_recipe_meta_box');

// Show the meta box in admin
    function show_embed_recipe_meta_box() {
        global $post;  
               $meta = get_post_meta($post->ID, 'recipe_embed', true);  

       // use nonce for verification  
        echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  

       echo '<table class="form-table">';   

            echo '<tr>
                <th><label for="recipe_embed">Ingredients</label></th>
                <td><textarea name="recipe_embed" id="recipe_embed" cols="60" rows="1">'.$meta.'</textarea></td>
                </tr>';

        echo '</table>';
    }

// Save the meta data
    function save_recipe_embed($post_id) {   
        // verify nonce
        if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
            return $post_id;

        // check autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
            return $post_id;

        // check permissions
        if ('page' == $_POST['post_type']) {
            if (!current_user_can('edit_page', $post_id))
                return $post_id;
            } elseif (!current_user_can('edit_post', $post_id)) {
                return $post_id;
        }  

        $old = get_post_meta($post_id, "recipe_embed", true);

        $new = $_POST["recipe_embed"];

        if ($new && $new != $old) {
            update_post_meta($post_id, "recipe_embed", $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, "recipe_embed", $old);
        }
    }
    add_action('save_post', 'save_recipe_embed');

and call the Metadata using this code :

<?php $recipe_embed = get_post_meta($post->ID, "recipe_embed", true);
   if($recipe_embed != ""){ //content for true if statement below ?>

   <div style ="background: green;">     
   <?php echo $recipe_embed; //this is the content of the value ?>
    </div> 

<?php }   //close the if statement ?>   

it works great to add a single field in my Metabox, and display that data later, but i'm lost trying to add an array to the input. any advice greatly appreciated. or alternate better ways of doing the same task :)

thanks!

1 Answer 1

0

You can use what you have with slight modification. Just increase the height of the meta box and let your users enter an item on each line.

Embed

$recipe_embed = get_post_meta( $post->ID, "recipe_embed", true );

if ( ! empty( $recipe_embed ) ): ?>

    <div style="background: green;">
        <?php

        // normalize returns
        $recipe_embed = str_replace( "\n", "\r", $recipe_embed );

        // convert to array and remove empty items
        $ingredients  = array_filter( explode( "\r", $recipe_embed ) );

        // list items
        echo "<ul class='ingredients'>";
        foreach ( $ingredients as $ingredient ) {
            echo "<li class='ingredient' >$ingredient</li>";
        }
        echo "</ul>";

        ?>
    </div>

<?php endif; // $recipe_embed

Meta Box

<?php

function add_embed_recipe_meta_box() {
    add_meta_box( 'embed_recipe_meta_box',     // $id
            'Recipe Post meta box',            // $title
            'show_embed_recipe_meta_box',      // $callback
            'post',                            // $page
            'normal',                          // $context
            'high' );
}

add_action( "add_meta_boxes", "add_embed_recipe_meta_box" );

function show_embed_recipe_meta_box( $post ) {
    $meta = get_post_meta( $post->ID, 'recipe_embed', true );

    wp_nonce_field( basename( __FILE__ ), "recipe-meta-box-nonce" );

    ?>
    <table class="form-table">
        <tr>
            <th><label for="recipe_embed">Ingredients</label></th>
            <td><textarea name="recipe_embed" id="recipe_embed" cols="60" rows="10"><?php echo $meta; ?></textarea></td>
        </tr>
    </table>
    <?php
}

function save_recipe_embed( $post_id, $post, $update ) {
    if ( ! isset( $_POST[ "recipe-meta-box-nonce" ] ) || ! wp_verify_nonce( $_POST[ "recipe-meta-box-nonce" ], basename( __FILE__ ) ) ) {
        return $post_id;
    }

    if ( ! current_user_can( "edit_post", $post_id ) ) {
        return $post_id;
    }

    if ( defined( "DOING_AUTOSAVE" ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    $slug = "post";
    if ( $slug != $post->post_type ) {
        return $post_id;
    }

    $recipe_embed = '';
    if ( isset( $_POST[ "recipe_embed" ] ) ) {
        $recipe_embed = $_POST[ "recipe_embed" ];
    }
    update_post_meta( $post_id, "recipe_embed", $recipe_embed );
}

add_action( "save_post", "save_recipe_embed", 10, 3 );
1
  • 1
    works perfectly! you are a wonderful and glorious thing. thanks!
    – ijason
    Jun 14, 2016 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.