当前位置:Gxlcms > PHP教程 > wordPress如何保存插件中上传的图片到对应插件的目录下面?

wordPress如何保存插件中上传的图片到对应插件的目录下面?

时间:2021-07-01 10:21:17 帮助过:6人阅读

开发了个插件中有个上传图片的功能,插件下面有个目录是img保存图片的,想问如何课一保存上传的啊图片到这个目录?

// Check that the nonce is valid, and the user can edit this post.
if (
    isset($_POST['my_image_upload_nonce'], $_POST['post_id'])
    && wp_verify_nonce($_POST['my_image_upload_nonce'], 'my_image_upload')
    && current_user_can('edit_post', $_POST['post_id'])
) {
    // The nonce was valid and the user has the capabilities, it is safe to continue.

    // These files need to be included as dependencies when on the front end.
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');

    // Let WordPress handle the upload.
    // Remember, 'my_image_upload' is the name of our file input in our form above.
    $url=WPWSL_PLUGIN_URL . '/img/' . $imageSize . '.png';
//    update_option('upload_path',WP_CONTENT_DIR.'/uploads');
    update_option('upload_path',WPWSL_PLUGIN_URL.'/img/');
    $attachment_id = media_handle_upload('my_image_upload', $_POST['post_id']);

    if (is_wp_error($attachment_id)) {
        // There was an error uploading the image.
        echo "

There was an error uploading your file.

"; } else { // The image was uploaded successfully! echo "

The image was uploaded successfully!

"; add_filter('img', $attachment_id); // add_filter('img', 'custom_upload_directory'); } } else { echo "

The security check failed, maybe show the user an error

"; // The security check failed, maybe show the user an error. }
  1. 列表项目

 
                    
                    
                    
                    

回复内容:

开发了个插件中有个上传图片的功能,插件下面有个目录是img保存图片的,想问如何课一保存上传的啊图片到这个目录?

// Check that the nonce is valid, and the user can edit this post.
if (
    isset($_POST['my_image_upload_nonce'], $_POST['post_id'])
    && wp_verify_nonce($_POST['my_image_upload_nonce'], 'my_image_upload')
    && current_user_can('edit_post', $_POST['post_id'])
) {
    // The nonce was valid and the user has the capabilities, it is safe to continue.

    // These files need to be included as dependencies when on the front end.
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');

    // Let WordPress handle the upload.
    // Remember, 'my_image_upload' is the name of our file input in our form above.
    $url=WPWSL_PLUGIN_URL . '/img/' . $imageSize . '.png';
//    update_option('upload_path',WP_CONTENT_DIR.'/uploads');
    update_option('upload_path',WPWSL_PLUGIN_URL.'/img/');
    $attachment_id = media_handle_upload('my_image_upload', $_POST['post_id']);

    if (is_wp_error($attachment_id)) {
        // There was an error uploading the image.
        echo "

There was an error uploading your file.

"; } else { // The image was uploaded successfully! echo "

The image was uploaded successfully!

"; add_filter('img', $attachment_id); // add_filter('img', 'custom_upload_directory'); } } else { echo "

The security check failed, maybe show the user an error

"; // The security check failed, maybe show the user an error. }
  1. 列表项目

 
                    
                    
                    
                    

人气教程排行