Advertisement
bedas

Example Custom Field Type

Aug 24th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.31 KB | None | 0 0
  1. <?php
  2. /*
  3.  
  4.   This is example how to add simple Google Maps field type.
  5.   Most of functions are called automatically from Types plugin.
  6.   Functions naming conventions are:
  7.  
  8.   - For basic type data (required) callback
  9.   wpcf_fields_$myfieldname()
  10.  
  11.   Optional
  12.  
  13.   - Group form data callback
  14.   wpcf_fields_$myfieldname_insert_form()
  15.  
  16.   - Post edit page form callback
  17.   wpcf_fields_$myfieldname_meta_box_form()
  18.  
  19.   - Editor popup callback
  20.   wpcf_fields_$myfieldname_editor_callback()
  21.  
  22.   - View function callback
  23.   wpcf_fields_$myfieldname_view()
  24.  
  25.  */
  26.  
  27. // Add registration hook
  28. add_filter( 'types_register_fields', 'my_types' );
  29.  
  30. /**
  31.  * Register custom post type on 'types_register_fields' hook.
  32.  *
  33.  * @param array $fields
  34.  * @return type
  35.  */
  36. function my_types( $fields ) {
  37.     $fields['google_map'] = __FILE__;
  38.     return $fields;
  39. }
  40.  
  41. /**
  42.  * Define field.
  43.  *
  44.  * @return type
  45.  */
  46. function wpcf_fields_google_map() {
  47.     return array(
  48.         'path' => __FILE__, // This is deprecated but should be tested for safe removal
  49.         'id' => 'google_map',
  50.         'title' => __( 'Google Map', 'wpcf' ),
  51.         'description' => __( 'This is additional field', 'wpcf' ),
  52.         /*
  53.          * Validation
  54.          *
  55.          * TODO Elaborate on this
  56.          * Add examples for various usage (review needed)
  57.          */
  58.         'validate' => array('required'),
  59.         /*
  60.          *
  61.          *
  62.          *
  63.          *
  64.          *
  65.          *
  66.          * Possible (optional) parameters
  67.          */
  68.         // Additional JS on post edit page
  69.         'meta_box_js' => array(// Add JS when field is active on post edit page
  70.             'wpcf-jquery-fields-my-field' => array(
  71.                 'inline' => 'wpcf_fields_google_map_meta_box_js_inline', // This calls function that renders JS
  72.                 'deps' => array('jquery'), // (optional) Same as WP's enqueue_script() param
  73.                 'in_footer' => true, // (optional) Same as WP's enqueue_script() param
  74.             ),
  75.             /**
  76.              * example how to add javascript file
  77.              *
  78.             'wpcf-jquery-fields-my-field' => array(
  79.                 'src' => get_stylesheet_directory_uri() . '/js/my-field.js', // This will load JS file
  80.             ),
  81.              */
  82.         ),
  83.         // Additional CSS on post edit page
  84.         'meta_box_css' => array(
  85.             'wpcf-jquery-fields-my-field' => array(
  86.                 'src' => get_stylesheet_directory_uri() . '/css/my-field.css', // or inline function 'inline' => $funcname
  87.                 'deps' => array('somecss'), // (optional) Same as WP's enqueue_style() param
  88.             ),
  89.         ),
  90.         // Additional JS on group edit page
  91.         'group_form_js' => array(// Add JS when field is active on post edit page
  92.             /**
  93.              * example how to add javascript file wit callback fundtion
  94.              *
  95.             'wpcf-jquery-fields-my-field' => array(
  96.                 'inline' => 'wpcf_fields_google_map_group_form_js_inline', // This calls function that renders JS
  97.                 'deps' => array('jquery'), // (optional) Same as WP's enqueue_script() param
  98.                 'in_footer' => true, // (optional) Same as WP's enqueue_script() param
  99.             ),
  100.              */
  101.             /**
  102.              * example how to add javascript file
  103.              *
  104.             'wpcf-jquery-fields-my-field' => array(
  105.                 'src' => get_stylesheet_directory_uri() . '/js/my-field.js', // This will load JS file
  106.             ),
  107.              */
  108.         ),
  109.         // Additional CSS on post edit page
  110.         'group_form_css' => array(
  111.             'wpcf-jquery-fields-my-field' => array(
  112.                 'src' => get_stylesheet_directory_uri() . '/css/my-field.css', // or inline function 'inline' => $funcname
  113.                 'deps' => array('somecss'), // (optional) Same as WP's enqueue_style() param
  114.             ),
  115.         ),
  116.         // override editor popup link (you must then load JS function that will process it)
  117. //        'editor_callback' => 'wpcfFieldsMyFieldEditorCallback(\'%s\')', // %s will inject field ID
  118.         // meta key type
  119.         'meta_key_type' => 'INT',
  120.         // Required WP version check
  121.         'wp_version' => '3.3',
  122.     );
  123. }
  124.  
  125. /**
  126.  * Types Group edit screen form.
  127.  *
  128.  * Here you can specify all additional group form data if nedded,
  129.  * it will be auto saved to field 'data' property.
  130.  *
  131.  * @return string
  132.  */
  133. function wpcf_fields_google_map_insert_form() {
  134.     $form['additional'] = array(
  135.         '#type' => 'textfield',
  136.         '#description' => 'Add some comment',
  137.         '#name' => 'comment',
  138.     );
  139.     return $form;
  140. }
  141.  
  142. /**
  143.  * Overrides form output in meta box on post edit screen.
  144.  */
  145. function wpcf_fields_google_map_meta_box_form( $data ) {
  146.     $form['name'] = array(
  147.         '#name' => 'wpcf[' . $data['slug'] . ']', // Set this to override default output
  148.         '#type' => 'textfield',
  149.         '#title' => __( 'Add Google Map coordinates', 'wpcf' ),
  150.         '#description' => __( 'Your input should look something like "41.934146,12.455821"', 'wpcf' )
  151.     );
  152.     return $form;
  153. }
  154.  
  155. /**
  156.  * Adds editor popup callnack.
  157.  *
  158.  * This form will be showed in editor popup
  159.  */
  160. function wpcf_fields_google_map_editor_callback( $field, $settings ) {
  161.     ob_start();
  162.  
  163.     ?>
  164.     <label><input type="text" name="width" value="<?php echo isset( $settings['width'] ) ? $settings['width'] : '425'; ?>" />&nbsp;<?php _e( 'Width', 'wpcf' ); ?></label>
  165.     <br />
  166.     <label><input type="text" name="height" value="<?php echo isset( $settings['height'] ) ? $settings['height'] : '350'; ?>" />&nbsp;<?php _e( 'Height', 'wpcf' ); ?></label>
  167.     <?php
  168.     $form = ob_get_contents();
  169.     ob_get_clean();
  170.     return array(
  171.         'tabs' => array(
  172.             'display' => array(
  173.                 'menu_title' => __( 'Display', 'wpcf' ),
  174.                 'title' => __( 'Display', 'wpcf' ),
  175.                 'content' => $form,
  176.             )
  177.         )
  178.     );
  179. }
  180.  
  181. /**
  182.  * Processes editor popup submit
  183.  */
  184. function wpcf_fields_google_map_editor_submit( $data, $field ) {
  185.     $add = '';
  186.  
  187.     // Add parameters
  188.     if ( !empty( $data['width'] ) ) {
  189.         $add .= ' width="' . strval( $data['width'] ) . '"';
  190.     }
  191.     if ( !empty( $data['height'] ) ) {
  192.         $add .= ' height="' . strval( $data['height'] ) . '"';
  193.     }
  194.  
  195.     // Generate and return shortcode
  196.     return wpcf_fields_get_shortcode( $field, $add );
  197. }
  198.  
  199. /**
  200.  * Renders view
  201.  *
  202.  * Useful $data:
  203.  * $data['field_value'] - Value of custom field
  204.  *
  205.  * @param array $data
  206.  */
  207. function wpcf_fields_google_map_view( $data ) {
  208.     $data['width'] = !empty( $data['width'] ) ? $data['width'] : 425;
  209.     $data['height'] = !empty( $data['height'] ) ? $data['height'] : 350;
  210.     return '<iframe width="' . $data['width'] . '" height="' . $data['height']
  211.             . '" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?q='
  212.             . $data['field_value']
  213.             . '&amp;num=1&amp;vpsrc=0&amp;hl=en&amp;ie=UTF8&amp;t=m&amp;z=14&amp;ll='
  214.             . $data['field_value']
  215.             . '&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?q='
  216.             . $data['field_value']
  217.             . '&amp;num=1&amp;vpsrc=0&amp;hl=en&amp;ie=UTF8&amp;t=m&amp;z=14&amp;ll='
  218.             . $data['field_value']
  219.             . '&amp;source=embed" style="color:#0000FF;text-align:left">'
  220.             . __( 'View Larger Map', 'wpcf' )
  221.             . '</a></small><br />';
  222. }
  223.  
  224. function WPToolset_Field_Google_Map_loader()
  225. {
  226.  
  227.     if ( class_exists('WPToolset_Field_Google_Map' ) ) {
  228.         return;
  229.     }
  230.  
  231.     class WPToolset_Field_Google_Map extends FieldFactory
  232.     {
  233.         public function metaform()
  234.         {
  235.             $attributes =  $this->getAttr();
  236.  
  237.             $metaform = array();
  238.             $metaform[] = array(
  239.                 '#type' => 'textfield',
  240.                 '#title' => $this->getTitle(),
  241.                 '#description' => $this->getDescription(),
  242.                 '#name' => $this->getName(),
  243.                 '#value' => $this->getValue(),
  244.                 '#validate' => $this->getValidationData(),
  245.                 '#repetitive' => $this->isRepetitive(),
  246.                 '#attributes' => $attributes,
  247.             );
  248.             return $metaform;
  249.         }
  250.  
  251.     }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement