WordPress.org

Forums

Default featured image
Filter only for specific post types (8 posts)

  1. Knut Sparhell
    Member
    Posted 1 year ago #

    I need this nicely working plugin, and I need it not to use default featured images for anything but posts. I have tried the dfi_thumbnail_id filter, but it will not work. The reason is the very intrusive get_post_metadata filter method set_dfi_meta_key that you are using. It will make has_post_thumbnail always return true and show the default featured image thereafter. You are manipulating every get_post_meta with key _thumbnail_id to return your plugin option dfi_image_id instead. This can't be overridden or filtered.

    So please add this filter to the beginning of method difi_set_meta_key:

    if ( ! in_array( get_post_type( $object_id ), apply_filters( 'dfi_post_types', get_post_types() ) ) )
    	return $null;

    Then I and others users can add this filter to exclude:

    function my_difi_post_types( $post_types ) {
    	return array( 'post' );
    }
    add_filter( 'dfi_post_types', 'my_difi_post_types' );

    Another thing: Shouldn't a filter method that wishes to do nothing in certain situations then just return the first argument? That's what I have learned. You name your first argument $null so therefore you should return $null to cancel filtering. And rename the variable to $metadata, since $null is a really bad variable name.

    Actions, on the other hand, may just return to cancel.

    http://wordpress.org/plugins/default-featured-image/

  2. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Hello Knut,

    those are some insightful comments. I'll take a serious look at them on saterday. One thing I won't do is rename $null, see wp-includes/meta.php Line #276 That's why.

    I do see a problem with your suggestion. It won't allow to set different thumbs per posttype.

  3. Knut Sparhell
    Member
    Posted 1 year ago #

    Setting different images for each post type would be a nice enhancement.

    Renaming a variable has to do with readability for humans only.

  4. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Hello Knut,

    I've changed where the filtering happens. has_post_thumbnail will now return false if the filter does not return a image.

    It also works to set different images per posttype.
    And by your suggestion I changed a few return values.

    This should fix your problem. Let me know.

  5. Knut Sparhell
    Member
    Posted 1 year ago #

    Actually, it didn't. I use some custom post types in special widgets, like ads and such, and I get those using get_posts(). Then the global $post variable is not set and I can't check for get_post_type() without that global or the actual post_id.

    That's the reason I wanted your plugin to check for the post_type itself, as it has the post_id.

    But I decided to globalize even custom post types used in special widgets for now, so it will work with your plugin. I don't need to, since I'm not using a general template to display them, so I would like not to.

    But an easy fix is to supply the $object_id as a second argument with the filter, as context. Supplying as much of context as possible, when applying a filter, is good practice. So, please.

    return apply_filters( 'dfi_thumbnail_id', get_option( 'dfi_image_id' ), $object_id );

    I will then use:
    add_filter( 'dfi_thumbnail_id', 'my_dfi_thumbnail_id', 10, 2 );

    but those who don't need the extra information will not need to change their function definition. The extra parameter will only be supplied i the call when two arguments are desired in add_filter.

  6. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Ahh I rarely use get_posts so I never thought of the non global calls. I'll add the extra param that should help indeed.
    I might do it tonight but probably tomorrow.

  7. Knut Sparhell
    Member
    Posted 1 year ago #

    Thank you responding so positively!

    Suggestion for future enhancement:

    1. Migrate to option keys "dfi_{$post_type}_thumbnail_id" or to an array with post_type as keys. Could easily be made bw compatible.

    2. Check for and list all registered post types supporting 'post_thumbnail' and that current_theme_supports('post_thumbnails'), and let users upload an image for each of them through the settings section.

  8. janw.oostendorp
    Member
    Plugin Author

    Posted 1 year ago #

    Hee Knut,

    I've updated the filter to give the post_id as an argument. And I've updated the faq.

    I've given thought of allowing to set custom post types and categories.
    I might someday, but I'm afraid of over complicating it. I kinda like that it's small as possible.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.