Custom fields not showing

Support General Custom fields not showing

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #48518
    Jai IvarssonJai Ivarsson
    Participant

    I have created numerous plugins with Meta Box and extensions bundled in using composer without problems but for some reason, my latest plugin isn't working. The custom post type is being created and I can var_dump to see that the add_filter for adding the fields is being called correctly but the custom fields just aren't showing on the custom post edit page. I even put one of my other plugins in instead to rule out something specific with this site and it didn't work.

    This is my Custom post class. Anyone see something stupid I'm doing?

    class RentalPostType extends Base
    {
        public function register()
        {
            add_action('init', [$this, 'register_rental_post_type']);
            add_filter('rwmb_meta_boxes', [$this, 'rental_fields']);
        }
    
        public function register_rental_post_type()
        {
            $labels = [
                'name'                     => esc_html__('Rentals', $this->text_domain),
                'singular_name'            => esc_html__('Rental', $this->text_domain),
                'add_new'                  => esc_html__('Add New', $this->text_domain),
                'add_new_item'             => esc_html__('Add New Rental', $this->text_domain),
                'edit_item'                => esc_html__('Edit Rental', $this->text_domain),
                'new_item'                 => esc_html__('New Rental', $this->text_domain),
                'view_item'                => esc_html__('View Rental', $this->text_domain),
                'view_items'               => esc_html__('View Rentals', $this->text_domain),
                'search_items'             => esc_html__('Search Rentals', $this->text_domain),
                'not_found'                => esc_html__('No rentals found.', $this->text_domain),
                'not_found_in_trash'       => esc_html__('No rentals found in Trash.', $this->text_domain),
                'parent_item_colon'        => esc_html__('Parent Rental:', $this->text_domain),
                'all_items'                => esc_html__('Rentals', $this->text_domain),
                'archives'                 => esc_html__('Rental Archives', $this->text_domain),
                'attributes'               => esc_html__('Rental Attributes', $this->text_domain),
                'insert_into_item'         => esc_html__('Insert into rental', $this->text_domain),
                'uploaded_to_this_item'    => esc_html__('Uploaded to this rental', $this->text_domain),
                'featured_image'           => esc_html__('Featured image', $this->text_domain),
                'set_featured_image'       => esc_html__('Set featured image', $this->text_domain),
                'remove_featured_image'    => esc_html__('Remove featured image', $this->text_domain),
                'use_featured_image'       => esc_html__('Use as featured image', $this->text_domain),
                'menu_name'                => esc_html__('Rentals', $this->text_domain),
                'filter_items_list'        => esc_html__('Filter rentals list', $this->text_domain),
                'filter_by_date'           => esc_html__('', $this->text_domain),
                'items_list_navigation'    => esc_html__('Rentals list navigation', $this->text_domain),
                'items_list'               => esc_html__('Rentals list', $this->text_domain),
                'item_published'           => esc_html__('Rental published.', $this->text_domain),
                'item_published_privately' => esc_html__('Rental published privately.', $this->text_domain),
                'item_reverted_to_draft'   => esc_html__('Rental reverted to draft.', $this->text_domain),
                'item_scheduled'           => esc_html__('Rental scheduled.', $this->text_domain),
                'item_updated'             => esc_html__('Rental updated.', $this->text_domain),
            ];
            $args = [
                'label'               => esc_html__('Rentals', $this->text_domain),
                'labels'              => $labels,
                'description'         => '',
                'public'              => true,
                'hierarchical'        => false,
                'exclude_from_search' => false,
                'publicly_queryable'  => true,
                'show_ui'             => true,
                'show_in_nav_menus'   => true,
                'show_in_admin_bar'   => true,
                'show_in_rest'        => true,
                'query_var'           => true,
                'can_export'          => false,
                'delete_with_user'    => false,
                '_slug_changed'       => true,
                'has_archive'         => true,
                'rest_base'           => '',
                'show_in_menu'        => true,
                'menu_position'       => '',
                'menu_icon'           => 'dashicons-location',
                'capability_type'     => 'post',
                // 'capabilities'        => [
                //     'create_posts' => 'do_not_allow', // Removes 'Add New' button
                // ],
                'supports'            => ['title', 'thumbnail'],
                'taxonomies'          => ['sd-region', 'sd-city', 'sd-suburb'],
                'rewrite'             => [
                    'with_front' => false,
                ],
            ];
    
            register_post_type('sd-rental', $args);
        }
    
        public function rental_fields($meta_boxes)
        {
            $meta_boxes[] = [
                'title'      => __('Rental Fields', $this->text_domain),
                'id'         => $this->text_domain . '-rental-fields',
                'post_types' => ['sd-rental'],
                'closed'     => false,
                'revision'   => false,
                'fields'     => [
                    [
                        'name'            => __('Display Address', $this->text_domain),
                        'id'              => $this->prefix . 'display_address',
                        'type'            => 'text',
                        'hide_from_rest'  => false,
                        'hide_from_front' => false,
                        'limit_type'      => 'character',
                    ],
                ]
            ];
    
            return $meta_boxes;
        }
    }
    #48519
    PeterPeter
    Moderator

    Hello Jai,

    Thanks for reaching out.

    There could be an issue with the priority of loading the Meta Box plugin. If I add your code to the file functions.php in a theme folder, the CPT and custom fields are registered as well. Please check this screenshot https://ibb.co/v4LT2SF2

    I think that when you plugin/custom code is executed before Meta Box plugin and filter hook rwmb_meta_boxes so the custom field is not registered to display on the editing post.

    #48520
    Jai IvarssonJai Ivarsson
    Participant

    Thanks Peter. I have been suspecting load order as the cause but can't see how to fix it...

    #48521
    Jai IvarssonJai Ivarsson
    Participant

    I tried loading the fields in the functions.php and it worked.
    I added some debugging to try and understand what is happening.

    this in the plugin class that registers the post type and fields:

            error_log('Admin Init - MetaBox Debug:');
            error_log('RWMB_VER: ' . (defined('RWMB_VER') ? RWMB_VER : 'not defined'));
            error_log('MetaBox Class exists: ' . (class_exists('RWMB_Core') ? 'yes' : 'no'));
    

    All comes back as expected in the logs:
    Admin Init - MetaBox Debug:
    RWMB_VER: 5.10.10
    MetaBox Class exists: yes

    I added this after the registration of everything and confirmed that nothing is being registered:

    
    $meta_box_registry = rwmb_get_registry('meta_box');
    $field_registry = rwmb_get_registry('field');
    var_dump($meta_box_registry->all());
    var_dump($field_registry);
    

    both dumps being empty

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.