• // Set the dfi in cache.
    		$meta_cache['_thumbnail_id'][0] = apply_filters( 'dfi_thumbnail_id', $dfi_id, $object_id );
    		wp_cache_set( $object_id, $meta_cache, 'post_meta' );

    Found this line in the plugin (searching for ‘dfi_thumbnail_id’) Would I place the additional code above or below this … or within functions.php? (‘dfi_thumbnail_id’ doesn’t appear in function.php – maybe another core? – or perhaps it only relates to your app’s php code)

    function dfi_category ( $dfi_id, $post_id ) {
    	if ( has_category( 'Free Section', $post_id ) ) {
            ...
            ..
            .
            }
    }
    add_filter( 'dfi_thumbnail_id', 'dfi_category', 10, 2 );

    ** Out of curiousity, what do the “10” and “2” relate too (if not too technical!)

    I’m also wondering if this add_filter will mean that WHENEVER it finds a post in that category, it will use that image. If poss, I’d like it to be “conditional”, ie if the post does have a feature image attached, use that, otherwise, substitute the default as set by function above

    (Don’t want much, do I !!)

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Jan-Willem

    (@janwoostendorp)

    Hi,

    Never change the code in a plugin, if a plugin get’s an upgrade your changes will get overwritten. Also some security scanners will flag it as a broken/infected plugin.

    I see you found some of the examples I created to filter where/when the DFI should show up.

    add_filter is A WordPress core feature. When code triggers certain events you can filter that event. In this case “filter”(/change) the image ID to set the image you would like.

    The add_filter allows you to specify the priority (10) and how many arguments the filter uses(2). Usually the defaults are fine.

    Where to put the code of a filter?
    You could put it in the functions.php of your theme. But it it’s a theme designed by someone else I could also be erased by an update.

    I recommend creating a new plugin, create the file.
    wp-content/plugins/dfi-category-exceptions.php

    Add the filter code inside there.

    <?php
    /**
     * Plugin Name:       Default Feature Image - category exception.
     * Plugin URI:        https://www.ads-software.com/support/topic/where-to-place-modified-code/
     * Version:          1.1
     * Requires at least: 5.0
     */
    
    function dfi_category( $dfi_id, $post_id ) {
    	if ( has_category( 'free-section', $post_id ) ) {
    		return 166;  // different image ID for Free Section.
    	}
    
    	return $dfi_id; // the "regular" DFI set in the media settings.
    }
    
    add_filter( 'dfi_thumbnail_id', 'dfi_category', 10, 2 );

    Don’t forget to activate the plugin from your admin ??
    Let me know if you need any other help.

    Jan-Willem

    Thread Starter cristofayre

    (@cristofayre)

    THIS is very insightful! I didn’t think it was THAT simple to create a plugin, (albeit simplified) I did wonder about the plugin upgrade ‘problem’ erasing amendment; this now solves that. Many thanks. (and for explaining “10” and “2”)

    ** You might have already covered this with the “regular DFI set in media settings”, but presume that refers to the single image in the admin area.

    Sorry to be a pain, but need clarification on the use default or user uploaded Feature Image conditional, (ie if they add a feature image to a their post, don’t overwrite it with the default image) Perhaps it has something to do with the “has_thumbnail” function. I don’t write PHP, (more javascript / perl / css / html) Presume it would be something along the lines of – syntax incorrect:

    
    if ($post_id has_thumbnail){
    return //from set exceptions plugin
    }
    else{
    if (catergory === "free"){
    // use DFI for free cat etc
    }
    else if (category === "tec"){
    // use default icon for 'tec'
    }
    etc

    Basically, I have a front end submission form where they can submit to six categories. If they include a FI, use that. If not, substitute with the DFI for the category. (Your code explains how to do the latter, but not sure if I need to add more code up front to use the user submitted image)

    Again, lot of helpful info for someone just finding their way around PHP of WordPress

    Plugin Author Jan-Willem

    (@janwoostendorp)

    Hi,

    I’m not sure what you are asking, sounds like you want confirmation about the order of things. So here goes;

    1 ) Set an DFI in the media settings
    2 ) When loading a post/page check if that post has an FI set,
    3 ) if not get DFI id and return that.
    4 ) Trigger optional filter(s) that checks for category in this case.
    5 ) Filter returns the original DFI or a category specific Image id.

    DFI should not trigger on saving. But I’ve had problems with frontend submissions before. Which plugin do you use for that?
    Do you have problems right now? Or are you just checking in advanced?

    Jan-Willem

    Thread Starter cristofayre

    (@cristofayre)

    Sorry for not explaining it too well. I have created a little infographic that might illustrate the point better. Had to use DropBox as there is no image upload on www.ads-software.com
    Featured image vs no featured image

    If the user adds a picture, the “default thumb” routine isn’t called, (or more correctly, does not run any code block ie “if ($post_id has_thumbnail){return}”) If the picture is missing, then it will use the default thumb associated with that category as specified by the extra exception plugin.

    (Or maybe I’m trying to do something it was never intended to do!)

    None of this is done via the admin panel. (I don’t think this will matter to your plugin, but just in case …) The submission form is displayed as a normal WP page. If the user adds a Feature Image, it is sent via AJAX to server and stored in the standard WP media folder, and assigned a code. Via the AJAX response, this code is then embedded in the form via a hidden input. When they finally submit the form, it is uploaded as a “draft” or “pending” post, and uses the hidden code to associate the image in the media folder as a Featured Image for that post.

    My theory is: When I set it to “published”, if no Featured Image is present, then the default thumbnail is substituted. =OR= when the page is displayed to an end user, if no FI is present on the post, the default thumb for that category is shown, (as per the “free” thumb in the infographic)

    Apologies if this is still all gobbledy-gook.

    • This reply was modified 3 years, 6 months ago by cristofayre.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Where to place modified code?’ is closed to new replies.