Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Introduction to the WP Filesystem

Posted on July 18, 2012 by Thomas Griffin in Advanced, Member Restricted, Subscriber Only, Tutorials, Writing Plugins 1 Comment
Home» Tutorials » Advanced » Introduction to the WP Filesystem
Tweet
Love It - 0

I've spent a lot of time lately dealing with a little known, under-documented but extremely useful class in WordPress called WP_Filesystem. WP_Filesystem is used throughout WordPress to handle core/plugin/themes updates and various tasks that require writing files to your webserver.

The WP_Filesystem class is a base class that has many extensions depending on what 'method' it can use to do the work it needs to do (install a plugin, upgrade core, move a file, etc.). There are many methods that WP_Filesystem can utilize based on your server setup and permissions:

  • Direct
  • FTP
  • FTP Sockets
  • SSH2

The beauty of how the WP_Filesystem class works is that it chooses the best option based on the user's setup, AND it makes sure that file ownership is correct before it performs any actions. It's a win-win for everyone.

WP_Filesystem Overview
WordPress provides a nifty function called request_filesystem_credentials() that does all the hard work for you in setting up WP_Filesystem.

The request_filesystem_credentials() function accepts 5 parameters:

  • $form_post
  • $method
  • $error
  • $context
  • $extra_fields
  • .

$form_post is the URL in which the resulting form should be posted to. Security is always an issue, so you should be using wp_nonce_url() to build this URL, where the nonce field matches the nonce in the submitted form field. You can easily pass along extra query args here as well using add_query_arg().

$method is the method in which you want WP_Filesystem to use. Because WP_Filesystem automatically determines and populates the best method for use, there's really no need to specify a particular method here unless you are doing it for testing purposes. A good use-case would be to force a particular method and ensure that WP_Filesystem will write files to your webserver correctly if it can't verify the ownership of files.

$error is a boolean to specify whether you want to output an error message if WP_Filesystem fails to connect. It is false by default, but can be helpful to turn on for testing and debugging.

$context is the directory in which you want to test WP_Filesystem so that it can verify ownership of files. By default, it will attempt to write a temporary file to the wp-content directory (specified by the constant WP_CONTENT_DIR). This field can be useful if you want to test the particular directory in which you are about to write files.

$extra_fields are extra $_POST fields from the previous form that should be included in the resulting post form. The $_POST fields must be strings (arrays are not currently accepted - see this ticket for more info).

Now that we have dissected request_filesystem_credentials(), let's put it into action.

You must be logged in and have an active premium membership to view the rest of this content. Register or login from the sidebar.

Thomas Griffin

Thomas Griffin Media is a web design and development company specializing in WordPress solutions for small businesses. I am located right outside of Charlotte, NC and offer WordPress design, development, performance optimization and hosting services to my clients. I love WordPress and use it exclusively to meet my clients’ needs. Whether you need a small online solution or a robust CMS, I can utilize WordPress for your project.
Tweet Follow @pippinsplugins
Thomas Griffin, WP_Filesystem

One comment on “Introduction to the WP Filesystem”

  1. Elliott Stocks says:
    July 20, 2012 at 6:22 pm

    Nice article, I’ve always wondered about the file system. Thanks !

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Win a Developer’s License for Soliloquy, the Best Slider Plugin for WordPress

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (2)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • Love it when I get to use wp_list_pluck() in a real world scenario
    May 22, 2013
  • RT @PicturesEarth: The happiest people don&#039;t have the most things. They just make the most of what they have. http://t.co/eL2cD3CH2B
    May 22, 2013
  • @JS_Zao Woo! I just need to work harder :P
    May 22, 2013

Topics

meta box hook add_shortcode Tom McFarlin get_user_meta featured wp_enqueue_script the_content shortcodes add_options_page campaign monitor contextual help Rémi Corson plugin do_action mail chimp login forms authors short codes attachment Related posts image apply_filters post types bbpress comments recent posts taxonomies short code custom post type images gallery Ajax Stripe taxonomy jquery widgets users add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2011 Pippin's Plugins