• I’m trying to hook into and filter a few things in my template and am having difficulty finding out how to do so after much scouring of the web.

    1) Need to hook into the Admin Media Library thumbnail src (the File column) so I can modify the path of the images without using image_size. I won’t get into why, I just need to be able to programmatically change what image is being fetched because the images are not being stored in the media library. Due to naming conventions, I can easily do this as long as I know how to get the src so I can modify it and then need to make sure it’s replaced before the thumbnails are loaded.

    2) I need to hook into a custom post type edit screen Featured Image src to do the same as the above, so instead of using the default src it will use my modified path to the image to display.

    3) I also need to hook into custom fields for attaching media and do the same—src manipulation. I use a plugin from github to generate the metaboxes (fieldmanager), so I will contact those devs as well, but I’m guessing they use standard code for adding the metaboxes and displaying attached media previews.

    4) I also can’t find how to sort a custom column for a CPT by another CPT meta key. In brief, CPT A is fetching a list of all posts of CPT B and displaying them in a select menu as a custom field and storing that selection as metadata. I’m loading the titles of CPT B into a custom column in the post list of CPT A. I need to sort those titles alphabetically, but the only way that seems possible is if I can also query CPT B custom meta field I created that provides a single word for sorting. Would appreciate a point in the right direction for how to accomplish this. Have been searching for months for how to do this.

    5) Using custom icons for PDFs and Text files is bugged in the Media Library list view, throws errors and displays no image (it looks in the core path for the icons instead of the path provided using wp_mime_type_icon function). Grid view works fine. Not sure if this is being tracked, but figured I’d mention it.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter liquidRock

    (@liquidrock)

    For some reason I can’t edit this post, but besides modifying the src for the images mentioned above (points 1 and 2), I would also like to add a class to them.

    Thread Starter liquidRock

    (@liquidrock)

    I managed to solve the first 3 problems by filtering wp_get_attachment_image_attributes finding the src array key and then modifying it and returning the $attr.

    Still would love to know how to sort a column the way I proposed in point 4.

    Moderator bcworkz

    (@bcworkz)

    You can normally only sort columns by criteria you can set as WP_Query “orderby” query vars. A meta value sort is possible, but it’ll be the meta value of the post type being queried, not one that is indirectly related to it.

    However, you could directly alter the orderby clause in the SQL query to sort by any field that is referenced in the query. Use the “posts_orderby” filter. You’ll need to examine the entire SQL query to learn how to reference the appropriate field. You’ll also need some mechanism to ensure you only alter the right queries and leave all others alone.

    I’m not seeing any icon issues in the media library on my site, at least for PDF files. I don’t have any text “media”. The icons by default come from /wp-includes/images/media/. Themes and plugins can alter this through filtering. I suspect that’s where you’re encountering trouble.

    Thread Starter liquidRock

    (@liquidrock)

    Thanks @bcworkz

    Yeah, I figured it would be complicated to sort the column this way, but not sure how else to accomplish it. I don’t know SQL at all. I’m wondering if there’s some way to append an incremental data-attribute using a counter and sort by that, although not sure if it’s possible to apply the sort once all the tables have been populated and that attribute created. idk. I might just have to wait until I can hire someone to do this properly.

    As for the document icons, I don’t get errors from the core media folder images, I’m replacing them with custom images.

    Here is the code I’m using.

    
    add_filter( 'wp_mime_type_icon', function( $icon, $mime, $post_id )
    {
        if( 'text/plain' === $mime && $post_id > 0 )
            $icon = get_template_directory_uri() . '/library/images/icons/txt.png';
    	
        if( 'application/pdf' === $mime && $post_id > 0 )
            $icon = get_template_directory_uri() . '/library/images/icons/pdf.png';
    	
        return $icon;
    }, 10, 3 );

    `

    The above code works fine if you view the Media Library in grid mode. There are no errors and the custom images load for the documents. However, view in list mode, it throws errors, looks for the image in the core folder, and no image is loaded at all.

    Moderator bcworkz

    (@bcworkz)

    The list table is re-queried when someone clicks one of the sorting column heads, so it’s feasible to set something on the initial pass through. You could use the post’s menu_order field (if not already in use) to contain sorting data, which is possible to order by in WP_Query, so can be sorted by in the list table.

    For icons, try the ‘wp_get_attachment_image_src’ filter instead or in addition. If you look at the source code for the related function, WP indeed only uses the wp_mime_type_icon() result to extract the filename, then assumes the path is to core files. Or you could continue to use ‘wp_mime_type_icon’, but also filter ‘icon_dir’ as well. This may be better since I think the grid view uses JSON data and does not use ‘wp_get_attachment_image_src’.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hooking into the Media Library template and Custom Post Edit screen, Meta Column’ is closed to new replies.