QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Bitcoin BankingBuy, Sell, Store and Earn Interest
  • Home
  • QIdea
  • QTrack
Home Carbon Ads File Upload using WordPress Settings API

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 dbms. 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

rsz_screen_shot_2015-03-31_at_80125_pm

Mar 31, 2015Narayan Prusty
WordPress Settings API Validation and SanitizationHTTP/2 for WordPress Sites
Comments: 3
  1. zsolt
    3 years ago

    Second useful tutorial from you. Thanks a lot.

    ReplyCancel
  2. Xzaara
    5 years ago

    I think that the form tag should have additional attribute: enctype=”multipart/form-data”.

    ReplyCancel
  3. Nick Pocock
    5 years ago

    Could you explain how/why you pass the parameter $option and where it comes from / what happens when you return it?

    ReplyCancel

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

Narayan Prusty

I am a full-stack web developer. I specialize in Blockchain and JavaScript. This is my personal blog where I write about programming and technologies that I learn and feel interesting to share.

5 years ago 3 Comments WordPress
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
Related Articles
  • Add Select Tag using WordPress Settings API
  • Add Checkbox using WordPress Settings API
  • Add Radio Buttons using WordPress Settings API
  • WordPress Settings API Tutorial with Examples
  • Customizing WordPress Admin Profile Page
Our Sponsor
Freelance: I am available
@narayanprusty
Hi, I am a full-stack developer - focusing on JavaScript, WordPress, Blockchain, DevOps, Serverless and Cordova. You can outsource your app development to me.





Will get back to you soon!!!
WordPress
ReactJS
Meteor
Blockchain
Serverless
Kubernetes
DevOps
DB & Storage
Ejabberd
Let's get started
My Books

2014 - 2015 © QNimate
All tutorials MIT license