Include Group plug-in in theme files with Roots

Support MB Group Include Group plug-in in theme files with Roots

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #561
    leebazleebaz
    Participant

    I just bought the GROUP extension.
    I'm using META-BOX with ROOTS to create a custom theme. I'm including the meta-box plug-in directly in the theme with this:

    
     // Re-define meta box path and URL
     define( 'RWMB_URL', trailingslashit( get_template_directory_uri() . '/assets/meta-box' ) );
     define( 'RWMB_DIR', trailingslashit( TEMPLATEPATH . '/assets/meta-box' ) );
     // Include the meta box script
     require_once RWMB_DIR . 'meta-box.php';
    

    And I'd like to do the same with the group plug in as currently it's not working.
    My meta boxes are set up like this:

    
    $meta_boxes[] = array(
    	// Meta box id, UNIQUE per meta box. Optional since 4.1.5
    	'id' => 'faq',
    	// Meta box title - Will appear at the drag and drop handle bar. Required.
    	'title' => __( 'Product FAQs', 'rwmb' ),
    	// Post types, accept custom post types as well - DEFAULT is array('post'). Optional.
    	'pages' => array( 'product' ),
    	// Where the meta box appear: normal (default), advanced, side. Optional.
    	'context' => 'normal',
    	// Order of meta box: high (default), low. Optional.
    	'priority' => 'low',
    	// Auto save: true, false (default). Optional.
    	'autosave' => true,
    	// List of meta fields
    	'fields' => array(
    			// Group
    			array(
    			    'name' => 'Group', // Optional
    			    'id' => 'group_id',
    			    'type'   => 'group', // Group type
    			    'clone'  => true,    // Can be cloned?
    					// TEXTAREA
    					array(
    						'name' => __( 'FAQ', 'rwmb' ),
    						'desc' => __( 'Add the FAQ here', 'rwmb' ),
    						'id'   => "{$prefix}faq",
    						'type' => 'textarea',
    						'cols' => 20,
    						'rows' => 3,
    						'clone' => true,
    					),
    			)
    	
    	
    	),
    
    );
    

    But I get the following error:

    Warning: call_user_func() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in /Users/Lee/Sites/akita/wp-content/themes/akita/assets/meta-box/inc/meta-box.php on line 233

    Thanks for any help!

    #570
    Anh TranAnh Tran
    Keymaster

    Hi,

    I see that the problem is you use the old way to integrate theme with Meta Box plugin. It's not recommended anymore as it broke as you saw.

    To eliminate this problem, please

    - Remove the first block of code to include Meta Box
    - Install Meta Box from wp.org as a normal plugin
    - Install MB Group extension
    - Register meta boxes according to this documentation (I don't see your full code so I'm not sure you're using rwmb_meta_boxes filter, if yes - then it's perfect, you don't need to change anything, but if no, please update the code).

    I wrote another documentation explaining why we should use plugin instead of included it directly in themes. Please take a look.

    Best regards

    #574
    leebazleebaz
    Participant

    Hi, thanks for your reply. I've moved the plug-ins and activated them as you said.
    Here is the full meta-box registering code:

    <?php
    /********************* META BOX DEFINITIONS ***********************/
    
    /**
     * Prefix of meta keys (optional)
     * Use underscore (_) at the beginning to make keys hidden
     * Alt.: You also can make prefix empty to disable it
     */
    // Better has an underscore as last sign
    $prefix = 'rw_';
    
    global $meta_boxes;
    
    $meta_boxes = array();
    
    //Home page slider inclusion//////////////////////////////////////////////////////////////////////////////////
    
    $meta_boxes[] = array(
    	// Meta box id, UNIQUE per meta box. Optional since 4.1.5
    	'id' => 'home-page-content',
    	// Meta box title - Will appear at the drag and drop handle bar. Required.
    	'title' => __( 'Conent for the Home Page', 'rwmb' ),
    	// Post types, accept custom post types as well - DEFAULT is array('post'). Optional.
    	'pages' => array( 'page' ),
    	// Where the meta box appear: normal (default), advanced, side. Optional.
    	'context' => 'normal',
    	// Order of meta box: high (default), low. Optional.
    	'priority' => 'high',
    	// Auto save: true, false (default). Optional.
    	'autosave' => true,
    	// List of meta fields
    	'fields' => array(
    	// TEXT BOX ONE
    	array(
    		// Field name - Will be used as label
    		'name'  => __( '1st Info Box Title', 'rwmb' ),
    		'id'    => "{$prefix}1st_box_title",
    		'desc'  => __( 'Enter the title for the first info box', 'rwmb' ),
    		'type'  => 'text',
    		'std'   => __( 'e.g what we do', 'rwmb' ),
    		'clone' => false,
    	),
    	// TEXTAREA
    	array(
    		'name' => __( '1st Info Box text content', 'rwmb' ),
    		'desc' => __( 'Text content for the first info box', 'rwmb' ),
    		'id'   => "{$prefix}1st_box_content",
    		'type' => 'textarea',
    		'cols' => 20,
    		'rows' => 3,
    	),
    	// TEXT BOX TWO
    	array(
    		// Field name - Will be used as label
    		'name'  => __( '2nd Info Box Title', 'rwmb' ),
    		'id'    => "{$prefix}2nd_box_title",
    		'desc'  => __( 'Enter the title for the second info box', 'rwmb' ),
    		'type'  => 'text',
    		'std'   => __( 'e.g what we do', 'rwmb' ),
    		'clone' => false,
    	),
    	// TEXTAREA
    	array(
    		'name' => __( '2nd Info Box text content', 'rwmb' ),
    		'desc' => __( 'Text content for the second info box', 'rwmb' ),
    		'id'   => "{$prefix}2nd_box_content",
    		'type' => 'textarea',
    		'cols' => 20,
    		'rows' => 3,
    	),
    	// RADIO BUTTONS
    	array(
    		'name'    => __( 'Video or Image', 'rwmb' ),
    		'id'      => "{$prefix}vid_img",
    		'type'    => 'radio',
    		// Array of 'value' => 'Label' pairs for radio options.
    		// Note: the 'value' is stored in meta field, not the 'Label'
    		'options' => array(
    			'usevid' => __( 'Video', 'rwmb' ),
    			'useimg' => __( 'Image', 'rwmb' ),
    		),
    	),
    	// URL
    	array(
    		'name'  => __( 'Video URL', 'rwmb' ),
    		'id'    => "{$prefix}vid_url",
    		'desc'  => __( 'Paste the YouTube EMBED URL here', 'rwmb' ),
    		'type'  => 'url',
    		'std'   => 'http://youtube.com/',
    	),
    	// IMAGE ADVANCED (WP 3.5+)
    	array(
    		'name'             => __( 'Video Replacement Image', 'rwmb' ),
    		'id'               => "{$prefix}vid_repl_img",
    		'type'             => 'image_advanced',
    		'max_file_uploads' => 1,
    		'desc'			   =>'Upload images for the home page ad next to the video 900px by 510px',
    	),
    	// IMAGE ADVANCED (WP 3.5+)
    	array(
    		'name'             => __( 'Video image advertisement', 'rwmb' ),
    		'id'               => "{$prefix}advert",
    		'type'             => 'image_advanced',
    		'max_file_uploads' => 1,
    		'desc'			   =>'Upload images for the home page ad next to the video 720px by 200px',
    	),
    	// URL
    	array(
    		'name'  => __( 'Ad URL', 'rwmb' ),
    		'id'    => "{$prefix}ad_url",
    		'desc'  => __( 'Paste the URL where you want the ad to link to here', 'rwmb' ),
    		'type'  => 'url',
    		'std'   => '',
    	),
    	// 1st tag line
    	array(
    		// Field name - Will be used as label
    		'name'  => __( '1st Tag Line', 'rwmb' ),
    		'id'    => "{$prefix}1st_tag_line",
    		'desc'  => __( 'Enter the 1st tag line here', 'rwmb' ),
    		'type'  => 'text',
    		'std'   => __( 'e.g across the planet', 'rwmb' ),
    		'clone' => false,
    	),
    	// 2nd tag line
    	array(
    		// Field name - Will be used as label
    		'name'  => __( '2nd Tag Line', 'rwmb' ),
    		'id'    => "{$prefix}2nd_tag_line",
    		'desc'  => __( 'Enter the 2nd tag line here', 'rwmb' ),
    		'type'  => 'text',
    		'std'   => __( 'e.g across the planet', 'rwmb' ),
    		'clone' => false,
    	),
    	// IMAGE ADVANCED (WP 3.5+)
    	array(
    		'name'             => __( 'Images', 'rwmb' ),
    		'id'               => "{$prefix}slideimg",
    		'type'             => 'image_advanced',
    		'max_file_uploads' => 3,
    		'desc'			   =>'Upload images for the page backgrounds 1440px by 875px',
    	),
    	
    	),
    	'only_on'    => array(
    		//'id'       => array( 1, 2 ),
    		 //'slug'  => array( 'home' ),
    		'template' => array( 'template-landing.php' ),
    		//'parent'   => array( 10 )
    	),
    );
    
    $meta_boxes[] = array(
    	// Meta box id, UNIQUE per meta box. Optional since 4.1.5
    	'id' => 'faq',
    	// Meta box title - Will appear at the drag and drop handle bar. Required.
    	'title' => __( 'Product FAQs', 'rwmb' ),
    	// Post types, accept custom post types as well - DEFAULT is array('post'). Optional.
    	'pages' => array( 'product' ),
    	// Where the meta box appear: normal (default), advanced, side. Optional.
    	'context' => 'normal',
    	// Order of meta box: high (default), low. Optional.
    	'priority' => 'low',
    	// Auto save: true, false (default). Optional.
    	'autosave' => true,
    	// List of meta fields
    	'fields' => array(
    			// Group
    			array(
    			    'name' => 'Group', // Optional
    			    'id' => 'group_id',
    			    'type'   => 'group', // Group type
    			    'clone'  => true,    // Can be cloned?
    					// TEXTAREA
    					array(
    						'name' => __( 'FAQ', 'rwmb' ),
    						'desc' => __( 'Add the FAQ here', 'rwmb' ),
    						'id'   => "{$prefix}faq",
    						'type' => 'textarea',
    						'cols' => 20,
    						'rows' => 3,
    						'clone' => true,
    					),
    			)
    	
    	
    	),
    
    );
    
    /********************* Add more meta boxes below ***********************/
    
    /********************* META BOX REGISTERING ***********************/
    
    /**
     * Register meta boxes
     *
     * @return void
     */
    function rw_register_meta_boxes()
    {
    	global $meta_boxes;
    
    	// Make sure there's no errors when the plugin is deactivated or during upgrade
    	if ( class_exists( 'RW_Meta_Box' ) ) {
    		foreach ( $meta_boxes as $meta_box ) {
    			if ( isset( $meta_box['only_on'] ) && ! rw_maybe_include( $meta_box['only_on'] ) ) {
    				continue;
    			}
    
    			new RW_Meta_Box( $meta_box );
    		}
    	}
    }
    
    add_action( 'admin_init', 'rw_register_meta_boxes' );
    
    /**
     * Check if meta boxes is included
     *
     * @return bool
     */
    function rw_maybe_include( $conditions ) {
    	// Include in back-end only
    	if ( ! defined( 'WP_ADMIN' ) || ! WP_ADMIN ) {
    		return false;
    	}
    
    	// Always include for ajax
    	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
    		return true;
    	}
    
    	if ( isset( $_GET['post'] ) ) {
    		$post_id = $_GET['post'];
    	}
    	elseif ( isset( $_POST['post_ID'] ) ) {
    		$post_id = $_POST['post_ID'];
    	}
    	else {
    		$post_id = false;
    	}
    
    	$post_id = (int) $post_id;
    	$post    = get_post( $post_id );
    
    	foreach ( $conditions as $cond => $v ) {
    		// Catch non-arrays too
    		if ( ! is_array( $v ) ) {
    			$v = array( $v );
    		}
    
    		switch ( $cond ) {
    			case 'id':
    				if ( in_array( $post_id, $v ) ) {
    					return true;
    				}
    			break;
    			case 'parent':
    				$post_parent = $post->post_parent;
    				if ( in_array( $post_parent, $v ) ) {
    					return true;
    				}
    			break;
    			case 'slug':
    				$post_slug = (!empty( $post->post_name));
    				if ( in_array( $post_slug, $v ) ) {
    					return true;
    				}
    			break;
    			case 'template':
    				$template = get_post_meta( $post_id, '_wp_page_template', true );
    				if ( in_array( $template, $v ) ) {
    					return true;
    				}
    			break;
    		}
    	}
    
    	// If no condition matched
    	return false;
    }

    but I'm still getting the following error message in the editor:

    Notice: Undefined index: fields in /Users/Lee/Sites/akita/wp-content/plugins/meta-box-group/group.php on line 43

    Warning: Invalid argument supplied for foreach() in /Users/Lee/Sites/akita/wp-content/plugins/meta-box-group/group.php on line 43

    #577
    Anh TranAnh Tran
    Keymaster

    Oh, I see. The problem is you missed the fields parameter for group field. You have to do like this:

    'fields' => array(
        array(
            'id' => 'group',
            'type' => 'group',
            'fields' => array( // you missed this parameter for group
                array(...),
                array(...),
            ),
        ),
    ),
    #579
    leebazleebaz
    Participant

    Thank you - appears to be working now!

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Include Group plug-in in theme files with Roots’ is closed to new replies.