Forum Replies Created

Viewing 15 replies - 1 through 15 (of 39 total)
  • 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 3 years, 9 months ago by jeremycaris.
    • This reply was modified 3 years, 9 months ago by jeremycaris.
    • This reply was modified 3 years, 9 months ago by jeremycaris.
    • This reply was modified 3 years, 9 months ago by jeremycaris.
    Thread Starter jeremycaris

    (@jeremycaris)

    Ahhhhh. Thanks again!

    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);
    }
    Thread Starter jeremycaris

    (@jeremycaris)

    I’m adding it in functions.php just to test. So I’ve been playing around. This works when I publish or update the post:

    add_action( 'cmb2_override_meta_save', 'my_custom_code', 10, 4 );
    function my_custom_code( $null,  $a,  $this_args,  $instance ) {
        my_log( $instance );
    }

    But this doesn’t fire off:

    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 );
    }

    And this doesn’t fire either:

    add_action( 'cmb2_save_vendor_fields_sofw_vendor_metabox', 'my_custom_code', 10, 3 );
    function my_custom_code( $object_id,  $this_updated,  $instance ) {
        my_log( $instance );
    }

    My object type is definitely ‘vendor’ and my metabox id is definitely ‘sofw_vendor_metabox’. I’m stumped.

    Thread Starter jeremycaris

    (@jeremycaris)

    In fact, I just revised to make sure the hook is firing off:

    add_action( 'cmb2_vendor_process_fields_sofw_vendor_metabox', 'my_custom_code', 10, 2 );
    function my_custom_code( $cmb2_obj, $cmb2_obj_id ) {
        // here I updated my funtion to log 'test' to the file (instead of $cmb2_obj), and nothing is logged... which tells my the hook is never triggered.
    }

    What am I missing?

    Thread Starter jeremycaris

    (@jeremycaris)

    Actually, I have one more question… So that worked for my option page, now I’m wondering if I can accomplish this just by publishing a new custom Vendor post type with the fields filled out using the same hook, but it’s not passing any data. Here’s my meta box:

    $sofw_vendor_post_box = new_cmb2_box( array(
        'id'            => 'sofw_vendor_metabox',
        'title'         => __( 'Create New Vendor', 'sofw-vendors' ),
        'object_types'  => array( 'vendor', ), // Post type
        'context'       => 'normal',
        'priority'      => 'high',
        'show_names'    => true, // Show field names on the left
    ) );

    And here’s the hook I’m using:

    add_action( 'cmb2_vendor_process_fields_sofw_vendor_metabox', 'my_custom_code', 10, 2 );
    
    function my_custom_code( $cmb2_obj, $cmb2_obj_id ) {
        $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_debug' . '.log';
        file_put_contents($log_file_data, date('d-M-Y G:i:s') . "\n\n" . print_r($cmb2_obj, true) . "\n\n", FILE_APPEND);
    }

    I’m just logging the $cmb2_obj. This works with the code I last posted for the options page, but I don’t get anything at all when I publish or update a new vendor post with the metabox fields filled out.

    Any thoughts?

    Thread Starter jeremycaris

    (@jeremycaris)

    This hook worked:

    add_action( 'cmb2_options-page_process_fields_sofw_vendors_options_page', 'my_custom_code', 10, 2 );
    

    Which allowed me to access the email field like this:

    $email = $cmb->data_to_save[email];

    From there, I can do everything I need to do. Thank again!

    Thread Starter jeremycaris

    (@jeremycaris)

    Thank you! I’m going to play around with this later today. I appreciate your time and help!

    Thread Starter jeremycaris

    (@jeremycaris)

    I need to follow up with several other custom functions. For instance, on post creation, I’m creating a new Gravity Form with the submitted email as the notification recipient, and assigning that gform id to the vendor post, so the custom page template pulls it automatically. I’m successfully doing all of this with Advanced Custom Fields, but I was trying to move toward not being dependent on ACF so that I could roll it all up into one plugin. If I just create a new vendor post type without having the custom meta defined, I don’t have all the data I need to create the form.

    Thread Starter jeremycaris

    (@jeremycaris)

    I can just use the standard update_post_meta function, but I’m trying to learn CMB2.

    Thread Starter jeremycaris

    (@jeremycaris)

    Actually, I’m trying to create a new post (custom post type “vendor”, which is working) and then save the submitted form fields as custom post meta for the post that was created.

    Thread Starter jeremycaris

    (@jeremycaris)

    Here’s the entire class just in case it helps:

    if ( !class_exists('SOFW_Vendors_Options_Page') ) {
    
        class SOFW_Vendors_Options_Page {
            
            function __construct() {
                add_action( 'cmb2_admin_init', array($this, 'sofw_vendors_options_page_setup') );
                add_action( 'cmb2_after_init', array($this, 'sofw_vendors_save_override' ) );
            }
    
            /**
             * Hook in and register a metabox to handle a theme options page and adds a menu item.
             */
            public function sofw_vendors_options_page_setup() {
    
                /**
                 * Registers main options page menu item and form.
                 */
                $main_options = new_cmb2_box( array(
                    'id'           => 'sofw_vendors_options_page',
                    'title'        => esc_html__( 'Create New Vendor', 'sofw-vendors' ),
                    'desc'       => esc_html__( 'Creates a New Vendor, generates a new Gravity Form, and assigns it to the Vendor.', 'sofw-vendors' ),
                    'object_types' => array( 'options-page' ),
    
                    /*
                     * The following parameters are specific to the options-page box
                     * Several of these parameters are passed along to add_menu_page()/add_submenu_page().
                     */
    
                    'option_key'      => 'sofw_vendors_options', // The option key and admin menu page slug.
                    'icon_url'        => 'dashicons-groups', // Menu icon. Only applicable if 'parent_slug' is left empty.
                    // 'menu_title'      => esc_html__( 'Options', 'cmb2' ), // Falls back to 'title' (above).
                    // 'parent_slug'     => 'themes.php', // Make options page a submenu item of the themes menu.
                    // 'capability'      => 'manage_options', // Cap required to view options-page.
                    'position'        => 91, // Menu position. Only applicable if 'parent_slug' is left empty.
                    // 'admin_menu_hook' => 'network_admin_menu', // 'network_admin_menu' to add network-level options page.
                    // 'display_cb'      => false, // Override the options-page form output (CMB2_Hookup::options_page_output()).
                    'save_button'     => esc_html__( 'Add Vendor', 'sofw-vendors' ), // The text for the options-page save button. Defaults to 'Save'.
                    // 'disable_settings_errors' => true, // On settings pages (not options-general.php sub-pages), allows disabling.
                    // 'message_cb'      => 'yourprefix_options_page_message_callback',
                ) );
    
                /**
                 * Options fields ids only need
                 * to be unique within this box.
                 * Prefix is not needed.
                 */
                $main_options->add_field( array(
                    'name'       => esc_html__( 'Business Name*', 'sofw-vendors' ),
                    'id'         => 'business_name',
                    'type'       => 'text',
                    'attributes' => array(
                        'data-validation' => 'required',
                    ),
                ) );
                
                $main_options->add_field( array(
                    'name'       => esc_html__( 'Point of Contact*', 'sofw-vendors' ),
                    'id'         => 'point_of_contact',
                    'type'       => 'text',
                    'attributes' => array(
                        'data-validation' => 'required',
                    ),
                ) );
                
                $main_options->add_field( array(
                    'name'       => esc_html__( 'Point of Contact\'s Position*', 'sofw-vendors' ),
                    'id'         => 'point_of_contact_position',
                    'type'       => 'text',
                    'attributes' => array(
                        'data-validation' => 'required',
                    ),
                ) );
    
                $main_options->add_field( array(
                    'name' => esc_html__( 'Email Address*', 'sofw-vendors' ),
                    'id'   => 'email',
                    'type' => 'text_email',
                    'attributes' => array(
                        'data-validation' => 'required',
                    ),
                ) );
    
                $main_options->add_field( array(
                    'name' => esc_html__( 'Website', 'sofw-vendors' ),
                    'id'   => 'website',
                    'type' => 'text_url',
                ) );
    
                $main_options->add_field( array(
                    'name'             => esc_html__( 'Category*', 'sofw-vendors' ),
                    'id'               => 'category',
                    'type'             => 'select',
                    'show_option_none' => true,
                    'options'          => array(
                        'Appraiser' => esc_html__( 'Appraiser', 'sofw-vendors' ),
                        'Cleaning Service' => esc_html__( 'Cleaning Services', 'sofw-vendors' ),
                        'Flooring' => esc_html__( 'Flooring', 'sofw-vendors' ),
                        'Handyman' => esc_html__( 'Handyman', 'sofw-vendors' ),
                        'Heating / Cooling Companies' => esc_html__( 'Heating / Cooling Companies', 'sofw-vendors' ),
                        'Home Interior Designers & Stagers' => esc_html__( 'Home Interior Designers & Stagers', 'sofw-vendors' ),
                        'Home Inspectors' => esc_html__( 'Home Inspectors', 'sofw-vendors' ),
                        'Home Remodel Companies' => esc_html__( 'Home Remodel Companies', 'sofw-vendors' ),
                        'Insurance' => esc_html__( 'Insurance', 'sofw-vendors' ),
                        'Landscaping Services' => esc_html__( 'Landscaping Services', 'sofw-vendors' ),
                        'Lawyers / Attorneys'   => esc_html__( 'Lawyers / Attorneys', 'sofw-vendors' ),
                        'Lenders'   => esc_html__( 'Lenders', 'sofw-vendors' ),
                        'Movers'   => esc_html__( 'Movers', 'sofw-vendors' ),
                        'Paint Companies'   => esc_html__( 'Paint Companies', 'sofw-vendors' ),
                        'Pest Control / Termites'   => esc_html__( 'Pest Control / Termites', 'sofw-vendors' ),
                        'Pool Services'   => esc_html__( 'Pool Services', 'sofw-vendors' ),
                        'Plumber Services'   => esc_html__( 'Plumber Services', 'sofw-vendors' ),
                        'Roofing'   => esc_html__( 'Roofing', 'sofw-vendors' ),
                        'Septic Pumping'   => esc_html__( 'Septic Pumping', 'sofw-vendors' ),
                        'Title Company'   => esc_html__( 'Title Company', 'sofw-vendors' ),
                        'Lenders'   => esc_html__( 'Lenders', 'sofw-vendors' ),
                        'Water Filtration'   => esc_html__( 'Water Filtration', 'sofw-vendors' ),
                        'Web Services'     => esc_html__( 'Web Services', 'sofw-vendors' ),
                        'Window’s & Glass'     => esc_html__( 'Window’s & Glass', 'sofw-vendors' ),
                    ),
                    'attributes' => array(
                        'data-validation' => 'required',
                    ),
                ) );
    
                $main_options->add_field( array(
                    'name'         => esc_html__( 'Photo or Logo', 'sofw-vendors' ),
                    'id'           => 'photo_or_logo',
                    'type'         => 'file_list',
                    'preview_size' => 'medium',
                    'query_args' => array(
                        'type' => array(
                            'image/jpeg', // Make library only display these.
                            'image/jpg',
                            'image/png',
                            ),
                    ),
                ) );
                
                $main_options->add_field( array(
                    'name' => esc_html__( 'Video', 'sofw-vendors' ),
                    'desc' => esc_html__( 'Enter a youtube or vimeo URL. URL\'s for videos from the following services will also work: Amazon Kindle instant previews, Animoto, DailyMotion, Flickr, TED, TikTok, VideoPress, and WordPress.tv.', 'sofw-vendors' ),
                    'id'   => 'video',
                    'type' => 'oembed',
                ) );
    
                $main_options->add_field( array(
                    'name'    => esc_html__( 'About Us*', 'sofw-vendors' ),
                    'id'      => 'about_us',
                    'type'    => 'wysiwyg',
                    'options' => array(
                        'textarea_rows' => 5,
                    ),
                    'attributes' => array(
                        'data-validation' => 'required',
                    ),
                ) );
    
                $main_options->add_field( array(
                    'name'    => esc_html__( 'Services', 'sofw-vendors' ),
                    'id'      => 'services',
                    'type'    => 'wysiwyg',
                    'options' => array(
                        'textarea_rows' => 5,
                    ),
                ) );
                
                $main_options->add_field( array(
                    'name'       => esc_html__( 'Incentive / Form Title (Call to Action)*', 'sofw-vendors' ),
                    'id'         => 'incentive',
                    'type'       => 'text',
                    'attributes' => array(
                        'data-validation' => 'required',
                    ),
                ) );
                
    
                /**
                 * Registers secondary options page, and set main item as parent.
                 */
                $secondary_options = new_cmb2_box( array(
                    'id'           => 'sofw_vendors_secondary_options_page',
                    'title'        => esc_html__( 'Settings', 'cmb2' ),
                    'object_types' => array( 'options-page' ),
                    'option_key'   => 'sofw_vendors_secondary_options',
                    'parent_slug'  => 'sofw_vendors_options',
                ) );
                
                $secondary_options->add_field( array(
                    'name'       => esc_html__( 'Available Vendor Categories*', 'sofw-vendors' ),
                    'id'         => 'avaible_vendor_categories',
                    'type'       => 'text',
                    'repeatable' => true,
                    'attributes' => array(
                        'data-validation' => 'required',
                    ),
                ) );
                
    
            }
    
            
            public function sofw_vendors_save_override() {
                $cmb = cmb2_get_metabox( 'sofw_vendors_options_page', 'fake-oject-id' );
                
                // Check security nonce
                if ( ! isset( $_POST[ $cmb->nonce() ] ) || ! wp_verify_nonce( $_POST[ $cmb->nonce() ], $cmb->nonce() ) ) {
                    return $cmb->prop( 'submission_error', new WP_Error( 'security_fail', __( 'Security check failed.' ) ) );
                }
    
                // Check title submitted
                if ( empty( $_POST['business_name'] ) ) {
                    return $cmb->prop( 'submission_error', new WP_Error( 'post_data_missing', __( 'New post requires a Business Name.' ) ) );
                }
                
                // Fetch sanitized values
                $sanitized_values = $cmb->get_sanitized_values( $_POST );
                
                // 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 : 11;
                
                // Create the new post
                $new_submission_id = wp_insert_post( $post_data, true );
                
                // If we hit a snag, update the user
                if ( is_wp_error( $new_submission_id ) ) {
                    return $cmb->prop( 'submission_error', $new_submission_id );
                }
                
                $cmb->save_fields( $new_submission_id, 'vendor', $sanitized_values );
     
                /*
                 * Redirect back to the form page with a query variable with the new post ID.
                 * This will help double-submissions with browser refreshes
                 */
                $redirect = add_query_arg( array(
                    'page' => 'sofw_vendors_options',
                    'post_submitted' => $new_submission_id,
                    ), 
                    $_SERVER["HTTP_REFERER"],
                );
                wp_redirect( $redirect );
                
                exit;
            }
            
        }
    }
    • This reply was modified 3 years, 10 months ago by jeremycaris.

    @melaniemasi Thank you! We were having the same issue. Thanks to your post, we asked WPEngine to add the caching exclusion and it is working for us as well. They’re now working on a support ticket to apply it to all of our installs.

    I’m having the same issue, with the same question. Changing the sort order has no effect, and why would you sort the DESC by default instead of ASC? Anyway, until the dev sorts this out, here’s a quick CSS fix to reverse the list order:

    .wpf_items_group ul {
    transform: rotate(180deg);
    }
    .wpf_items_group ul > li {
    transform: rotate(-180deg);
    }

    Same thing is happening to me for the first time. It is also a new WordPress install, multisite is not enabled.

Viewing 15 replies - 1 through 15 (of 39 total)