WordPress.org

Ready to get started?Download WordPress

Forums

Multiple Template Images
[resolved] Customizing MTI to support taxonomies... (2 posts)

  1. ururk
    Member
    Posted 3 years ago #

    I needed to get this working on a WP site with multiple taxonomies. In addition, I wanted to clean up the admin screen so site users would only see the appropriate thumbnails. I don't claim that my code changes are the most efficient... but they work:

    Starting with functions.php, we modify the code:

    mti_add_image_slot('clients-image-1', 'Portfolio Image', array('clients'));

    Note the new item - an array. This contains the post types (default post / page) that are accepted.

    Next, we need to modify the core plugin code:

    Modifying _on_admin_menu - loop through every image_slot, and check to see if we should add the meta box to the post type page:

    if(function_exists('add_meta_box') && !empty($this->image_slots)) // The function check seems unnecessary (add_meta_box() exists in all recent versions)
          {
    		// Only display Template Images on specified post types
    		$page_types = array();
    		foreach($this->image_slots as $value) {
    			foreach($value['post_types'] as $value2) {
    				// $value2 represents a post type
    				if (!in_array($value2, $page_types)) {
    					$page_types[] = $value2;
    					add_meta_box($this->name, __('Template Images', $this->name), array(&$this, 'show_meta_box'), $value2, 'normal', 'high');
    				}
    			}
    		}
          }

    Next, modify add_image_slot - we only assign an image_slot if the currently edited (or added) post_type matches the image_slot. I'm not too happy to whole-scale copy the first part of posts.php, but I did make some modifications:

    // Register an image slot in the posts and page editors
        function add_image_slot($img_id, $title, $post_types = array('post', 'page'), $default = null)
        {
    		if ( isset($_GET['post']) )
    			$post_id = (int) $_GET['post'];
    		elseif ( isset($_POST['post_ID']) )
    			$post_id = (int) $_POST['post_ID'];
    		else
    			$post_id = 0;
    
    		$post_ID = $post_id;
    		$post = null;
    		$post_type_object = null;
    		$post_type = null;
    		if ( $post_id ) {
    			$post = get_post($post_id);
    			if ( $post ) {
    				$post_type = $post->post_type;
    			}
    		} elseif ( isset($_POST['post_type']) ) {
    			$post_type = $_POST['post_type'];
    		} elseif ( isset($_GET['post_type']) ) {
    			$post_type = $_GET['post_type'];
    		}
    
    		if (in_array($post_type, $post_types)) {
    	    	$this->image_slots[$img_id] = array('title' => $title, 'post_types' => $post_types, 'default' => $default);
    		}
    
        }

    Finally, a small change to mti_add_image_slot - add in defaults post_types:

    function mti_add_image_slot($img_id, $title, $post_types = array('post', 'page'), $default = null)
      {
        global $templateimages;
        return $templateimages->add_image_slot($img_id, $title, $post_types, $default);
      }

    http://wordpress.org/extend/plugins/multiple-template-images/

  2. thatwastaxing
    Member
    Posted 2 years ago #

    Thanks a lot for sharing! Works with my custom post types now ;)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic