• Resolved Jamie

    (@rabillion)


    hi,

    is it possible to create a new post from a custom metabox?
    I’m getting the metabox form by the function cmb2_get_metabox_form

    The form is shown on frontpage, but i can only edit a post and not create a new post

    Code below:

    //create shortcode
    class CustomFrontendform {
    
        public static function init() {
            add_shortcode( 'cmb-form', array( __CLASS__, '_do_frontend_form_shortcode' ) );
        }
    
        public static function _do_frontend_form_shortcode( $atts = array() ) {
    
            // If no metabox id is set, yell about it
            if ( empty( $atts['id'] ) ) {
                return 'Please add an "id" attribute to specify the CMB2 form to display.';
            }
    
            $metabox_id = esc_attr( $atts['id'] );
            // Get our form
            $form = cmb2_get_metabox_form( $metabox_id );
    
            return $form;
        }
    }
    
    new CustomFrontendform();
    // custom metabox
    protected function hooks() {
            add_action( 'cmb2_init', array( __CLASS__, '_register_custommetabox_formfields' ) );
        }
    
        /**
         * custommetabox field
         * Hook in and add a metabox to select templates
         */
        public function _register_custommetabox_formfields() {
            new_cmb2_box( array(
                'id'           => Bootstrap::PREFIX . 'custommetabox',
                'title'        => __( 'Kleinanzeige', 'cmb2' ),
                'object_types' => array( 'custom_posts', ), // Post type
                'cmb_styles'   => false,
                'fields'       => array(
                    array(
                        'name' => __( 'Email', 'cmb2' ),
                        'id'   => Bootstrap::PREFIX . 'email',
                        'type' => 'text_email',
                    ),
                    array(
                        'name' => __( 'Phone', 'cmb2' ),
                        'id'   => Bootstrap::PREFIX . 'phone',
                        'type' => 'text',
                    ),
                    array(
                        'name'       => __( 'ZIP', 'theme-domain' ),
                        'id'         => Bootstrap::PREFIX . 'zipcode',
                        'type'       => 'text',
                        'attributes' => array(
                            'type' => 'number',
                        ),
                    ),
                    array(
                        'name'         => __( 'Image', 'cmb2' ),
                        'id'           => Bootstrap::PREFIX . 'image',
                        'type'         => 'file',
                        'preview_size' => array( 100, 100 ),
                    ),
                    array(
                        'name'    => __( 'Text', 'cmb2' ),
                        'id'      => Bootstrap::PREFIX . 'content',
                        'type'    => 'textarea',
                        'options' => array(
                            'textarea_rows' => 12,
                        )
                    ),
                )
            ) );
        }

    https://www.ads-software.com/plugins/cmb2/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Jamie

    (@rabillion)

    UPDATE
    if solved it. I haved used wp_insert_post:

    The only thing i need, is to clear the form by default. Now, the form is filled by the latest post.
    Has someone a soulution for this?

    protected function _save_posts() {
    
            $post_title       = substr( wp_strip_all_tags( $_POST['custom_content'] ), 0, 30 );
            $post_information = array(
                'post_title'  => date( "d.m.Y H:i" ) . ' - ' . $post_title . ' ...',
                'post_type'   => 'custom_posts',
                'post_status' => 'pending',
                'meta_input'  => array(
                    'custom_category' => $_POST['custom_category'],
                    'custom_email'    => $_POST['custom_email'],
                    'custom_phone'    => $_POST['custom_phone'],
                    'custom_zipcode'  => $_POST['custom_zipcode'],
                    'custom_image'    => $_POST['custom_image'],
                    'custom_image_id' => $_POST['custom_image_id'],
                    'custom_content'  => $_POST['custom_content'],
                ),
            );
            wp_insert_post( $post_information, $wp_error = false );
        }
    Plugin Author Justin Sternberg

    (@jtsternberg)

    Thread Starter Jamie

    (@rabillion)

    thanks! It works now

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘save new post by cmb2_get_metabox_form on frontpage’ is closed to new replies.