Custom Fields
Re-Envisioned
Support MB User Meta Expanding User Meta to work with other post types

This topic contains 0 replies, has 1 voice, and was last updated by  pluginoven 4 months, 2 weeks ago.

  • Creator
    Topic
  • #14471

    pluginoven
    Participant

    I am using a plugin to create multiple tabs in the user profiles. Since these tabs are not on the ‘profile’ or ‘user-edit’ pages, any meta_box that has type => ‘user’ will not render.

    Here are the changes so that User Meta will work with other plugins and on any post_type:

    File: inc/class-mb-user-meta-box.php

    appended add_meta_boxes to the object_hooks function:

    protected function object_hooks() {
        ...
        // Allow user meta fields to be used by other plugins and post types.
        add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
        ...
    }

    add a filter to expand valid edit_screens

    public function is_edit_screen( $screen = null ) {
        if ( ! is_admin() ) {
            return false;
        }
        $screen = get_current_screen();
        // Allow edit screens to be expanded.
        $edit_screens = apply_filters( 'rwmb_user_edit_screens', array( 'profile', 'user-edit', 'profile-network', 'user-edit-network' ) );
        return in_array( $screen->id, $edit_screens, true );
    }

    do not limit get_current_user_id to user-edit and profile pages:

    public static function get_current_user_id() {
        if ( ! is_admin() ) {
            return false;
        }
    
        if( !empty( $_REQUEST['user_id'] )){
            return absint( $_REQUEST['user_id'] );
        }
        else if( get_current_user_id() ){
            return get_current_user_id();
        }
    
        return false;
    }

    I’ll submit a pull request on bitbucket if you like to include these useful changes in a future release.

You must be logged in to reply to this topic.