wordpress自定义帖子附件字段

时间:2014-01-06 05:49:35

标签: php wordpress

我正在创建自定义帖子字段所有文本字段都正常工作但我在保存附件字段时有点困惑。它在数据库中保存文件名,但不在上传目录中移动该文件。enter image description here

这是代码:

`$ sp_boxes = array(     '产品详情'=>数组(

    array( 'author', 'Author / product:' ),  //text field
    array( 'filesize', 'File size / license:' ),//text field
     array( 'abc', 'Requirements: ' ),//text field
     array( 'screen', 'Screen Shots: ',"img" ),//Attachment Field

),`

* Attachemnet(上传图片字段令人困惑)    add_action( 'admin_menu', 'sp_add_custom_box' );     //使用save_post操作对输入的数据执行某些操作     //保存自定义字段     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;

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

// Genrate box contents
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':
    case 'img':
    return attachment( $args );
    default:
        return text_field( $args );
}
}

function attachment ( $args ) {
global $post;

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

$label_format =
      '<label for="%1$s">%2$s</label><br />'
    . '<input type="file" id="%1$s" name="%1$s" value="" size="25"><br /><br />';

return vsprintf( $label_format, $args );
}
function text_field ( $args ) {
global $post;

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

$label_format =
      '<label for="%1$s">%2$s</label><br />'
    . '<input style="width: 95%%;" type="text" name="%1$s" value="%3$s" /><br /><br    />';

return vsprintf( $label_format, $args );

}

`

  • 这就是我保存帖子数据的方式

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

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( ! wp_verify_nonce( $_POST['sp_nonce_name'], plugin_basename(__FILE__) ) ) {
    return $post->ID;
}

// Is the user allowed to edit the post or page?
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;
}

// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.

// The data is already in $sp_boxes, but we need to flatten it out.
foreach ( $sp_boxes as $sp_box ) {
    foreach ( $sp_box as $sp_fields ) {
        $my_data[$sp_fields[0]] =  $_POST[$sp_fields[0]];
    }
}

// Add values of $my_data as custom fields
// Let's cycle through the $my_data array!
foreach ($my_data as $key => $value) {
    if ( 'revision' == $post->post_type  ) {
        // don't store custom data twice
        return;
    }

    // if $value is an array, make it a CSV (unlikely)
    $value = implode(',', (array)$value);

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


        // Custom field has a value.
        update_post_meta($post->ID, $key, $value);


    } else {

        // Custom field does not have a value.
        add_post_meta($post->ID, $key, $value);
    }

    if (!$value) {
enter code here
        // delete blanks
        delete_post_meta($post->ID, $key);
    }
}
}`

所有工作正常的上传图片字段将图片名称保存在db中,但图片未移动到上传目录。 任何人都可以帮助我??? 感谢

1 个答案:

答案 0 :(得分:1)

在保存功能中使用此单词按上传功能:

           // Upload the goal image to the uploads directory, resize the image, then upload the resized version
            $goal_image_file = wp_upload_bits( $_FILES['post_media']['name'], null, wp_remote_get( $_FILES['post_media']['tmp_name'] ) );

            // Set post meta about this image. Need the comment ID and need the path.
            if( false == $goal_image_file['error'] ) {

              // Since we've already added the key for this, we'll just update it with the file.
              update_post_meta( $post_id, 'umb_file', $goal_image_file['url'] );

            }