WordPress.org

Support

Support » Plugins and Hacks » Custom Write Panels

Custom Write Panels

  • Devin Price
    Member

    @downstairsdev

    I’m having trouble getting my custom meta item to show up in the text area where it was saved after updating the page. (However, it does save as custom post meta and shows up in the custom fields area.)

    Everything is based on this tutorial:http://wefunction.com/2008/10/tutorial-create-custom-write-panels-in-wordpress.

    Any help is appreciated.

    <?php
    
    /*
    Plugin Name: Custom Write Panel
    Plugin URI: http://wefunction.com/2008/10/tutorial-create-custom-write-panels-in-wordpress
    Description: Allows custom fields to be added to the WordPress Post Page
    Version: 1.0
    Author: Spencer
    Author URI: http://wefunction.com
    /* ----------------------------------------------*/  
    
    $new_meta_boxes =
    array(
    "textarea" => array(
    "name" => "scontent",
    "std" => "",
    "title" => "Sidebar Content",
    "description" => "Add content to display in the sidebar.")
    );
    
    function new_meta_boxes() {
    	global $post, $new_meta_boxes;  
    
    	foreach($new_meta_boxes as $meta_box) {
    		$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);  
    
    		if($meta_box_value == "")
    			$meta_box_value = $meta_box['std'];  
    
    			echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';  
    
    			echo'<p>'.$meta_box['title'].'</p>';  
    
    			echo'<textarea name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" rows="1" cols="40" style="margin:0;height:4em;width:98%;"></textarea><br />';  
    
    			echo'<p><label for="'.$meta_box['name'].'_value">'.$meta_box['description'].'</label></p>';
    		}
    	}  
    
    function create_meta_box() {
    	global $theme_name;
    	if ( function_exists('add_meta_box') ) {
    		add_meta_box( 'new-meta-boxes', 'Custom Settings', 'new_meta_boxes', 'page', 'normal', 'high' );
    	}
    }  
    
    function save_postdata( $post_id ) {
    	global $post, $new_meta_boxes;  
    
    	foreach($new_meta_boxes as $meta_box) {
    		// Verify
    		if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
    			return $post_id;
    		}  
    
    		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;
    			}  
    
    	$data = $_POST[$meta_box['name'].'_value'];  
    
     	if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
     		add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
     	elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
     		update_post_meta($post_id, $meta_box['name'].'_value', $data);
     	elseif($data == "")
     		delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
    	}
    }  
    
    add_action('admin_menu', 'create_meta_box');
    add_action('save_post', 'save_postdata');
    
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hey Devin, Have you tried the following for your textarea element:

    echo'<textarea name="'.$meta_box['name'].'_value" rows="1" cols="40" style="margin:0;height:4em;width:98%;">' . $meta_box_value . '</textarea><br />';

    I don’t think they work with value attributes 🙂

    Devin Price
    Member

    @downstairsdev

    I owe you a beer.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Write Panels’ is closed to new replies.