【问题标题】:Wordpress creating custom fields in custom plugins codeWordpress 在自定义插件代码中创建自定义字段
【发布时间】:2012-12-13 11:13:23
【问题描述】:

根据要求,我需要在 wordpress 3.0 中创建带有自定义字段的插件。我看看在 wordpress 中创建插件。我可以通过硬编码的 HTML 字段代码创建带有自定义字段的插件。比如提供输入类型名称id等。

但我需要使用 wordpress 自定义字段功能创建文本框、图像上传、按钮等字段。只需调用具有字段类型的函数即可生成字段。就像我在 drupal 7 中所做的那样。

下面是drupal 7中创建文本字段的示例代码

$form['posts']['Title'] = array(
    '#prefix' => '<div class="container-inline">',
    '#required' => '1',
    '#size' => '20',
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#suffix' => '</div>',
);

在 wordpress 中可以吗?请在 wordpress 中指导我创建自定义插件。提前致谢...

【问题讨论】:

    标签: wordpress drupal plugins custom-fields


    【解决方案1】:

    这是我用来在自定义帖子类型中添加自定义字段的代码示例。 或者您可以使用插件Advanced Custom Field 添加自定义字段并将它们附加到您的自定义帖子类型。

    希望对你有帮助!

    <?php
    // Metabox declaration
    $prefix = 'bookmark_';  
    
    // The only way I found too pass the fields informations to the action
    global $bookmark_meta_fields;
    $bookmark_meta_fields = array(  
        array(  
            'label'=> 'Url',  
            'desc'  => 'Url of the bookmark.',  
            'id'    => $prefix.'url',  
            'type'  => 'text'  
        ),  
        array(  
            'label'=> 'Comments',  
            'desc'  => 'A small comments about the bookmarks.',  
            'id'    => $prefix.'comment',  
            'type'  => 'textarea'  
        ),  
    
    ); 
    
    
    
    add_action('add_meta_boxes', 'vban_bookmark_metabox');
    
    function vban_bookmark_metabox() {  
        add_meta_box(  
            'bookmark_info', // $id  
            'Bookmark info', // $title  
            'vban_bookmark_metabox_show', // $callback  
            'vbanBookmarks', // $page  
            'normal', // $context  
            'high'); // $priority  
    }  
    
    /*
    * show metabox function
    */
    function vban_bookmark_metabox_show() {  
        global $bookmark_meta_fields, $post;  
        // Use nonce for verification  
        echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  
        // Begin the field table and loop  
        echo '<table class="form-table">';  
        foreach ($bookmark_meta_fields as $field) {  
            // get value of this field if it exists for this post  
            $meta = get_post_meta($post->ID, $field['id'], true);  
            // begin a table row with  
            echo '<tr> 
                    <th><label for="'.$field['id'].'">'.$field['label'].'</label></th> 
                    <td>';  
                    switch($field['type']) {  
                       case 'text':  
                            echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
                            <br /><span class="description">'.$field['desc'].'</span>';
                            // textarea  
                        break; 
                        case 'textarea':  
                            echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea> 
                                <br /><span class="description">'.$field['desc'].'</span>';  
                        break;    
    
                    } //end switch  
            echo '</td></tr>';  
        } // end foreach  
        echo '</table>'; // end table  
    } 
    
    
    
    
    /*
    * SAVE metabox custom_field
    */
    
    add_action('save_post', 'vban_bookmark_metabox_save');
    
    // Save the Data  
    function vban_bookmark_metabox_save($post_id) {  
    
        global $bookmark_meta_fields;  
        // verify nonce  
        if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__))) {        
            return $post_id;  
        } 
    
        // check autosave  
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
             return $post_id;
        }
    
        // check permissions  
        if ('vbanBookmarks' == $_POST['post_type']) {  
            if (!current_user_can('edit_bookmark', $post_id))
                return $post_id;  
            } elseif (!current_user_can('edit_post', $post_id)) {
                return $post_id;  
            }
        // loop through fields and save the data  
        foreach ($bookmark_meta_fields as $field) {  
    
            $old = get_post_meta($post_id, $field['id'], true);  
            $new = $_POST[$field['id']];  
            if ($new && $new != $old) {  
                update_post_meta($post_id, $field['id'], $new);  
            } elseif ('' == $new && $old) {  
                delete_post_meta($post_id, $field['id'], $old);  
            }  
        } // end foreach  
    }  
    ?>
    

    【讨论】:

      【解决方案2】:

      您还可以看看这个插件:高级自定义帖子类型。

      “这个框架不仅可以在 WordPress 中创建自定义帖子类型、角色和分类,还可以让您快速创建自定义字段(仅限帖子类型)。”

      https://github.com/kevindees/advanced_custom_post_types

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多