Support » Plugin: CMB2 » save_fields isn’t working

  • Resolved jeremycaris

    (@jeremycaris)


    I’m following this example: https://github.com/CMB2/CMB2-Snippet-Library/blob/master/front-end/cmb2-front-end-submit.php

    $cmb = cmb2_get_metabox( 'sofw_vendors_options_page', 'fake-oject-id' );
    
    $sanitized_values = $cmb->get_sanitized_values( $_POST );
    
    // result is this array
    $sanitized_values = Array
    (
        [business_name] => Test Business
        [point_of_contact] => John Doe
        [point_of_contact_position] => Owner
        [email] => johnny@aol.com
        [website] => https://testbusiness.com
        [category] => Web Services
        [video] => https://youtu.be/7AoJwMImQZE
        [about_us] => About us.
        [services] => Services.
        [incentive] => Give us a call!
    )
    
    // Set our post data arguments
    $post_data = array();
    $post_data['post_title'] = $sanitized_values['business_name'];
    $post_data['post_content'] = '';
    $post_data['post_status'] = 'publish';
    $post_data['post_type'] = 'vendor';
    $user_id = get_current_user_id();
    $post_data['post_author'] = $user_id ? $user_id : 1;
    
    // This successfully creates new vendor cpt and new_submission_id = post_ID
    $new_submission_id = wp_insert_post( $post_data, true );
    
    // But save_fields isn't working here. No post meta saved...
    $cmb->save_fields( $new_submission_id, 'vendor', $sanitized_values );
    

    What am I missing, or is this a bug?

Viewing 7 replies - 16 through 22 (of 22 total)
  • Thread Starter jeremycaris

    (@jeremycaris)

    Thank you! Here’s all the code together to make it easy.

    require_once __DIR__ . '/inc/cmb2/init.php';
    
    add_action( 'init', 'create_vendor_post_type' );
    
    function create_vendor_post_type () {
        $singular = 'Vendor';
        $plural = 'Vendors';
    
        $slug = sanitize_title($singular);
        $textdomain = get_stylesheet();
        # Create Post Type
        $post_labels = array(
            "name" => __( $plural, $textdomain ),
            "singular_name" => __( $singular, $textdomain ),
        );
        $post_args = array(
            "label" => __( $plural, $textdomain ),
            "labels" => $post_labels,
            "description" => "",
            "public" => true,
            "publicly_queryable" => true,
            "show_ui" => true,
            "delete_with_user" => false,
            "show_in_rest" => true,
            "rest_base" => "",
            "rest_controller_class" => "WP_REST_Posts_Controller",
            "has_archive" => true,
            "show_in_menu" => true,
            "show_in_nav_menus" => true,
            "exclude_from_search" => false,
            "capability_type" => "post",
            "map_meta_cap" => true,
            "hierarchical" => true,
            "rewrite" => array( "slug" => $slug, "with_front" => true ),
            "query_var" => true,
            "menu_position" => 90,
            'menu_icon' => 'dashicons-groups',
            "supports" => array( "title", "editor", "thumbnail", "excerpt", "custom-fields", "revisions", "author", "page-attributes", "post-formats", "comments" ),
        );
        register_post_type( $slug, $post_args );
        # Create Category
        $cat_labels = array(
            "name" => __( "Categories", $textdomain ),
            "singular_name" => __( "Category", $textdomain ),
        );
        $cat_args = array(
            "label" => __( "Categories", $textdomain ),
            "labels" => $cat_labels,
            "public" => true,
            "publicly_queryable" => true,
            "hierarchical" => true,
            "show_ui" => true,
            "show_in_menu" => true,
            "show_in_nav_menus" => true,
            "query_var" => true,
            "rewrite" => array( 'slug' => $slug.'_category', 'with_front' => true, ),
            "show_admin_column" => false,
            "show_in_rest" => true,
            "rest_base" => $slug."_category",
            "rest_controller_class" => "WP_REST_Terms_Controller",
            "show_in_quick_edit" => false,
            );
        register_taxonomy( $slug."_category", array( $slug ), $cat_args );
    }
    
    add_action( 'cmb2_admin_init', 'sofw_vendor_metaboxes' );
    
    function sofw_vendor_metaboxes() {
        $sofw_vendor_post_box = new_cmb2_box( array(
            'id'            => 'sofw_vendor_metabox',
            'title'         => __( 'Create New Vendor', 'sofw-vendors' ),
            'object_types'  => array( 'vendor', ),
            'context'       => 'normal',
            'priority'      => 'high',
            'show_names'    => true,
            'show_in_rest'  => false,
        ) );
    
        $sofw_vendor_post_box->add_field( array(
            'name' => esc_html__( 'Email Address*', 'sofw-vendors' ),
            'id'   => 'email',
            'type' => 'text_email',
            'attributes' => array(
                'required' => 'required',
            ),
        ) );
    }
    
    add_action( 'cmb2_vendor_process_fields_sofw_vendor_metabox', 'my_custom_code', 10, 2 );
    function my_custom_code( $cmb2_obj, $cmb2_obj_id ) {
        my_log( $cmb2_obj );
    }
    
    function my_log($log_msg) {
        $log_time = date('Y-m-d h:i:sa');
        $log_filename = get_stylesheet_directory()."/log";
        if (!file_exists($log_filename)) 
        {
            // create directory/folder uploads.
            mkdir($log_filename, 0777, true);
        }
        $log_file_data = $log_filename.'/log_' . date('d-M-Y') . '.log';
        $log_file_data = $log_filename.'/log_debug' . '.log';
        file_put_contents($log_file_data, date('d-M-Y G:i:s') . "\n\n" . print_r($log_msg, true) . "\n\n", FILE_APPEND);
    }
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Looks like:

    1) I failed to more closely read the comments
    2) We have a slightly confusing spot, overall.

    * The dynamic portion of the hook name, $object_type, refers to the
    * metabox/form’s object type
    * Usually post (this applies to all post-types).
    * Could also be comment, user or options-page.

    Change

    add_action( 'cmb2_vendor_process_fields_sofw_vendor_metabox', 'my_custom_code', 10, 2 );
    

    to

    add_action( 'cmb2_post_process_fields_sofw_vendor_metabox', 'my_custom_code', 10, 2 );
    

    and it caught just fine.

    I believe this should be fine if you’re just using CMB2 for this one post type, but if you’re going to expand, then perhaps add some checks for post type via https://developer.wordpress.org/reference/functions/get_post_type/ to return early with.

    Otherwise I think this finishes off that issue overall.

    Thread Starter jeremycaris

    (@jeremycaris)

    Ahhhhh. Thanks again!

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome

    Thread Starter jeremycaris

    (@jeremycaris)

    By the way… I had specified ‘vendor’ (my custom post type slug) as ‘object_types’ because the cmb2 wiki says ‘object_types’ is “An array containing post type slugs…”

    It would be awesome if a future release included support for custom post type slugs.

    • This reply was modified 7 months, 2 weeks ago by jeremycaris.
    • This reply was modified 7 months, 2 weeks ago by jeremycaris.
    • This reply was modified 7 months, 2 weeks ago by jeremycaris.
    • This reply was modified 7 months, 2 weeks ago by jeremycaris.
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    The object_types field did what it says, technically, because the metaboxes were being added to the correct post type. The nested hook, however, was defaulting to just the “post” post type and didn’t vary based on post type. Plus, as we saw, it still worked, just needed potentially some extra checks with other information available.

    That said, I agree the hook should handle variation on its own.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not sure if we’re going to be able to super easily change this to be variable by post type, and probably best to leave as just “post”.

Viewing 7 replies - 16 through 22 (of 22 total)
  • The topic ‘save_fields isn’t working’ is closed to new replies.