2014-01-06 44 views
0

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

这里是代码:

`$ sp_boxes =阵列( '商品详情'=>数组(

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); //向“高级”Po添加自定义部分st和Page编辑屏幕'

`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); 

}

`

  • 这就是我如何保存后的数据

`函数sp_save_postdata($ POST_ID,$后){ 全球$ 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); 
    } 
} 
}` 

所有工作正常上传影像领域保存图像名称数据库,但图像是不动的上传目录。 任何人都可以帮我吗? 感谢

+0

格式我们的代码正确。 –

+0

@BindiyaPatoliya现在我认为你可以检查它... – naCheex

+0

它的紧急请帮助我 – naCheex

回答

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

      } 
+0

我试过这是没有文件进入上传目录.. – naCheex

+0

'post_media'是输入标签的名称? (在我的情况下,它会是%1 $ s)? – naCheex

相关问题