简体   繁体   English

WordPress的自定义帖子附件字段

[英]wordpress custom post attachment fields

I am creating custom post fields all text fields are working fine but i am little bit confused in saving attachment field. 我正在创建自定义帖子字段,所有文本字段都工作正常,但是在保存附件字段时我有点困惑。 its saving file name in database but not moving that file in upload directory . 它的保存文件名在数据库中,但不会在上传目录中移动该文件。 在此处输入图片说明

here is the code : 这是代码:

` $sp_boxes = array ( 'Product Details' => array ( `$ sp_boxes = array('Product Details'=> 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 (upload image field is confusing ) add_action( 'admin_menu', 'sp_add_custom_box' ); * Attachemnet(上传图片字段令人困惑) add_action( 'admin_menu', 'sp_add_custom_box' ); // Use the save_post action to do something with the data entered // Save the custom fields add_action( 'save_post', 'sp_save_postdata', 1, 2 ); //使用save_post操作对输入的数据进行处理//保存自定义字段add_action( 'save_post', 'sp_save_postdata', 1, 2 ); // Adds a custom section to the "advanced" Post and Page edit screen` //将自定义部分添加到“高级”帖子和页面编辑屏幕中`

` function sp_add_custom_box() { global $sp_boxes; `function sp_add_custom_box(){全局$ 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 );

} }

` `

  • And this is how i am saving post data 这就是我保存帖子数据的方式

` function sp_save_postdata($post_id, $post) { global $sp_boxes; `function sp_save_postdata($ post_id,$ post){全局$ 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);
    }
}
}`

All are working fine upload image field save the image name in db but image is not moving to upload directory. 一切正常,上传图片字段将图片名称保存在db中,但图片未移至上传目录。 Anyone can help me??? 任何人都可以帮助我吗??? thanks 谢谢

Use this word press upload function in your save function : 在保存功能中使用此词按上传功能:

           // 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'] );

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM