Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 57 lines (35 sloc) 3.486 kb
f68980f @kovshenin preparing for production use
authored
1 # Post Options API
2
3 Howdy and welcome to the Post Options API, an alternative approach at Custom Fields in WordPress. The API makes it extremely easy to show different kinds of extra fields to posts, pages and custom post types in WordPress, with nice looking meta boxes and a whole set of predefined callback functions to generate controls like checkboxes, text fields and more, although you can always use your custom callback.
4
5 If you're familiar with WordPress' Settings API you'll find this extremely easy to use, except of course for the object/classes madness that we created. Let me explain.
6
a0a4bcf @kovshenin readme
authored
7 ## Compatibility Approach
8
f68980f @kovshenin preparing for production use
authored
9 To ensure compatibility with other themes and plugins that might be using the Post Options API (perhaps a different version) we've built in two initialization functions -- `get_post_options_api` to retrieve the API itself and `get_post_options_api_fields` to retrieve the fields helper. Here's how you use them:
10
11 function your_function() {
12
13 // Initialize Post Options API
14 require( dirname( __FILE__ ) . '/inc/post-options-api.1.0.php' );
15 $post_options = get_post_options_api( '1.0' );
16 $post_fields = get_post_options_api_fields( '1.0' );
17 }
18
19 add_action( 'init', 'your_function' );
20
21 And then you can use the `$post_options` and `$post_fields` objects like you normally would, i.e.:
22
23 $post_options->register_post_options_section( ... );
24 $post_options->add_section_to_post_type( ... );
25 $post_options->register_post_option(
26 ...
27 'callback' => $post_fields->checkbox( ... )
28 );
29
30 Hope this explains it and doesn't make it too crazy to use, but we really had to go this way to ensure that the same version is always one, i.e. the class is a singleton, never redefined and never initialized twice (unless you explicitly ask it to of course).
31
a0a4bcf @kovshenin readme
authored
32 ## Usage
33
a008f36 @kovshenin readme minor
authored
34 Grab the latest stable `post-options-api.x.php` file from the `lib` directory, where `x` is a version number. Memorize that version number since you'll need it later on. Copy the file into an include directory in your WordPress theme or plugin, typically called `inc`. In your theme's `functions.php` file or your plugin source file during the WordPress `init` action, load that file and initialize the two objects. I have shown this in the snippet above, but here you go again:
a0a4bcf @kovshenin readme
authored
35
36 function your_function() {
37
38 // Initialize Post Options API
39 require( dirname( __FILE__ ) . '/inc/post-options-api.1.0.php' );
40 $post_options = get_post_options_api( '1.0' );
41 $post_fields = get_post_options_api_fields( '1.0' );
42
43 // Add the fields here
44 }
45
46 add_action( 'init', 'your_function' );
47
48 And `1.0` is that version number you should have memorized. It will return a `WP_Error` if the requested version does not exist so you might want to do a check for `is_wp_error()` before going on, but if you're bundling the API together with your theme or plugin that should never be the case.
49
50 ## Examples
51
52 There's an example directory which you can check out. It's basically a plugin that ads a bunch of options to posts and pages in WordPress. Copy the plugin directory into your `wp-content/plugins` folder and activate it in the WordPress dashboard. You'll see the the madness going on in your Edit Post and Edit Page screens. To find out how that madness has been created inspect the plugin source files. It's quite easy to understand and follow along.
53
54 ## Questions?
55
a008f36 @kovshenin readme minor
authored
56 Good luck using this and if you have any questions, feel free to contact me at kovshenin@gmail.com or kovshenin on Skype. Cheers!
Something went wrong with that request. Please try again.