我在 wordpress 中创建了一个元框,用于将照片上传到我的插件自定义目录。我使用了以下代码
<form method="post" enctype="multipart/form-data">
<input type="file" name="taggr_upload">
</form>
这是我在add_meta_box()
方法中创建的表格
然后我添加了这个 save_post 动作钩子
add_action( 'save_post', 'boj_mbe_save_meta' );
function boj_mbe_save_meta( $post_id ) {
move_uploaded_file($_FILES['taggr_upload']['tmp_name'], 'photo/'. basename( $_FILES['taggr_upload']['name'] ));
}
为什么打开我的文件夹时它没有保存到我的照片文件夹中?
这是完整的代码:
<?php
/*
Plugin Name: random plug
Plugin URI: http://example.com/wordpress-plugins/my-plugin
Description: A plugin demonstrating Cron in WordPress
Version: 1.0
Author: Brad Williams
Author URI: http://wrox.com
License: GPLv2
*/
add_action('init', 'register_tagging_post');
function register_tagging_post(){
$tagging_args = array(
'public' => true,
'supports' => array(
'title',
'thumbnail'
),
'query_var' => 'tagging',
'rewrite' => array(
'slug' => 'tagging',
'with_front' => false
),
'labels' => array(
'name' => 'Albums',
'singular_name' => 'Album',
'add_new' => 'Add New Album',
'add_new_item' => 'Add New Album',
'edit_item' => 'Edit Album',
'new_item' => 'New Album',
'view_item' => 'View Album',
'search_items' => 'Search Albums',
'not_found' => 'No Albums Found',
'not_found_in_trash' => 'No Albums Found In Trash'
),
);
register_post_type('tagging', $tagging_args);
}
//add metabox
add_action( 'add_meta_boxes', 'boj_mbe_create' );
function boj_mbe_create() {
//create a custom meta box
add_meta_box( 'boj-meta', 'My Custom Meta Box', 'boj_mbe_function', 'tagging', 'normal', 'high' );
}
function boj_mbe_function( $post ) {
//retrieve the meta data values if they exist
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="taggr_upload">
</form>
<?php
echo WP_PLUGIN_DIR;
//hook to save the meta box data
add_action( 'save_post', 'boj_mbe_save_meta' );
function boj_mbe_save_meta( $post_id ) {
//verify the meta data is set
if ( isset( $_POST['taggr_upload'] ) ) {
//save the meta data
$path_to_plugin = WP_PLUGIN_DIR . '/yeah';
move_uploaded_file($_FILES['taggr_upload']['tmp_name'], $path_to_plugin . '/photo/'. basename( $_FILES['taggr_upload']['name'] ));
}
}
//create custom post column
}
?>
为什么它不起作用?