Upload file inside plugins options page

I want to upload images in my custom plugin options page.<form action="?page=slideshow_options" method=&quo

I want to upload images in my custom plugin options page.

<form action="?page=slideshow_options" method="post">
    <input type="file" name="async-upload" id="async-upload">
    <input type="submit" name="html-upload" id="html-upload" class="button" value="Upload">
</form>

If I try to check wether submit button was clicked with isset I get some weird errors. How can I get it to call a function uploadimages() for example?

I want to upload images in my custom plugin options page.

<form action="?page=slideshow_options" method="post">
    <input type="file" name="async-upload" id="async-upload">
    <input type="submit" name="html-upload" id="html-upload" class="button" value="Upload">
</form>

If I try to check wether submit button was clicked with isset I get some weird errors. How can I get it to call a function uploadimages() for example?

Share Improve this question asked May 31, 2012 at 15:32 Badr HariBadr Hari 2894 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

Wow... this one is not a simple one... it's not a matter of calling the magic function uploadimages()... I wish it would :)

I've learned from a tutorial I cannot seem to find (but this one seems fair) and from analyzing other plugins.

The following is just a general orientation, sorry for not making a detailed manual. There may be flaws but hopefully you'll manage to join the pieces and find the way to do your magic.

You'll need to enqueue the following scripts and styles in your plugin's page:

function my_admin_scripts() {
    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');
    wp_register_script('your-plugin-js', THIS_PLUG_DIR . 'js/uploader.js', array('jquery', 'media-upload', 'thickbox'));
    wp_enqueue_script('your-plugin-js');
}

function my_admin_styles() {
    wp_enqueue_style('thickbox');
}

The upload button and the image container.

<?php
$img = (isset($link->link_image) && '' !== $link->link_image) ? '<img src="' . $link->link_image . '" >' : '';
$spanimg = sprintf('<div id="my-link-img">%s</div>', $img);
?>
<a id="upload_image_button" href="#"><?php _e('Set image', 'your-textdomain'); ?></a>
<?php echo $spanimg; ?>

And finally, the jQuery inside the file uploader.js where the trick happens:

jQuery(document).ready(function($) {    
    $('#upload_image_button').click(function() {
        formfield = $('#link_image').attr('name'); 
        tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
        return false;
    });

    window.send_to_editor = function(html) {
        imgurl = $('img',html).attr('src');
        imgsrc = '<img src="'+imgurl+'">';
        $('#link_image').val(imgurl); // this populates a text field with the image url
        $('#my-link-img').html(imgsrc); // this prints the image into a div
        tb_remove();
    }
});

These snippets were taken from a plugin of mine (which needs an update, as I learned quite a few things after releasing it).

Good luck!

File Upload using WordPress Settings API

The register_setting function saves the value attribute on form submission directly into the database. We can also alter or validate the value based on our choice. The register_setting function takes a third argument which is a callback, this callback is fired before it saves the settings into the database.

For uploading files through settings pages we can handle the uploads via the callback and then store the URL to the file in DB. Here is example code showing how to do that:

<?php

function demo_settings_page()
{
    add_settings_section("section", "Section", null, "demo");
    add_settings_field("demo-file", "Demo File", "demo_file_display", "demo", "section");  
    register_setting("section", "demo-file", "handle_file_upload");
}

function handle_file_upload($option)
{
    if(!empty($_FILES["demo-file"]["tmp_name"]))
    {
        $urls = wp_handle_upload($_FILES["demo-file"], array('test_form' => FALSE));
        $temp = $urls["url"];
        return $temp;   
    }

    return $option;
}

function demo_file_display()
{
   ?>
        <input type="file" name="demo-file" /> 
        <?php echo get_option('demo-file'); ?>
   <?php
}

add_action("admin_init", "demo_settings_page");

function demo_page()
{
  ?>
      <div class="wrap">
         <h1>Demo</h1>

         <form method="post" action="options.php">
            <?php
               settings_fields("section");

               do_settings_sections("demo");

               submit_button(); 
            ?>
         </form>
      </div>
   <?php
}

function menu_item()
{
  add_submenu_page("options-general.php", "Demo", "Demo", "manage_options", "demo", "demo_page"); 
}

add_action("admin_menu", "menu_item");

Here is a screenshot of the settings page:

Answer based on this article http://qnimate/file-upload-using-wordpress-settings-api/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745384722a4625387.html

相关推荐

  • Upload file inside plugins options page

    I want to upload images in my custom plugin options page.<form action="?page=slideshow_options" method=&quo

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信