Book Image

WordPress Plugin Development: Beginner's Guide

Book Image

WordPress Plugin Development: Beginner's Guide

Overview of this book

If you can write WordPress plug-ins, you can make WordPress do just about anything. From making the site easier to administer, to adding the odd tweak or new feature, to completely changing the way your blog works, plug-ins are the method WordPress offers to customize and extend its functionality. This book will show you how to build all sorts of WordPress plug-ins: admin plug-ins, Widgets, plug-ins that alter your post output, present custom "views" of your blog, and more. WordPress Plug-in Development (Beginner's Guide) focuses on teaching you all aspects of modern WordPress development. The book uses real and published WordPress plug-ins and follows their creation from the idea to the finishing touches, in a series of carefully picked, easy-to-follow tutorials. You will discover how to use the WordPress API in all typical situations, from displaying output on the site in the beginning to turning WordPress into a CMS in the last chapter. In Chapters 2 to 7 you will develop six concrete plug-ins and conquer all aspects of WordPress development. Each new chapter and each new plug-in introduces different features of WordPress and how to put them to good use, allowing you to gradually advance your knowledge. This book is written as a guide to take your WordPress skills from the very beginning to the level where you are able to completely understand how WordPress works and how you can use it to your advantage.
Table of Contents (14 chapters)
WordPress Plugin Development
Credits
About the Author
About the Reviewer
Preface
Index

Handling file and image uploads


WordPress provides ready functions for handling file uploads and image manipulation.

It is always best practice to use built-in WordPress functions for checking the file, moving it and setting file permissions. We will use these functions in the following example.

Time for action — Handle uploaded image

  1. Let's add a function to handle images uploaded by the user. The function uses the built-in wp_handle_upload() function:

    function handle_image_upload($upload)
    {
    // check if image
    if (file_is_displayable_image( $upload['tmp_name'] ))
    {
    // handle the uploaded file
    $overrides = array('test_form' => false);
    $file=wp_handle_upload($upload, $overrides);
    }
    return $file;
    }
    
  2. Now, we need to include the image to our post. Let's expand the display_form() function to include handling of uploaded images and inserting a new post with attachment:

// Display the Post form
function display_form() {
global $wpdb;
$page=$_GET['page'];
$published=isset($_POST['publish']);
$title...