Advertisement
Guest User

Wordpress Custom Post Admin Meta Box

a guest
Apr 4th, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.57 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. //Create Custom Post
  5. function rapanui_productos() {
  6.     $labels = array(
  7.         'name'               => _x( 'Productos', 'post type general name' ),
  8.         'singular_name'      => _x( 'Producto', 'post type singular name' ),
  9.         'add_new'            => _x( 'Add New', 'Event' ),
  10.         'add_new_item'       => __( 'Add New Producto' ),
  11.         'edit_item'          => __( 'Edit Producto' ),
  12.         'new_item'           => __( 'New Producto' ),
  13.         'all_items'          => __( 'All Productos' ),
  14.         'view_item'          => __( 'View Producto' ),
  15.         'search_items'       => __( 'Search Productos' ),
  16.         'not_found'          => __( 'No productos found' ),
  17.         'not_found_in_trash' => __( 'No productos found in the Trash' ),
  18.         'parent_item_colon'  => '',
  19.         'menu_name'          => 'Productos'
  20.     );
  21.     $args = array(
  22.         'labels'        => $labels,
  23.         'description'   => 'Productos de Rapa Nui Chocolates',
  24.         'public'        => true,
  25.         'show_ui' => true,
  26.         'capability_type' => 'page',
  27.         'hierarchical' => true,
  28.         'menu_position' => 6,
  29.         'has_archive'   => false,
  30.         'rewrite' => array('slug' => __('productos') , 'with_front' => true ),
  31.         //'rewrite' => false,
  32.         'query_var' => true,
  33.         'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'author','custom-fields', 'revisions','page-attributes','post-formats' ),
  34.     );
  35.     register_post_type( 'productos', $args );
  36. }
  37. add_action( 'init', 'rapanui_productos' );
  38.  
  39. //Add custom messages
  40.  
  41. function productMessages( $messages ) {
  42.     global $post, $post_ID;
  43.     $messages['productos'] = array(
  44.         0 => '',
  45.         1 => sprintf( __('Producto updated. <a href="%s">View product</a>'), esc_url( get_permalink($post_ID) ) ),
  46.         2 => __('Custom field updated.'),
  47.         3 => __('Custom field deleted.'),
  48.         4 => __('Producto updated.'),
  49.         5 => isset($_GET['revision']) ? sprintf( __('Producto restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
  50.         6 => sprintf( __('Producto published. <a href="%s">View Producto</a>'), esc_url( get_permalink($post_ID) ) ),
  51.         7 => __('Producto saved.'),
  52.         8 => sprintf( __('Producto submitted. <a target="_blank" href="%s">Preview Producto</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  53.         9 => sprintf( __('Producto scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Producto</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
  54.         10 => sprintf( __('Producto draft updated. <a target="_blank" href="%s">Preview Producto</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  55.     );
  56.     return $messages;
  57. }
  58. add_filter( 'post_updated_messages', 'productMessages' );
  59.  
  60. // ----------------------------------------------------------------------------------------------
  61. // Start Custom Meta Box Stuff ------------------------------------------------------------------
  62.  
  63. // Admin Meta Box for Products
  64.  
  65. add_action( 'add_meta_boxes', 'product_data_box' );
  66. function product_data_box() {
  67.     add_meta_box(
  68.         'product_data_box',
  69.         __( 'Product Information', 'myplugin_textdomain' ),
  70.         'product_data_box_content',
  71.         'productos',
  72.         'normal',
  73.         'core',
  74.         'low'
  75.     );
  76. }
  77.  
  78.  
  79. // The Product Admin  Metabox
  80.  
  81. function product_data_box_content() {
  82.     global $post;
  83.    
  84.     // Noncename needed to verify where the data originated
  85.     echo '<input type="hidden" name="product_noncename" id="product_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
  86.    
  87.     // Get the location data if its already been entered
  88.     $product_title = get_post_meta($post->ID, 'product_title', true);
  89.     $product_desc = get_post_meta($post->ID, 'product_desc', true);
  90.     $small_img = get_post_meta($post->ID, 'small_img', true);
  91.     $large_img = get_post_meta($post->ID, 'large_img', true);
  92.    
  93.     // Echo out the field
  94.     echo '<label>Titulo del Producto';
  95.     echo '<input type="text" name="product_title" value="' . $product_title  . '" class="widefat" />';
  96.     echo '</label>';
  97.     echo '<label>Descripción del Producto';
  98.     echo '<textarea type="text" name="product_desc" class="widefat" >' . $product_desc . '</textarea>';
  99.     echo '</label>';
  100.     echo '<label>Imagen Pequeña';
  101.     echo '<input type="text" name="small_img"  value="' . $small_img . '" class="widefat" />';
  102.     echo '</label>';
  103.     echo '<label>Imagen Grande';
  104.     echo '<input type="text" name="large_img" value="' . $large_img  . '" class="widefat" />';
  105.     echo '</label>';
  106.  
  107. }
  108.  
  109. // Save the Metabox Data
  110.  
  111. function events_meta_save($post_id, $post) {
  112.    
  113.     // verify this came from the our screen and with proper authorization,
  114.     // because save_post can be triggered at other times
  115.     if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
  116.     return $post->ID;
  117.     }
  118.  
  119.     // Is the user allowed to edit the post or page?
  120.     if ( !current_user_can( 'edit_post', $post->ID ))
  121.         return $post->ID;
  122.  
  123.     // OK, we're authenticated: we need to find and save the data
  124.     // We'll put it into an array to make it easier to loop though.
  125.    
  126.     $events_meta['product_title'] = $_POST['product_title'];
  127.     $events_meta['product_desc'] = $_POST['product_desc'];
  128.     $events_meta['small_img'] = $_POST['small_img'];
  129.     $events_meta['large_img'] = $_POST['large_img'];
  130.    
  131.     // Add values of $events_meta as custom fields
  132.    
  133.     foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
  134.  
  135.         if( $post->post_type == 'revision' ) return; // Don't store custom data twice
  136.  
  137.         $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
  138.  
  139.         if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
  140.  
  141.             update_post_meta($post->ID, $key, $value);
  142.  
  143.         } else { // If the custom field doesn't have a value
  144.             add_post_meta($post->ID, $key, $value);
  145.         }
  146.         if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
  147.     }
  148.  
  149. }
  150.  
  151. // End Custom Meta Box Stuff
  152. //--------------------------------------------------------------------------------------------
  153.  
  154.  
  155.  
  156.  
  157. // Moving the editor lower in the Edit Page bellow my newly created Custom Meta Box
  158.  
  159.     add_action( 'add_meta_boxes', 'action_add_meta_boxes', 0 );
  160.     function action_add_meta_boxes() {
  161.         global $_wp_post_type_features;
  162.             if (isset($_wp_post_type_features['productos']['editor']) && $_wp_post_type_features['productos']['editor']) {
  163.                 unset($_wp_post_type_features['productos']['editor']);
  164.                 add_meta_box(
  165.                     'description_section',
  166.                     __('Description'),
  167.                     'inner_custom_box',
  168.                         'productos', 'normal', 'low'
  169.                 );
  170.             }
  171.     add_action( 'admin_head', 'action_admin_head'); //white background
  172.     }
  173.     function action_admin_head() {
  174.     ?>
  175.     <style type="text/css">
  176.         .wp-editor-container{background-color:#fff;}
  177.     </style>
  178.     <?php
  179.     }
  180.     function inner_custom_box( $post ) {
  181.     echo '<div class="wp-editor-wrap">';
  182.     //the_editor is deprecated in WP3.3, use instead:
  183.     wp_editor($post->post_content, 'content', array('dfw' => true, 'tabindex' => 1) );
  184.     //the_editor($post->post_content);
  185.     echo '</div>';
  186.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement