WordPress.org

Ready to get started?Download WordPress

Forums

More Fields Plugin Remove Meta Boxes instead of Hiding them with Jquery (6 posts)

  1. RickyV
    Member
    Posted 5 years ago #

    My site uses a lot of plugins that add meta boxes to the write panel of wordpress. I’m using More Fields plugin to create different types of posts (EX: Events, Article, Gallery, etc) and show only the meta boxes assigned to each type of post. It works fine, the problem is that More Fields use Javascript to hide the boxes , instead of only loading the corresponding meta boxes. This causes that my write page load very slow.
    I tried to use remove_meta_box on different parts of my site but it didn’t work. I not sure if there is something wrong with this function.
    My current solution involves modifying the wordpress do_meta_boxes function defined on the Template.php file to filter the metaboxes based on the post type. Can anybody suggest a better solution?

    function do_meta_boxes($page, $context, $object) {
    	global $wp_meta_boxes;
    
    	$type = attribute_escape($_GET['type']);  //get type
    	if ($type == '') $type = 'Article'; // default type 
    
    	$article_mbx = array('yapbdiv','wp-post-thumbnail','postexcerpt','postcustom','commentstatusdiv','tagsdiv');
    	$event_mbx = array('yapbdiv','wp-post-thumbnail','postexcerpt','postcustom','commentstatusdiv','tagsdiv','ec3_schedule_editor','event');
    	$venue_mbx = array('yapbdiv','wp-post-thumbnail','postexcerpt','postcustom','commentstatusdiv','tagsdiv','venues');
    	$gallery_mbx = array('yapbdiv','postexcerpt','postcustom','commentstatusdiv','tagsdiv','wp-gallery-rmz','gallerydiv');
    	$rotator_mbx = array('yapbdiv','postexcerpt','postcustom','rot3_schedule_editor');
    	$blog_mbx = array('yapbdiv','wp-post-thumbnail','postexcerpt','postcustom','commentstatusdiv','tagsdiv');
    
    	do_action('do_meta_boxes', $page, $context, $object);
    
    	if ( !isset($wp_meta_boxes) || !isset($wp_meta_boxes[$page]) || !isset($wp_meta_boxes[$page][$context]) )
    		return;
    
    	foreach ( array('high', 'core', 'default', 'low') as $priority ) {
    		foreach ( (array) $wp_meta_boxes[$page][$context][$priority] as $box ) {
    			if ( false === $box )
    				continue;
    			if ($type == 'Article' && !in_array($box['id'], $article_mbx)) // filter metaboxes
    				continue;
    			if ($type == 'Event' && !in_array($box['id'], $event_mbx)) // filter metaboxes
    				continue;
    			if ($type == 'Venue' && !in_array($box['id'], $venue_mbx)) // filter metaboxes
    				continue;
    			if ($type == 'Gallery' && !in_array($box['id'], $gallery_mbx)) // filter  metaboxes
    				continue;
    			if ($type == 'Rotator' && !in_array($box['id'], $rotator_mbx)) // filter  metaboxes
    				continue;
    			if ($type == 'Blog' && !in_array($box['id'], $blog_mbx)) // filter  metaboxes
    				continue;	
    
    				echo '<div id="' . $box['id'] . '" class="postbox ' . postbox_classes($box['id'], $page) . '">' . "\n";
    				echo "<h3>{$box['title']}</h3>\n";
    				echo '<div class="inside">' . "\n";
    				call_user_func($box['callback'], $object, $box);
    				echo "</div>\n";
    				echo "</div>\n";
    
    		}
    	}
    }
  2. jpadie
    Member
    Posted 5 years ago #

    i guess there are two alternative approaches:

    1. do not rely on javascript but instead set the various div id's to display:none in the css. the meta boxes will download still but they will not 'load' on the page and this should save the rendering engine some time and speed up the process.

    2. at least in 2.8 and 2.7.1 (i've not checked any further back) there are action hooks for do_meta_boxes (for normal, advanced and side). i think you should be able to hook into any one of these and use the remove_meta_box() function to remove the offending metaboxes. something like this plugin should work

    <?php
    /*
    Plugin Name: removeMetaBoxes
    Plugin URI: http://rathercurious.net
    Description: Allows core meta boxes to be removed
    Author: Justin Adie
    Version: 1.0
    Author URI: http://rathercurious.net
    */
    
    class removeMetas{
    	public function __construct(){
    		add_action('do_meta_boxes', array($this, 'removeMetaBoxes'), 10, 3);
    	}
    
    	public function removeMetaBoxes($type, $context, $post){
    		/**
    		 * usages
    		 * remove_meta_box($id, $page, $context)
    		 * add_meta_box($id, $title, $callback, $page, $context = 'advanced', $priority = 'default')
    		 */
    		$boxes = array(	'slugdiv', 'postexcerpt', 'passworddiv', 'categorydiv',
    						'tagsdiv', 'trackbacksdiv', 'commentstatusdiv', 'commentsdiv',
    						'authordiv', 'revisionsdiv', 'postcustom');
    
    		foreach ($boxes as $box){
    			foreach (array('link', 'post', 'page') as $page){
    				foreach (array('normal', 'advanced', 'side') as $context){
    					remove_meta_box($box, $type, $context);
    				}
    			}
    		}
    	}
    }
    
    $removeMetas = new removeMetas();
    ?>
  3. bdwaldman
    Member
    Posted 4 years ago #

    @RickyV - Did you ever find a suitable solution to this?

  4. carlla
    Member
    Posted 4 years ago #

    A great code =D thanks! help me a lot.

    But it didn't work in tag's subanel in WP 2.8.4

    maybe the its name was changed in this version. I am looking for the new name in code. Any sugestion?

  5. Gene
    Member
    Posted 4 years ago #

    Carlia- when using jpadie's plugin solution on 2.8 and later change 'tagsdiv' to 'tagsdiv-post_tag' and all should be well

  6. carlla
    Member
    Posted 4 years ago #

    ok.. done!.. it work well now.. thank you very much =)

Topic Closed

This topic has been closed to new replies.

About this Topic