重力形式+自定义PostType不保存复选框或下拉列表

时间:2019-05-20 16:38:35

标签: custom-post-type gravity-forms-plugin

我有一个成员“自定义帖子”类型。我使用Gravity表单和+ Custom Fields插件来创建CPT的未决条目,其中填充有来自gravity Form的字段。除复选框和1下拉菜单未保存到CPT之外,此功能均有效。创建成员后,我可以手动保存该值,但是该值并不来自重力形式提交。

我想知道是否有一种方法可以使这些字段正常工作?我看到一个使用gform_after_submission修复具有相同问题的ACF复选框的示例,但是,我是GF的新手,我的元框不是来自ACF。

在我的情况下,提供最佳修复方式和语法的任何帮助都将不胜感激。生成我的CPT元框的代码如下。

/**
 * Generated by the WordPress Meta Box Generator at 
 */
class Rational_Meta_Box {
    private $screens = array(
        'Member',
    );
    private $fields = array(
        array(
            'id' => 'description',
            'label' => 'Description',
            'type' => 'textarea',
        ),
        array(
            'id' => 'fname',
            'label' => 'First Name',
            'type' => 'text',
        ),
        array(
            'id' => 'lname',
            'label' => 'Last Name',
            'type' => 'text',
        ),

        array(
            'id' => 'contact',
            'label' => 'Contact',
            'type' => 'text',
        ),
        array(
            'id' => 'address',
            'label' => 'Street Address',
            'type' => 'text',
        ),
        array(
            'id' => 'po-box',
            'label' => 'PO Box',
            'type' => 'text',
        ),
        array(
            'id' => 'city',
            'label' => 'City',
            'type' => 'text',
        ),
        array(
            'id' => 'state',
            'label' => 'State',
            'type' => 'text',
        ),
        array(
            'id' => 'zip',
            'label' => 'ZIP',
            'type' => 'text',
        ),
        array(
            'id' => 'country',
            'label' => 'Country',
            'type' => 'text',
        ),
        array(
            'id' => 'map_check',
            'label' => 'Do NOT show on Map',
            'type' => 'checkbox',
        ),
        array(
            'id' => 'phone',
            'label' => 'Phone',
            'type' => 'tel',
        ),
        array(
            'id' => 'email',
            'label' => 'Email',
            'type' => 'email',
        ),

            array(
            'id' => 'check_email',
            'label' => 'Do NOT show email',
            'type' => 'checkbox',
        ),
        array(
            'id' => 'website',
            'label' => 'Website',
            'type' => 'url',
        ),
        array(
            'id' => 'facebook',
            'label' => 'Facebook',
            'type' => 'url',
        ),
        array(
            'id' => 'annual-chamber-dues',
            'label' => 'Annual Chamber Dues',
            'type' => 'select',
            'options' => array(
                '55' => '1-4 employees - $55',
                '90' => '5-9 employees - $90',
                '110' => '10-15 employees - $110',
                '160' => '16+ employees - $160',
                '25' => 'Associate Membership (non-business) - $25',
                '50' => 'Non-profit Groups - $50',
            ),
        ),
        array(
            'id' => 'comments',
            'label' => 'Comments',
            'type' => 'textarea',
        ),
         array(
            'id' => 'featured',
            'label' => 'Featured Member',
            'type' => 'checkbox',
        ), 
    );

    /**
     * Class construct method. Adds actions to their respective WordPress hooks.
     */
    public function __construct() {
        add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
        add_action( 'save_post', array( $this, 'save_post' ) );
    }

    /**
     * Hooks into WordPress' add_meta_boxes function.
     * Goes through screens (post types) and adds the meta box.
     */
    public function add_meta_boxes() {
        foreach ( $this->screens as $screen ) {
            add_meta_box(
                'elc-member',
                __( 'ElC-Member', 'quark' ),
                array( $this, 'add_meta_box_callback' ),
                $screen,
                'advanced',
                'default'
            );
        }
    }

    /**
     * Generates the HTML for the meta box
     * 
     * @param object $post WordPress post object
     */
    public function add_meta_box_callback( $post ) {
        wp_nonce_field( 'elc_member_data', 'elc_member_nonce' );
        echo 'Data for the members';
        $this->generate_fields( $post );
    }

    /**
     * Generates the field's HTML for the meta box.
     */
    public function generate_fields( $post ) {
        $output = '';
        foreach ( $this->fields as $field ) {
            $label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
            $db_value = get_post_meta( $post->ID, 'elc_member_' . $field['id'], true );
            switch ( $field['type'] ) {
                case 'checkbox':
                    $input = sprintf(
                        '<input %s id="%s" name="%s" type="checkbox" value="1">',
                        $db_value === '1' ? 'checked' : '',
                        $field['id'],
                        $field['id']
                    );
                    break;
                case 'select':
                    $input = sprintf(
                        '<select id="%s" name="%s">',
                        $field['id'],
                        $field['id']
                    );
                    foreach ( $field['options'] as $key => $value ) {
                        $field_value = !is_numeric( $key ) ? $key : $value;
                        $input .= sprintf(
                            '<option %s value="%s">%s</option>',
                            $db_value === $field_value ? 'selected' : '',
                            $field_value,
                            $value
                        );
                    }
                    $input .= '</select>';
                    break;
                case 'textarea':
                    $input = sprintf(
                        '<textarea class="large-text" id="%s" name="%s" rows="6">%s</textarea>',
                        $field['id'],
                        $field['id'],
                        $db_value
                    );
                    break;
                default:
                    $input = sprintf(
                        '<input %s id="%s" name="%s" type="%s" value="%s">',
                        $field['type'] !== 'color' ? 'class="regular-text"' : '',
                        $field['id'],
                        $field['id'],
                        $field['type'],
                        $db_value
                    );
            }
            $output .= $this->row_format( $label, $input );
        }
        echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
    }

    /**
     * Generates the HTML for table rows.
     */
    public function row_format( $label, $input ) {
        return sprintf(
            '<tr><th scope="row">%s</th><td>%s</td></tr>',
            $label,
            $input
        );
    }
    /**
     * Hooks into WordPress' save_post function
     */
    public function save_post( $post_id ) {
        if ( ! isset( $_POST['elc_member_nonce'] ) )
            return $post_id;

        $nonce = $_POST['elc_member_nonce'];
        if ( !wp_verify_nonce( $nonce, 'elc_member_data' ) )
            return $post_id;

        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return $post_id;

        foreach ( $this->fields as $field ) {
            if ( isset( $_POST[ $field['id'] ] ) ) {
                switch ( $field['type'] ) {
                    case 'email':
                        $_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
                        break;
                    case 'text':
                        $_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
                        break;
                }
                update_post_meta( $post_id, 'elc_member_' . $field['id'], $_POST[ $field['id'] ] );
            } else if ( $field['type'] === 'checkbox' ) {
                update_post_meta( $post_id, 'elc_member_' . $field['id'], '0' );
            }
        }
    }
}
new Rational_Meta_Box;

0 个答案:

没有答案