$_FILES 在 update_post_meta WordPress 中返回空

发布于 2025-01-11 15:29:27 字数 8454 浏览 0 评论 0原文

我尝试在保存后使用 $_FILES 访问我的自定义字段文件(图像)值,但它 返回一个空字符串,我尝试使用 $_POST 但它仅返回我想要获取文件名 + mime 类型的文件名 我尝试使用 $FILES[input_name][type] 它也返回一个空值。我尝试了很多解决方案但没有成功

//theme shop menu
class BakentakeShopMenu{
    function __construct(){
        //register shop menu custom post type
        add_action( 'init', array($this,'create_menuitem_cpt'), 1, );
        //register custom fields for shop menu post type
        add_action("add_meta_boxes",[$this,'shop_items_custom_fields'],2);
        
        add_action('save_post',[$this,'save_shop_items_fields']);
        add_action("rest_api_init",[$this,"add_menu_data_to_rest"]);
    }

    // add menu items to the rest api 
    function add_menu_data_to_rest(){
        // register_rest_route("wp/v2","menu_data",[
        //  "methods" => "GET",
        //  "callback" => [$this,"set_menu_data_to_rest_config"]
        // ]);
        register_rest_field("menuitem","menuData",array(
            "get_callback" => [$this,'register_menu_item_data_rest_field'],
            'update_callback' => null,
            'schema' => null,
            )
        );
    }

    function register_menu_item_data_rest_field($post){
        $itemName = get_post_meta($post['id'],'item_name_value',true);
        $itemDescription = get_post_meta($post['id'],'item_description_value',true);
        $itemPrice = get_post_meta($post['id'],'item_price_value',true);
        $itemDiscountPrice = get_post_meta($post['id'],'item_discount_value',true);
        $itemImage = get_post_meta($post['id'],'item_image_value',true);

        return [$itemName,$itemDescription,$itemPrice,$itemDiscountPrice,$itemImage];
    }

// Register Custom Post Type Menu Item
function create_menuitem_cpt() {
    $labels = array(
        'name' => _x( 'Menu Items', 'Post Type General Name', 'textdomain' ),
        'singular_name' => _x( 'Menu Item', 'Post Type Singular Name', 'textdomain' ),
        'menu_name' => _x( 'Menu Items', 'Admin Menu text', 'textdomain' ),
        'name_admin_bar' => _x( 'Menu Item', 'Add New on Toolbar', 'textdomain' ),
        'archives' => __( 'Menu Item Archives', 'textdomain' ),
        'attributes' => __( 'Menu Item Attributes', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Menu Item:', 'textdomain' ),
        'all_items' => __( 'All Menu Items', 'textdomain' ),
        'add_new_item' => __( 'Add New Menu Item', 'textdomain' ),
        'add_new' => __( 'Add New', 'textdomain' ),
        'new_item' => __( 'New Menu Item', 'textdomain' ),
        'edit_item' => __( 'Edit Menu Item', 'textdomain' ),
        'update_item' => __( 'Update Menu Item', 'textdomain' ),
        'view_item' => __( 'View Menu Item', 'textdomain' ),
        'view_items' => __( 'View Menu Items', 'textdomain' ),
        'search_items' => __( 'Search Menu Item', 'textdomain' ),
        'not_found' => __( 'Not found', 'textdomain' ),
        'not_found_in_trash' => __( 'Not found in Trash', 'textdomain' ),
        'featured_image' => __( 'Featured Image', 'textdomain' ),
        'set_featured_image' => __( 'Set featured image', 'textdomain' ),
        'remove_featured_image' => __( 'Remove featured image', 'textdomain' ),
        'use_featured_image' => __( 'Use as featured image', 'textdomain' ),
        'insert_into_item' => __( 'Insert into Menu Item', 'textdomain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this Menu Item', 'textdomain' ),
        'items_list' => __( 'Menu Items list', 'textdomain' ),
        'items_list_navigation' => __( 'Menu Items list navigation', 'textdomain' ),
        'filter_items_list' => __( 'Filter Menu Items list', 'textdomain' ),
    );
    $args = array(
        'label' => __( 'Menu Item', 'textdomain' ),
        'description' => __( '', 'textdomain' ),
        'labels' => $labels,
        'menu_icon' => 'dashicons-cart',
        'supports' => array('thumbnail', 'trackbacks', 'page-attributes', 'post-formats', 'custom-fields'),
        'taxonomies' => array(),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'hierarchical' => false,
        'exclude_from_search' => false,
        'show_in_rest' => true,
        'publicly_queryable' => true,
        'capability_type' => 'post',
    );
    register_post_type( 'menuitem', $args );
}

    //add custom field settings
    function shop_items_custom_fields(){
        add_meta_box(
            'shop_item',
            'shop items',
            array($this,'shop_items_config'),
            'menuitem',
            'normal',
            'low',
            ''

        );
    }
    // save custom field values 
    function save_shop_items_fields($post_id ){
        //item name
            update_post_meta(
                $post_id ,
                "item_name_value",
                $_POST['item_name']
            );
        //item description
            update_post_meta(
                $post_id ,
                "item_description_value",
                $_POST['item_description']
            );
        //item price
            update_post_meta(
                $post_id ,
                "item_price_value",
                $_POST['item_price']
            );
        //discount price
            update_post_meta(
                $post_id ,
                "item_discount_value",
                $_POST['item_price']
            );
        //Image
            update_post_meta(
                $post_id ,
                "item_image_value",
                $_FILES['item_image']
            );          
    }   
    //custom fields config
    function shop_items_config($post){
        $itemName = get_post_meta($post->ID,'item_name_value',true);
        $itemDescription = get_post_meta($post->ID,'item_description_value',true);
        $itemPrice = get_post_meta($post->ID,'item_price_value',true);
        $itemDiscountPrice = get_post_meta($post->ID,'item_discount_value',true);
        $itemImage = get_post_meta($post->ID,'item_image_value',true);

        //upload images
        $wordpress_upload_dir = wp_upload_dir();
        $file_path = $wordpress_upload_dir['path'] . '/' . $itemImage;
        $file = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1200px-Image_created_with_a_mobile_phone.png";
        ?>
        <form action = "" method="POST" enctype="multipart/form-data">
            <h1><?php print_r($itemImage) ?></h1>
            <div class="shop-items-form">
                <div class="shop-items-form-field">
                    <label for="item-name">Item Name</label>
                    <input type="text" name="item_name" value="<?php echo $itemName ?>"/>
                </div>
                <div class="shop-items-form-field">
                    <label for="item-description">Item Description</label>
                    <textarea name="item_description"><?php echo $itemDescription ?></textarea>
                </div>
                <div class="shop-items-form-field__wraper">
                    <div class="shop-items-form-field">
                        <label for="itemPrice">Item Price</label>
                        <input type="number" name="item_price" value="<?php echo $itemPrice ?>"/>
                    </div>
                    <div class="shop-items-form-field">
                        <label for="discount-price">Discount Price</label>
                        <input type="number" name="discount_price" value="<?php echo $itemDiscountPrice ?>"/>
                    </div>
                </div>
                <div class="shop-items-form-field">
                    <label for="discount-price">Add Item Image</label>
                    <input type="file" name="item_image" accept="image/png,image/jpeg" value=<?php $itemImage ?>/>
                </div>
            </div>
        </form>
        <?php
    }
};

new BakentakeShopMenu;

I tried to access my custom field file(image) value using $_FILES on on post save but it
return a empty string, I tried using $_POST but it returns only the name of the file i want to get the file name + mime type And I tried using $FILES[input_name][type] it also return's a empty value. I tried many solutions but didn't work

//theme shop menu
class BakentakeShopMenu{
    function __construct(){
        //register shop menu custom post type
        add_action( 'init', array($this,'create_menuitem_cpt'), 1, );
        //register custom fields for shop menu post type
        add_action("add_meta_boxes",[$this,'shop_items_custom_fields'],2);
        
        add_action('save_post',[$this,'save_shop_items_fields']);
        add_action("rest_api_init",[$this,"add_menu_data_to_rest"]);
    }

    // add menu items to the rest api 
    function add_menu_data_to_rest(){
        // register_rest_route("wp/v2","menu_data",[
        //  "methods" => "GET",
        //  "callback" => [$this,"set_menu_data_to_rest_config"]
        // ]);
        register_rest_field("menuitem","menuData",array(
            "get_callback" => [$this,'register_menu_item_data_rest_field'],
            'update_callback' => null,
            'schema' => null,
            )
        );
    }

    function register_menu_item_data_rest_field($post){
        $itemName = get_post_meta($post['id'],'item_name_value',true);
        $itemDescription = get_post_meta($post['id'],'item_description_value',true);
        $itemPrice = get_post_meta($post['id'],'item_price_value',true);
        $itemDiscountPrice = get_post_meta($post['id'],'item_discount_value',true);
        $itemImage = get_post_meta($post['id'],'item_image_value',true);

        return [$itemName,$itemDescription,$itemPrice,$itemDiscountPrice,$itemImage];
    }

// Register Custom Post Type Menu Item
function create_menuitem_cpt() {
    $labels = array(
        'name' => _x( 'Menu Items', 'Post Type General Name', 'textdomain' ),
        'singular_name' => _x( 'Menu Item', 'Post Type Singular Name', 'textdomain' ),
        'menu_name' => _x( 'Menu Items', 'Admin Menu text', 'textdomain' ),
        'name_admin_bar' => _x( 'Menu Item', 'Add New on Toolbar', 'textdomain' ),
        'archives' => __( 'Menu Item Archives', 'textdomain' ),
        'attributes' => __( 'Menu Item Attributes', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Menu Item:', 'textdomain' ),
        'all_items' => __( 'All Menu Items', 'textdomain' ),
        'add_new_item' => __( 'Add New Menu Item', 'textdomain' ),
        'add_new' => __( 'Add New', 'textdomain' ),
        'new_item' => __( 'New Menu Item', 'textdomain' ),
        'edit_item' => __( 'Edit Menu Item', 'textdomain' ),
        'update_item' => __( 'Update Menu Item', 'textdomain' ),
        'view_item' => __( 'View Menu Item', 'textdomain' ),
        'view_items' => __( 'View Menu Items', 'textdomain' ),
        'search_items' => __( 'Search Menu Item', 'textdomain' ),
        'not_found' => __( 'Not found', 'textdomain' ),
        'not_found_in_trash' => __( 'Not found in Trash', 'textdomain' ),
        'featured_image' => __( 'Featured Image', 'textdomain' ),
        'set_featured_image' => __( 'Set featured image', 'textdomain' ),
        'remove_featured_image' => __( 'Remove featured image', 'textdomain' ),
        'use_featured_image' => __( 'Use as featured image', 'textdomain' ),
        'insert_into_item' => __( 'Insert into Menu Item', 'textdomain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this Menu Item', 'textdomain' ),
        'items_list' => __( 'Menu Items list', 'textdomain' ),
        'items_list_navigation' => __( 'Menu Items list navigation', 'textdomain' ),
        'filter_items_list' => __( 'Filter Menu Items list', 'textdomain' ),
    );
    $args = array(
        'label' => __( 'Menu Item', 'textdomain' ),
        'description' => __( '', 'textdomain' ),
        'labels' => $labels,
        'menu_icon' => 'dashicons-cart',
        'supports' => array('thumbnail', 'trackbacks', 'page-attributes', 'post-formats', 'custom-fields'),
        'taxonomies' => array(),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'hierarchical' => false,
        'exclude_from_search' => false,
        'show_in_rest' => true,
        'publicly_queryable' => true,
        'capability_type' => 'post',
    );
    register_post_type( 'menuitem', $args );
}

    //add custom field settings
    function shop_items_custom_fields(){
        add_meta_box(
            'shop_item',
            'shop items',
            array($this,'shop_items_config'),
            'menuitem',
            'normal',
            'low',
            ''

        );
    }
    // save custom field values 
    function save_shop_items_fields($post_id ){
        //item name
            update_post_meta(
                $post_id ,
                "item_name_value",
                $_POST['item_name']
            );
        //item description
            update_post_meta(
                $post_id ,
                "item_description_value",
                $_POST['item_description']
            );
        //item price
            update_post_meta(
                $post_id ,
                "item_price_value",
                $_POST['item_price']
            );
        //discount price
            update_post_meta(
                $post_id ,
                "item_discount_value",
                $_POST['item_price']
            );
        //Image
            update_post_meta(
                $post_id ,
                "item_image_value",
                $_FILES['item_image']
            );          
    }   
    //custom fields config
    function shop_items_config($post){
        $itemName = get_post_meta($post->ID,'item_name_value',true);
        $itemDescription = get_post_meta($post->ID,'item_description_value',true);
        $itemPrice = get_post_meta($post->ID,'item_price_value',true);
        $itemDiscountPrice = get_post_meta($post->ID,'item_discount_value',true);
        $itemImage = get_post_meta($post->ID,'item_image_value',true);

        //upload images
        $wordpress_upload_dir = wp_upload_dir();
        $file_path = $wordpress_upload_dir['path'] . '/' . $itemImage;
        $file = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1200px-Image_created_with_a_mobile_phone.png";
        ?>
        <form action = "" method="POST" enctype="multipart/form-data">
            <h1><?php print_r($itemImage) ?></h1>
            <div class="shop-items-form">
                <div class="shop-items-form-field">
                    <label for="item-name">Item Name</label>
                    <input type="text" name="item_name" value="<?php echo $itemName ?>"/>
                </div>
                <div class="shop-items-form-field">
                    <label for="item-description">Item Description</label>
                    <textarea name="item_description"><?php echo $itemDescription ?></textarea>
                </div>
                <div class="shop-items-form-field__wraper">
                    <div class="shop-items-form-field">
                        <label for="itemPrice">Item Price</label>
                        <input type="number" name="item_price" value="<?php echo $itemPrice ?>"/>
                    </div>
                    <div class="shop-items-form-field">
                        <label for="discount-price">Discount Price</label>
                        <input type="number" name="discount_price" value="<?php echo $itemDiscountPrice ?>"/>
                    </div>
                </div>
                <div class="shop-items-form-field">
                    <label for="discount-price">Add Item Image</label>
                    <input type="file" name="item_image" accept="image/png,image/jpeg" value=<?php $itemImage ?>/>
                </div>
            </div>
        </form>
        <?php
    }
};

new BakentakeShopMenu;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

豆芽 2025-01-18 15:29:27

尽管您的表单允许文件上传,但它包含在默认的 Wordpress Post 表单中,因此您必须让 Wordpress 知道该表单可能包含文件。默认情况下,此选项在所有帖子表单(帖子、页面、自定义帖子类型等)中处于禁用状态。

<?php
// Add this hook on your init file
    add_action('post_edit_form_tag','update_edit_form'));
// Create the function that allows file uploads on post forms
    function update_edit_form(){
      echo 'enctype="multipart/form-data"';
    }
?>

Although your form allows file uploads, it is contained in the default Wordpress Post form so you have to let Wordpress know that the form may contain files. This option its disabled by default in all post forms (Post, page, custom post type, etc).

<?php
// Add this hook on your init file
    add_action('post_edit_form_tag','update_edit_form'));
// Create the function that allows file uploads on post forms
    function update_edit_form(){
      echo 'enctype="multipart/form-data"';
    }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文