wpXtreme / wpdk

WordPress Development Kit
http://wpxtreme.github.io/wpdk/
Other
78 stars 19 forks source link

Example for use custom field in the post #26

Closed Mte90 closed 10 years ago

Mte90 commented 10 years ago

Example for add the custom field in the post edit.

gfazioli commented 10 years ago

@Mte90 in your admin class construct use

<?php
// Register meta boxes for post only
add_action( 'add_meta_boxes_post', array( 'WPXYoutubeAdPostMetaBox', 'init' ) );

// Load scripts and style
add_action( 'admin_head-post-new.php', array ( 'WPXYoutubeAdPostMetaBox', 'enqueue' ) );
add_action( 'admin_head-post.php', array ( 'WPXYoutubeAdPostMetaBox', 'enqueue' ) );

// When a post is save/update these actions can be used
add_action( 'save_post', array( 'WPXYoutubeAdPostMetaBox', 'update' ), 10, 3 );

// OR
add_action( 'save_post_post', array( 'WPXYoutubeAdPostMetaBox', 'update' ), 10, 3 );

Then create a file and class as show below

<?php

/**
 * Description
 *
 * @class           WPXyoutubeAdPostMetaBox
 * @author          =undo= <info@wpxtre.me>
 * @copyright       Copyright (C) 2012-2014 wpXtreme Inc. All Rights Reserved.
 * @date            2014-03-19
 * @version         1.0.0
 *
 */
class WPXyoutubeAdPostMetaBox extends WPDKMetaBoxView {

  /**
   * Return a singleton instance of WPXyoutubeAdPostMetaBox class
   *
   * @brief Singleton
   *
   * @return WPXyoutubeAdPostMetaBox
   */
  public static function init()
  {
    static $instance = null;
    if ( is_null( $instance ) ) {
      $instance = new self();
    }

    return $instance;
  }

  /**
   * Create an instance of WPXyoutubeAdPostMetaBox class
   *
   * @brief Construct
   *
   * @return WPXyoutubeAdPostMetaBox
   */
  public function __construct()
  {
    parent::__construct( 'wpxytad', __( 'Hello', WPXYOUTUBEAD_TEXTDOMAIN ), 'post', WPDKMetaBoxContext::NORMAL, WPDKMetaBoxPriority::HIGH );
  }

  /**
   * Load scripts and styles
   *
   * @brief Enqueue
   */
  public static function enqueue()
  {
    WPDKUIComponents::init()->enqueue( WPDKUIComponents::CONTROLS, WPDKUIComponents::TOOLTIP );
  }

  /**
   * The metabox content
   *
   * @brief Display
   */
  public function draw() {

$fields = array(
      'I am a Legend' => array(

        'Hi, Welcome to CLA (Controls Layout Form).',

        array(
          array(
            'type'  => WPDKUIControlType::TEXT,
            'name'  => 'input_a',
            'label' => 'Move mouse hover me',
            'title' => 'I am a tooltip for control WPDKUIControlType::TEXT',
            'value' => ''
          ),
          array(
            'type'        => WPDKUIControlType::TEXT,
            'name'        => 'input_b',
            'label'       => 'With a place holder',
            'placeholder' => 'Eg: Hello',
            'value'       => ''
          )
        ),
      )
    );

$layout = new WPDKUIControlsLayout( $fields );
$layout->display();
  }

public function update( $post_id, $post_object, $update ) {
 // Do not save...
 if ( ( defined( 'DOING_AUTOSAVE' ) && true === DOING_AUTOSAVE ) ||
      ( defined( 'DOING_AJAX' ) && true === DOING_AJAX ) ||
      ( defined( 'DOING_CRON' ) && true === DOING_CRON )
    ) {
   return;
 }
 // Get post type information
 $post_type        = get_post_type();
 $post_type_object = get_post_type_object( $post_type );
 // Exit
 if ( false == $post_type || is_null( $post_type_object ) ) {
   return;
 }
 // This function only applies to the following post_types
 if ( !in_array( $post_type, array( $this->id ) ) ) {
   return;
 }
 // Find correct capability from post_type arguments
 if ( isset( $post_type_object->cap->edit_posts ) ) {
   $capability = $post_type_object->cap->edit_posts;
   // Return if current user cannot edit this post
   if ( !current_user_can( $capability ) ) {
     return;
   }
 }
 // If all ok and post request then update()
 if ( wpdk_is_request_post() ) {

   // Save your post data in post meta
   update_post_meta( $post_id, 'your-meta-key', esc_attr( $_POST['input_a'] ) );
   // ...
 }

}

}
Mte90 commented 10 years ago

+1 This is perfect for the wiki!

Mte90 commented 10 years ago

There is some typo error in this example:

add_action( 'save_post', array( 'WPXYoutubeAdPostMetaBox', 'update', 10, 3 ));

missing last braces

$layout = new WPDKUIControlsLayout( $fields );

fields it's an array not a function

it's missing in the __construct this line

WPDKUIComponents::init()->enqueue( WPDKUIComponents::CONTROLS, WPDKUIComponents::TOOLTIP );

That load the js file for the tooltip

gfazioli commented 10 years ago

@Mte90 fixed first and second typo

For this

WPDKUIComponents::init()->enqueue( WPDKUIComponents::CONTROLS, WPDKUIComponents::TOOLTIP );

This code can be insert a part in action hook ( see right code above )

// For example you can use
// Load scripts and style
add_action( 'admin_head-post-new.php', array ( 'WPXYoutubeAdPostMetaBox', 'enqueue' ) );
add_action( 'admin_head-post.php', array ( 'WPXYoutubeAdPostMetaBox', 'enqueue' ) );

// and crete a static method where you like, for instance in WPXyoutubeAdPostMetaBox class

  /**
   * Load scripts and styles
   *
   * @brief Enqueue
   */
  public static function enqueue()
  {
    WPDKUIComponents::init()->enqueue( WPDKUIComponents::CONTROLS, WPDKUIComponents::TOOLTIP );
  }
Mte90 commented 10 years ago

If i want use the tabs in the meta box? I'm working to a solution for create the custom field on the fly. I have a set of field that need to be cloned (empty) with javascript and for optimize the interface i have think to use the tabs.