Table of Contents
回复内容:
Home Backend Development PHP Tutorial wordPress 如何保存插件中上传的图片到对应插件的目录下面?

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

Jun 06, 2016 pm 08:10 PM
php wordpress

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

<code>// 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 "<p>There was an error uploading your file.</p>";
    } else {
        // The image was uploaded successfully!
        echo "<p>The image was uploaded successfully!</p>";
        add_filter('img', $attachment_id);
//        add_filter('img', 'custom_upload_directory');
    }

} else {
    echo "<p>The security check failed, maybe show the user an error</p>";
    // The security check failed, maybe show the user an error.

}</code>
Copy after login
  1. 列表项目

 <td>
                    <input type="file" name="my_image_upload" id="my_image_upload" multiple>
                    <input type="hidden" name="post_id" id="post_id" value="55">
                    <?php wp_nonce_field('my_image_upload', 'my_image_upload_nonce'); ?>
                    <input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload">

                            
                        


                                                                                                                        
                     <h2 id="回复内容">回复内容:</h2>
                      
                                                            
<p>开发了个插件中有个上传图片的功能,插件下面有个目录是img保存图片的,想问如何课一保存上传的啊图片到这个目录?</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;code&gt;// Check that the nonce is valid, and the user can edit this post.
if (
    isset($_POST['my_image_upload_nonce'], $_POST['post_id'])
    &amp;&amp; wp_verify_nonce($_POST['my_image_upload_nonce'], 'my_image_upload')
    &amp;&amp; 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 &quot;&lt;p&gt;There was an error uploading your file.&lt;/p&gt;&quot;;
    } else {
        // The image was uploaded successfully!
        echo &quot;&lt;p&gt;The image was uploaded successfully!&lt;/p&gt;&quot;;
        add_filter('img', $attachment_id);
//        add_filter('img', 'custom_upload_directory');
    }

} else {
    echo &quot;&lt;p&gt;The security check failed, maybe show the user an error&lt;/p&gt;&quot;;
    // The security check failed, maybe show the user an error.

}&lt;/code&gt;</pre><div class="contentsignin">Copy after login</div></div>
<ol><li><p>列表项目</p></li></ol>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;code&gt; &lt;td&gt;
                    &lt;input type=&quot;file&quot; name=&quot;my_image_upload&quot; id=&quot;my_image_upload&quot; multiple&gt;
                    &lt;input type=&quot;hidden&quot; name=&quot;post_id&quot; id=&quot;post_id&quot; value=&quot;55&quot;&gt;
                    &lt;?php wp_nonce_field('my_image_upload', 'my_image_upload_nonce'); ?&gt;
                    &lt;input id=&quot;submit_my_image_upload&quot; name=&quot;submit_my_image_upload&quot; type=&quot;submit&quot; value=&quot;Upload&quot;&gt;

                            
                        &lt;/td&gt;&lt;/code&gt;</pre><div class="contentsignin">Copy after login</div></div>
</td>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles