Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建wordpress插件,向写入/编辑页面添加额外的输入字段_Wordpress_Plugins_Input - Fatal编程技术网

如何创建wordpress插件,向写入/编辑页面添加额外的输入字段

如何创建wordpress插件,向写入/编辑页面添加额外的输入字段,wordpress,plugins,input,Wordpress,Plugins,Input,我正在编写一个插件,当他们创建一篇文章时,需要从使用中获得额外的信息。我需要一个文本框,他们将输入一个数字,可以有小数和下拉框,将提供一些选项。我还需要将此数据与其他post数据一起保存在wordpress数据库中。谁能帮我一下吗 另外,我以后需要在帖子本身添加一个区域,当它显示一段从插件计算出来的数据时,但首先要做的是 编辑: 我已经完成了第一部分,但是现在我无法使用新字段来保存文章中的数据,这是我的代码 <?php /* Plugin Name: Column Height Calcu

我正在编写一个插件,当他们创建一篇文章时,需要从使用中获得额外的信息。我需要一个文本框,他们将输入一个数字,可以有小数和下拉框,将提供一些选项。我还需要将此数据与其他post数据一起保存在wordpress数据库中。谁能帮我一下吗

另外,我以后需要在帖子本身添加一个区域,当它显示一段从插件计算出来的数据时,但首先要做的是

编辑: 我已经完成了第一部分,但是现在我无法使用新字段来保存文章中的数据,这是我的代码

<?php
/*
Plugin Name: Column Height Calculator
Plugin URI: #
Description: calculates the height of the column
Version: 0.1
Author: Ben Crawford
Author URI:
*/

add_action('admin_menu', 'my_post_options_box');

function my_post_options_box() {
add_meta_box('post_info', 'Column Height Info', 'custom_post_info', 'post', 'side', 'high');
}

//Adds the actual option box
function custom_post_info() {
global $post;
?>
<fieldset id="mycustom-div">
<div>
<p>
<label for="column_type" >Column Type:</label>
<br />
<select name="column_type" id="column_type">
  <option value="JBC">Justified Body Copy</option>
  <option value="LRC">Left Raggid Copy</option>
</select>
<br />
<br />
<label for="header_size">Header Size:</label>
<br />
<input type="text" name="header_size" id="header_size" value="<?php echo get_post_meta($post->ID, 'header_size', true); ?>">
</p>
</div>
</fieldset>
<?php
}

add_action('save_post', 'custom_add_save');
function custom_add_save($postID){
// called after a post or page is saved
if($parent_id = wp_is_post_revision($postID))
{
$postID = $parent_id;
}

if ($_POST['column_type']) {
update_custom_meta($postID, $_POST['column_type'], 'column_type');
}
if ($_POST['header_size']) {
update_custom_meta($postID, $_POST['header_size'], 'header_size');
}
}

function update_custom_meta($postID, $newvalue, $field_name) {
// To create new meta
if(!get_post_meta($postID, $field_name)){
add_post_meta($postID, $field_name, $newvalue);
}else{
// or to update existing meta
update_post_meta($postID, $field_name, $newvalue);
}
}
?>


列类型:

对正正文副本 左碎布拷贝

标题大小:

您可以使用wordpress函数编写自己的元框。这将给你最大的控制权


但是,如果您只是使用这个元盒来保存文章中的数据,那么您可以将其合并到插件中。这将使添加一些保存post元数据的数据字段变得非常容易。然后,您可以在循环中获取此元数据。

您可以使用wordpress函数编写自己的元框。这将给你最大的控制权


但是,如果您只是使用这个元盒来保存文章中的数据,那么您可以将其合并到插件中。这将使添加一些保存post元数据的数据字段变得非常容易。然后,您可以使用在循环中获取此元数据。

我发现,数据保存在Posteta中,而不是post中。现在一切都正常了,这是我现在需要添加其他功能的简单部分。

我发现,数据保存在Posteta中,而不是post中。现在一切都正常了,这是我现在需要添加其他功能的简单部分。

-这是新的插件版本。查看一个罐箱,如示例所示:

/**
 * Initialize the metabox class.
 */
function cmb_initialize_cmb_meta_boxes()
{
    if (!class_exists('cmb_Meta_Box')) {
        require_once(plugin_dir_path(__FILE__) . '../CMB2/init.php');
    }
}
add_action('init', 'cmb_initialize_cmb_meta_boxes', 9999);
add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );
/**
 * Define the metabox and field configurations.
 */
function cmb2_sample_metaboxes() {

    // Start with an underscore to hide fields from custom fields list
    $prefix = '_yourprefix_';

    /**
     * Initiate the metabox
     */
    $cmb = new_cmb2_box( array(
        'id'            => 'test_metabox',
        'title'         => __( 'Test Metabox', 'cmb2' ),
        'object_types'  => array( 'page', 'post'), // Post types
        'context'       => 'normal',
        'priority'      => 'high',
        'show_names'    => true, // Show field names on the left
    ) );

    // Regular text field
    $cmb->add_field( array(
        'name'       => __( 'Test Text', 'cmb2' ),
        'desc'       => __( 'field description (optional)', 'cmb2' ),
        'id'         => $prefix . 'text',
        'type'       => 'text',
        'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value

    ) );
}
-这是新的插件版本。查看一个罐箱,如示例所示:

/**
 * Initialize the metabox class.
 */
function cmb_initialize_cmb_meta_boxes()
{
    if (!class_exists('cmb_Meta_Box')) {
        require_once(plugin_dir_path(__FILE__) . '../CMB2/init.php');
    }
}
add_action('init', 'cmb_initialize_cmb_meta_boxes', 9999);
add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );
/**
 * Define the metabox and field configurations.
 */
function cmb2_sample_metaboxes() {

    // Start with an underscore to hide fields from custom fields list
    $prefix = '_yourprefix_';

    /**
     * Initiate the metabox
     */
    $cmb = new_cmb2_box( array(
        'id'            => 'test_metabox',
        'title'         => __( 'Test Metabox', 'cmb2' ),
        'object_types'  => array( 'page', 'post'), // Post types
        'context'       => 'normal',
        'priority'      => 'high',
        'show_names'    => true, // Show field names on the left
    ) );

    // Regular text field
    $cmb->add_field( array(
        'name'       => __( 'Test Text', 'cmb2' ),
        'desc'       => __( 'field description (optional)', 'cmb2' ),
        'id'         => $prefix . 'text',
        'type'       => 'text',
        'show_on_cb' => 'cmb2_hide_if_no_cats', // function should return a bool value

    ) );
}

我现在知道了添加元数据框的部分,我只需要将他们输入的数据与帖子一起保存,以便我以后可以从循环中访问它。对于您的项目,我强烈建议在我的回答中使用Github上的项目I链接,因为它正是您想要做的。但是,要自己构建它,您需要使用save_post钩子。有一些代码可能会有所帮助。我想我为此添加了一个钩子,我在上面添加了我的代码。它仍然不会保存。我现在找到了添加元数据框部分,我只需要将他们输入的数据与帖子一起保存,以便我以后可以从循环中访问它。对于您的项目,我强烈建议在我的答案中使用Github I link上的项目,因为它正是您想要做的。但是,要自己构建它,您需要使用save_post钩子。有一些代码可能会有所帮助。我想我为此添加了一个钩子,我在上面添加了我的代码。它仍然无法保存。正确地缩进代码可以使其更易于阅读和调试。您可以在中找到许多工作代码的示例,请检查。正确缩进代码可以使其更易于阅读和调试。您可以在中找到许多工作代码的示例,请检查。