Book Image

WordPress 3 Plugin Development Essentials

Book Image

WordPress 3 Plugin Development Essentials

Overview of this book

WordPress is one of the most popular platforms for building blogs and general websites. By learning how to develop and integrate your own plugins, you can add functionality and extend WordPress in any way imaginable. By tapping into the additional power and functionality that plugins provide, you can make your site easier to administer, add new features, or even alter the very nature of how WordPress works. Covering WordPress version 3, this book makes it super easy for you to build a variety of plugins.WordPress 3 Plugin Development Essentials is a practical hands-on tutorial for learning how to create your own plugins for WordPress. Using best coding practices, this book will walk you through the design and creation of a variety of original plugins.WordPress 3 Plugin Development Essentials focuses on teaching you all aspects of modern WordPress development. The book uses real and published WordPress plugins and follows their creation from the idea to the finishing touches in a series of easy-to-follow and informative steps. You will discover how to deconstruct an existing plugin, use the WordPress API in typical scenarios, hook into the database, version your code with SVN, and deploy your new plugin to the world.Each new chapter introduces different features of WordPress and how to put them to good use, allowing you to gradually advance your knowledge. WordPress 3 Plugin Development Essentials is packed with information, tips, and examples that will help you gain comfort and confidence in your ability to harness and extend the power of WordPress via plugins.
Table of Contents (19 chapters)
WordPress 3 Plugin Development Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

PHP functions


There are over 700 functions built into PHP. The following are seven that we used, with examples.

dirname

string dirname( string $path); 

Returns parent directory's path. Often this is used to determine the path to the current script.

Example:

print dirname(__FILE__); // prints something like '/users/me'

file_get_contents

string file_get_contents( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )

Reads an entire file into a string. This is a useful way to read static, non-PHP files. It can even work for downloading remote files, but it's better to tie into the curl functions for serious downloading.

preg_match

int preg_match( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

The preceding code performs a regular expression match using Perl-compatible regular expressions.

Example:

if ( preg_match('/^wp_/', $post_type) ){
   print 'Post type cannot begin with wp_';
}

Note

Special codes are used to signify different things:

  • ^ = The beginning of the string.

  • $ = The end of the string.

  • [0-9] = Any digit, 0-9.

  • [a-z] = Any lowercase letter, a-z. You can make the search case-insensitive by using the "i" flag.

  • .* = Shorthand for any character.

preg_replace

mixed preg_replace('/[^a-z|_]/', '_', $sanitized['post_type']);

Performs a regular expression search and replaces using Perl-compatible regular expressions.

Example:

$string = 'The dog ate my homework';
$pattern = '/dog/i';
$replacement = 'bear';
echo preg_replace($pattern, $replacement, $string);

Returns:

"The bear ate my homework"

print_r

mixed print_r( mixed $expression [, bool $return = false ] )

Prints human-readable information about a variable. This is extremely useful for debugging.

Example:

$x = array( 'x' => 'Something',
       'y' => array('a' => 'alpha')
);
print_r($x);

Output:

Array
(
    [x] => Something
    [y] => Array
        (
            [a] => alpha
        )
   
)

sprintf

string sprintf( string $format [, mixed $args [, mixed $... ]] )

Returns a string produced according to the formatting string format. This function helps to avoid sloppy PHP concatenations. Mostly, we have used only the string types, marked by the %s placeholder, but there are others available. If your format string uses two or more placeholders, you can make use of the syntax for argument-swapping.

Example:

$format = 'The %1$s contains %2$s monkeys';
$output = sprintf($format, 'zoo', 'many');

strtolower

string strtolower( string $str )

Makes a string lowercase. This is a simple text formatting tool.

substr

string substr( string $string , int $start [, int $length ] )

Returns part of a string.

Example:

// Get the first 20 characters of a long string:
$short_str = substr($long_str, 0, 20);