PHP:
This data structure tells the script to set up a form similar to this (this isn't exactly it because I am too lazy to find the css in the admin section which controls this stuff):
Property Details
Perhaps, more importantly, this produces the exact same thing we might have done previously with some editing, copying, pasting and, potentially, human error. Below is a listing of the replacement for the Nathan Rice Plugin file: write-panel.php. You can find a file version here. It's on another web site. I'll move it over to this one soon. I am working on a full Plugin, but it will need to include a page template drop down that is not included here and I'd like to make it a PHP class. Feel free to leave comments at the bottom of this page. (You may need a user account to comment.)
PHP:
- <?php
- /*
- Plugin Name: Add Dynamic Meta Boxes
- Plugin URI: http://www.clarksonenergyhomes.com/wordpress/wordpress-plugin-add-dynamic-meta-boxes/
- Description: Allows you to add boxes and fields to the Write Post panel, and store the value as a custom field. Based on script by Nathan Rice (http://www.nathanrice.net/)
- Version: 0.1 (Not rigorously tested.)
- Author: Charles Clarkson
- Author URI: http://www.clarksonenergyhomes.com/wordpress/about/
- */
- /*
- Each box has a name and a set of fields. Currently,
- only text and textarea fields are suppoted. 'text'
- fields are the default.
- To add a box named: "Name Box" with a field named
- "_name", add this:
- 'Name Box' => array (
- array( '_name', 'Name:', 'text' ),
- ),
- You can leave the 'text' field off. It is the default.
- 'Name Box' => array (
- array( '_name', 'Name:' ),
- ),
- */
- // Edit this data structure to change the form in WordPress:
- ),
- ),
- ),
- );
- // Do not edit past this point.
- // Use the admin_menu action to define the custom boxes
- add_action( 'admin_menu', 'sp_add_custom_box' );
- // Use the save_post action to do something with the data entered
- // Save the custom fields
- add_action( 'save_post', 'sp_save_postdata', 1, 2 );
- // Adds a custom section to the "advanced" Post and Page edit screens
- function sp_add_custom_box() {
- global $sp_boxes;
- add_meta_box( $box_name, __( $box_name, 'sp' ), 'sp_post_custom_box', 'post', 'normal', 'high' );
- }
- }
- }
- function sp_post_custom_box ( $obj, $box ) {
- global $sp_boxes;
- // Run once
- if ( ! $sp_nonce_flag ) {
- echo_sp_nonce();
- $sp_nonce_flag = true;
- }
- // Genrate box contents
- foreach ( $sp_boxes[$box['id']] as $sp_box ) {
- }
- }
- function field_html ( $args ) {
- switch ( $args[2] ) {
- case 'textarea':
- return text_area( $args );
- case 'checkbox':
- // To Do
- case 'radio':
- // To Do
- case 'text':
- default:
- return text_field( $args );
- }
- }
- function text_field ( $args ) {
- global $post;
- // adjust data
- $args[2] = get_post_meta($post->ID, $args[0], true);
- $args[1] = __($args[1], 'sp' );
- $label_format =
- '<label for="%1$s">%2$s</label><br />'
- . '<input style="width: 95%%;" type="text" name="%1$s" value="%3$s" /><br /><br />';
- }
- function text_area ( $args ) {
- global $post;
- // adjust data
- $args[2] = get_post_meta($post->ID, $args[0], true);
- $args[1] = __($args[1], 'sp' );
- $label_format =
- '<label for="%1$s">%2$s</label><br />'
- . '<textarea style="width: 95%%;" name="%1$s">%3$s</textarea><br /><br />';
- }
- /* When the post is saved, saves our custom data */
- function sp_save_postdata($post_id, $post) {
- global $sp_boxes;
- // verify this came from the our screen and with proper authorization,
- // because save_post can be triggered at other times
- if ( ! wp_verify_nonce( $_POST['sp_nonce_name'], plugin_basename(__FILE__) ) ) {
- return $post->ID;
- }
- // Is the user allowed to edit the post or page?
- if ( 'page' == $_POST['post_type'] ) {
- if ( ! current_user_can( 'edit_page', $post->ID ))
- return $post->ID;
- } else {
- if ( ! current_user_can( 'edit_post', $post->ID ))
- return $post->ID;
- }
- // OK, we're authenticated: we need to find and save the data
- // We'll put it into an array to make it easier to loop though.
- // The data is already in $sp_boxes, but we need to flatten it out.
- foreach ( $sp_boxes as $sp_box ) {
- foreach ( $sp_box as $sp_fields ) {
- $my_data[$sp_fields[0]] = $_POST[$sp_fields[0]];
- }
- }
- // Add values of $my_data as custom fields
- // Let's cycle through the $my_data array!
- foreach ($my_data as $key => $value) {
- if ( 'revision' == $post->post_type ) {
- // don't store custom data twice
- return;
- }
- // if $value is an array, make it a CSV (unlikely)
- if ( get_post_meta($post->ID, $key, FALSE) ) {
- // Custom field has a value.
- update_post_meta($post->ID, $key, $value);
- } else {
- // Custom field does not have a value.
- add_post_meta($post->ID, $key, $value);
- }
- if (!$value) {
- // delete blanks
- delete_post_meta($post->ID, $key);
- }
- }
- }
- function echo_sp_nonce () {
- // Use nonce for verification ... ONLY USE ONCE!
- '<input type="hidden" name="%1$s" id="%1$s" value="%2$s" />',
- 'sp_nonce_name',
- wp_create_nonce( plugin_basename(__FILE__) )
- );
- }
- // A simple function to get data stored in a custom field
- function get_custom_field($field) {
- global $post;
- $custom_field = get_post_meta($post->ID, $field, true);
- echo $custom_field;
- }
- }
- ?>
No Responses to “ WordPress Plugin — Add Dynamic Meta Boxes ”