具有元盒子实现的模块

时间:2011-11-14 作者:giorgio79

我正在寻找一些实现add meta box的模块,如图所示here 也许需要一些ajax魔术。

如果你有任何模块,我可以签出将是欢迎的。

2 个回复
最合适的回答,由SO网友:Bainternet 整理而成

我最近发布了一个名为My Meta Box 它负责元盒的大部分创建和数据保存,这是Rilwis从元盒脚本中派生出来的。

使用该类很简单,例如:

<?php

require_once("meta-box-class/my-meta-box-class.php");
if (is_admin()){
/*
* prefix of meta keys, optional
* use underscore (_) at the beginning to make keys hidden, for example $prefix = \'_ba_\';
* you also can make prefix empty to disable it
*
*/
$prefix = \'ba_\';
/*
* configure your meta box
*/
$config = array(
\'id\' => \'demo_meta_box\', // meta box id, unique per meta box
\'title\' => \'Demo Meta Box\', // meta box title
\'pages\' => array(\'post\', \'page\'), // post types, accept custom post types as well, default is array(\'post\'); optional
\'context\' => \'normal\', // where the meta box appear: normal (default), advanced, side; optional
\'priority\' => \'high\', // order of meta box: high (default), low; optional
\'fields\' => array(), // list of meta fields (can be added by field arrays)
\'local_images\' => false // Use local or hosted images (meta box images for add/remove)
);
/*
* Initiate your meta box
*/
$my_meta = new AT_Meta_Box($config);

/*
* Add fields to your meta box
*/

//text field
$my_meta->addTextField($prefix.\'text_field_id\',array(\'name\'=> \'My Text Field\'));
//textarea field
$my_meta->addTextareaField($prefix.\'textarea_field_id\',array(\'name\'=> \'My Textarea Field\'));
//checkbox field
$my_meta->addCheckboxField($prefix.\'checkbox_field_id\',array(\'name\'=> \'My Checkbox Field\'));
//select field
$my_meta->addSelectField($prefix.\'select_field_id\',array(\'selectkey1\'=>\'Select Value1\',\'selectkey2\'=>\'Select Value2\'),array(\'name\'=> \'My select Field\', \'std\'=> array(\'selectkey2\')));
//checkboxList
$my_meta->addCheckboxListField($prefix.\'ch_list_field_id\',array(\'selectkey1\'=>\'Select Value1\',\'selectkey2\'=>\'Select Value2\'),array(\'name\'=> \'My CheckBox List Field\', \'std\'=> array(\'selectkey2\')));
//radio field
$my_meta->addRadioField($prefix.\'radio_field_id\',array(\'radiokey1\'=>\'Radio Value1\',\'radiokey2\'=>\'Radio Value2\'),array(\'name\'=> \'My Radio Filed\', \'std\'=> array(\'radionkey2\')));
//date field
$my_meta->addDateField($prefix.\'date_field_id\',array(\'name\'=> \'My Date Field\'));
//Time field
$my_meta->addTimeField($prefix.\'time_field_id\',array(\'name\'=> \'My Time Field\'));
//Color field
$my_meta->addColorField($prefix.\'color_field_id\',array(\'name\'=> \'My Color Field\'));
//Image field
$my_meta->addImageField($prefix.\'image_field_id\',array(\'name\'=> \'My Image Field\'));
//file upload field
$my_meta->addFileField($prefix.\'file_field_id\',array(\'name\'=> \'My File Field\'));
//wysiwyg field
$my_meta->addWysiwygField($prefix.\'wysiwyg_field_id\',array(\'name\'=> \'My wysiwyg Editor Field\'));
//taxonomy field
$my_meta->addTaxonomyField($prefix.\'taxonomy_field_id\',array(\'taxonomy\' => \'category\'),array(\'name\'=> \'My Taxonomy Field\'));
//posts field
$my_meta->addPostsField($prefix.\'posts_field_id\',array(\'post_type\' => \'post\'),array(\'name\'=> \'My Posts Field\'));

/*
* To Create a reapeater Block first create an array of fields
* use the same functions as above but add true as a last param
*/

$repeater_fields[] = $my_meta->addTextField($prefix.\'text_field_id\',array(\'name\'=> \'My Text Field\'),true);
$repeater_fields[] = $my_meta->addTextareaField($prefix.\'textarea_field_id\',array(\'name\'=> \'My Textarea Field\'),true);
$repeater_fields[] = $my_meta->addCheckboxField($prefix.\'checkbox_field_id\',array(\'name\'=> \'My Checkbox Field\'),true);

/*
* Then just add the fields to the repeater block
*/
//repeater block
$my_meta->addRepeaterBlock($prefix.\'text_field_id\',array(\'inline\' => true, \'name\' => \'This is a Repeater Block\',\'fields\' => $repeater_fields));
/*
* Don\'t Forget to Close up the meta box decleration
*/
//Finish Meta Box Decleration
$my_meta->Finish();
}
看看class-usage-demo.php 该文件也可以作为WordPress插件进行测试。在“我的元框”类中可以看到每个字段的其他选项。php文件。

你也可以在我最新的插件中看到它的使用ShortCodes UI

SO网友:Tom J Nowell

我不太确定你在问什么,但你能在这里找到的最基本的例子,以及其他所有需要了解的东西add_meta_box, 完成保存代码和安全检查:

http://codex.wordpress.org/Function_Reference/add_meta_box

<?php
/* Define the custom box */
add_action( \'add_meta_boxes\', \'myplugin_add_custom_box\' );


/* Do something with the data entered */
add_action( \'save_post\', \'myplugin_save_postdata\' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
    add_meta_box( 
        \'myplugin_sectionid\',
        __( \'My Post Section Title\', \'myplugin_textdomain\' ),
        \'myplugin_inner_custom_box\',
        \'post\' 
    );
}

/* Prints the box content */
function myplugin_inner_custom_box( $post ) {

  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename\' );

  // The actual fields for data entry
  echo \'<label for="myplugin_new_field">\';
       _e("Description for this field", \'myplugin_textdomain\' );
  echo \'</label> \';
  echo \'<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="whatever" size="25" />\';
}

/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
      return;

  // 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[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) )
      return;


  // Check permissions
  if ( \'page\' == $_POST[\'post_type\'] ) 
  {
    if ( !current_user_can( \'edit_page\', $post_id ) )
        return;
  }
  else
  {
    if ( !current_user_can( \'edit_post\', $post_id ) )
        return;
  }

  // OK, we\'re authenticated: we need to find and save the data

  $mydata = $_POST[\'myplugin_new_field\'];

  // Do something with $mydata 
  // probably using add_post_meta(), update_post_meta(), or 
  // a custom table (see Further Reading section below)
}
?>

结束

相关推荐

使用WPAlChemy MetaBox单选框创建If/Else语句

我正在使用WPAlchemy MetaBox类在我的站点上创建其他MetaBox。其中一个有一个带有一组广播箱的部分。我将其编码如下: <?php $mb->the_field(\'docposs\'); ?> <input type=\"radio\" name=\"<?php $mb->the_name(); ?>\" value=\"yes\"<?php $mb->the_radio_state(\'yes\')?\' ch