0

i got close solution for this question from this link "How to add a textarea to only one of the fields of this custom metabox?", but with different code and he use new metabox, i need to see value on custom fields also, with this solution i cant see on "custom fields box".

so i use this code:

    $sp_boxes = array (
    'Digi Box' => array (
        array( 'shortdesc', 'Shortdesc:' ),
        array( 'url', 'Url:' ),
    ),
);


add_action( 'admin_menu', 'sp_add_custom_box' );


add_action( 'save_post', 'sp_save_postdata', 1, 2 );

function sp_add_custom_box() {
    global $sp_boxes;

    if ( function_exists( 'add_meta_box' ) ) {

        foreach ( array_keys( $sp_boxes ) as $box_name ) {
            add_meta_box( $box_name, __( $box_name, 'sp' ), 'sp_post_custom_box', 'post', 'normal', 'high' );
        }
    }
}


function sp_post_custom_box ( $obj, $box ) {
    global $sp_boxes;
    static $sp_nonce_flag = false;

    if ( ! $sp_nonce_flag ) {
        echo_sp_nonce();
        $sp_nonce_flag = true;
    }

    foreach ( $sp_boxes[$box['id']] as $sp_box ) {
        echo field_html( $sp_box );
    }
}

function field_html ( $args ) {

    switch ( $args[2] ) {

        case 'textarea':
            return text_area( $args );

        case 'checkbox':
            // To Do

        case 'radio':
            // To Do

        case 'text':
        default:
            return text_field( $args );
    }
}

function text_field ( $args ) {
    global $post;

    $args[2] = get_post_meta($post->ID, $args[0], true);
    $args[1] = __($args[1], 'sp' );

    $label_format =
    '<label for="%1$s" style="font-size: 15px; font-weight: bold;">%2$s</label><input style="float: right; margin: -2px 0 0; width: 88%%;" type="text" name="%1$s" value="%3$s" /><br /><br /><br />';

    return vsprintf( $label_format, $args );
}

function sp_save_postdata($post_id, $post) {
    global $sp_boxes;


    if ( ! wp_verify_nonce( $_POST['sp_nonce_name'], plugin_basename(__FILE__) ) ) {
        return $post->ID;
    }


    if ( 'page' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post->ID ))
            return $post->ID;

    } else {
        if ( ! current_user_can( 'edit_post', $post->ID ))
            return $post->ID;
    }

    foreach ( $sp_boxes as $sp_box ) {
        foreach ( $sp_box as $sp_fields ) {
            $my_data[$sp_fields[0]] =  $_POST[$sp_fields[0]];
        }
    }


    foreach ($my_data as $key => $value) {
        if ( 'revision' == $post->post_type  ) {

            return;
        }


        $value = implode(',', (array)$value);

        if ( get_post_meta($post->ID, $key, FALSE) ) {


            update_post_meta($post->ID, $key, $value);


        } else {

            add_post_meta($post->ID, $key, $value);
        }

        if (!$value) {

            delete_post_meta($post->ID, $key);
        }
    }
}

function echo_sp_nonce () {

    echo sprintf(
        '<input type="hidden" name="%1$s" id="%1$s" value="%2$s" />',
        'sp_nonce_name',
        wp_create_nonce( plugin_basename(__FILE__) )
    );
}

i have 2 fields: 1. shortdesc 2. url

with this code, it just show input type only. now, how to make "shortdesc" to be textarea type? and "url" still input type.

Thanks.

sorry for my english.

1 Answer 1

0

i got the solutions,

array( 'shortdesc', 'shortdesc' , 'textarea'),

and add new function for text_area:

function text_area ( $args ) {
global $post;
// adjust data
$args[2] = get_post_meta($post->ID, $args[0], true);
$args[1] = __($args[1], 'fp' );
$label_format =
'<label for="%1$s">%2$s</label><br />' . '<textarea style="width: 95%%;"  name="%1$s">%3$s</textarea><br /><br />';
return vsprintf( $label_format, $args );
}

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.