How to edit a custom post type on front end form using CMB2?
-
I have created a form on the front end for a custom post type using CMB2 (https://github.com/WebDevStudios/CMB2). I am now trying to edit the custom post types using the same form.
I can display the post variables on the page no problem but I am having issues getting the variables to display in the relevant input field so that they can be edited. However, files that I uploaded for that post using file_list are displayed.
I cannot find any information about editing and would appreciate any help.
I have tried using the ID of the inputs, so if the ID of a field is ‘_cmb_title’, I have tried $cmb_title = $post_values->post_title;
Full code is below:
<?php class editForm { // Set prefix public $prefix = '_cmb_'; /** * Construct the class. */ public function __construct() { add_filter( 'cmb_meta_boxes', array( $this, 'cmb_metaboxes' ) ); add_shortcode( 'cmb-edit-form', array( $this, 'do_frontend_form' ) ); add_action( 'init', array( $this, 'initialize_cmb_meta_boxes' ), 9 ); add_action( 'save_post', array( $this, 'save_fields' ), 10, 4 ); /*add_action( 'cmb_save_post_fields', array( $this, 'save_fields' ), 10, 4 ); */ /*add_filter( 'bp_blogs_record_comment_post_types', 'add_activity' ); */ } /** * Define the metabox and field configurations. */ public function cmb_metaboxes( array $meta_boxes ) { /** * Metabox for the "Memorials" front-end submission form */ $meta_boxes['edit_files_metabox'] = array( 'id' => 'edit-files', 'title' => __( 'Upload files / Embed media / Add links', 'cmb' ), 'pages' => array( 'Files' ), // Post type 'context' => 'normal', 'priority' => 'high', 'show_names' => true, // Show field names on the left 'fields' => array( array( 'name' => __( 'Title', 'cmb' ), 'desc' => __( 'Add title for files, links and/or media', 'cmb' ), 'id' => $this->prefix . 'title', 'type' => 'text', ), array( 'name' => __( 'Description', 'cmb' ), 'desc' => __( 'Add a description of files, links and/or media', 'cmb' ), 'id' => $this->prefix . 'description', 'type' => 'textarea', ), array( 'name' => __( 'Project stage', 'cmb' ), 'desc' => __( 'Assign to stage of project', 'cmb' ), 'id' => $this->prefix . 'category', 'show_option_none' => true, 'type' => 'taxonomy_select', 'taxonomy' => 'stages', // Taxonomy Slug ), array( 'name' => __( 'Post date', 'cmb' ), 'desc' => __( 'Date files posted.', 'cmb' ), 'id' => $this->prefix . 'post_date', 'type' => 'text_date' ), array( 'name' => __( 'Link to website', 'cmb' ), 'desc' => __( 'Add url to website. eg. https://www.google.co.uk', 'cmb' ), 'id' => $this->prefix . 'url', 'type' => 'text_url', 'protocols' => array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet'), // Array of allowed protocols // 'repeatable' => true, ), array( 'name' => __( 'Upload files', 'cmb' ), 'desc' => __( 'Upload file(s)', 'cmb' ), 'id' => $this->prefix . 'file_list', 'type' => 'file_list', ), array( 'name' => __( 'oEmbed', 'cmb' ), 'desc' => __( 'Enter a youtube, twitter, or instagram URL to embed. Supported services are listed at https://codex.www.ads-software.com/Embeds.', 'cmb' ), 'id' => $this->prefix . 'embed', 'type' => 'oembed', ), ), ); return $meta_boxes; } /** * Shortcode to display a CMB form for a post ID. */ public function do_frontend_form() { // Default metabox ID $metabox_id = 'edit_files_metabox'; // Get all metaboxes $meta_boxes = apply_filters( 'cmb_meta_boxes', array() ); // If the metabox specified doesn't exist, yell about it. if ( ! isset( $meta_boxes[ $metabox_id ] ) ) { return __( "A metabox with the specified 'metabox_id' doesn't exist.", 'cmb' ); } // This is the WordPress post ID where the data should be stored/displayed. if(($_GET['action']=='edit')&&(isset($_GET['post']))){ $post_id = $_GET['post']; //get post values to populate form $post_values = get_post( $post_id ); $cmb_title = $post_values->post_title; echo $cmb_title; $_cmb_title = $cmb_title; $cmb_description = $post_values->post_content; echo $cmb_description; $cmb_date = $post_values->post_date; echo $cmb_date; //$_cmb_title = $post_values['post_title']; //get files / links to display } if ( $edit_id = $this->intercept_post_id() ) { $post_id = $edit_id; echo '<div id="message">'; echo '<p>Your file entry has been edited.</p>'; echo '</div>'; } // Shortcodes need to return their data, not echo it. $echo = false; // Get our form $form = cmb_metabox_form( $meta_boxes[ $metabox_id ], $post_id, $echo ); return $form; } /** * Get data before saving to CMB. */ public function intercept_post_id() { // Check for $_POST data if ( empty( $_POST ) ) { return false; } // Check nonce if ( ! ( isset( $_POST['submit-cmb'], $_POST['wp_meta_box_nonce'] ) && wp_verify_nonce( $_POST['wp_meta_box_nonce'], cmb_Meta_Box::nonce() ) ) ) { return; } // Setup and sanitize data if ( isset( $_POST[ $this->prefix . 'title' ] ) && ($_POST[ $this->prefix . 'title'] != '') ) { //add post data to database $this->new_submission = wp_insert_post( array( 'post_title' => sanitize_text_field( $_POST[ $this->prefix . 'title' ] ), 'post_content' => sanitize_text_field( $_POST[ $this->prefix . 'description' ] ), 'post_author' => get_current_user_id(), 'post_status' => 'publish', // Set to draft so we can review first 'post_type' => 'file', /* 'post_category' => array($_POST[ $this->prefix . 'category' ]), none of these work, see wp_set_object_terms below */ /*'tax_input' => array($_POST[ $this->prefix . 'category' ])*/ /* 'tax_input' => array( 'stages'=> $_POST[ $this->prefix . 'category' ] ) */ ), true ); // If no errors, save the data into a new post draft if ( ! is_wp_error( $this->new_submission ) ) { wp_set_object_terms( $this->new_submission, $_POST[ $this->prefix . 'category' ], 'stages' ); //add select custom taxonomy (project stage) return $this->new_submission; } } else { $error = new WP_Error( 'post_data_missing', __( 'File post requires a title.' ) ); if( is_wp_error( $error ) ) { echo '<div id="message">'; echo '<p>' . $error->get_error_message() . '</p>'; echo '</div>'; } } } /** * Grant temporary permissions to subscribers. */ public function grant_publish_caps( $caps, $cap, $args ) { if ( 'edit_post' == $args[0] ) { $caps[$cap[0]] = true; } return $caps; } /** * Save custom fields and uploaded files */ public function save_fields( $post_id, $post ) { /*if($_POST['_cmb_category'] != ''){ //shouldn't need this anymore as should save as part of main post ie. wp_insert_post() in code above add_post_meta( $post_id, '_cmb_category', $_POST['_cmb_category'], true ); }*/ if($_POST['_cmb_url'] != ''){ add_post_meta( $post_id, '_cmb_url', $_POST['_cmb_url'], true ); } if($_POST['_cmb_embed'] != ''){ add_post_meta( $post_id, '_cmb_embed', $_POST['_cmb_embed'], true ); } if($_POST['_cmb_file_list'] != ''){ //add file to database add_post_meta( $post_id, '_cmb_file_list', $_POST['_cmb_file_list'], true ); //get file info back from DB $attachments = get_post_meta($post_id, '_cmb_file_list'); $attachments = $attachments[0]; //loop through files to set a featured image $img_ext = array('gif','png' ,'jpg'); $counter = 0; foreach($attachments as $attachment_id => $filename){ if($counter == 0){ $ext = pathinfo($filename, PATHINFO_EXTENSION); if(!in_array($ext,$img_ext) ) { set_post_thumbnail( $post_id, $attachment_id ); $counter ++; } } } } } /** * Initialize CMB. */ public function initialize_cmb_meta_boxes() { if ( ! class_exists( 'cmb_Meta_Box' ) ) { require_once 'init.php'; } } } // end class $editForm = new editForm(); ?>
Thanks
- The topic ‘How to edit a custom post type on front end form using CMB2?’ is closed to new replies.