WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Custom Field Template] BUG multi to single checkbox selection causes duplicates (2 posts)

  1. FlashUK
    Member
    Posted 4 years ago #

    Dear author,

    I discovered a bug while testing your plugin for a website.

    When you create a number of checkboxes, for instance:
    [Fruits]
    type = checkbox
    value = Apple # Orange # Banana
    default =

    If I tick all 3 in a post and save, all 3 will be saved.
    If you then only tick "Apple" and untick the rest, you will get 3 duplicate entries that all say "Apple".

    I managed to track down the code and fixed it. I changed the following:

    custom-field-template.php
    line: 2107

    if ( count($values) == 1 ) :
    				if ( !add_post_meta( $id, $title, apply_filters('cft_'.urlencode($title), $values[0]), true ) ) :
    					update_post_meta( $id, $title, apply_filters('cft_'.urlencode($title), $values[0]) );
    				endif;
    			elseif ( count($values) > 1 ) :
    				$tmp = get_post_meta( $id, $title, false );
    				if ( count($tmp)>0 ) :
    					if ( $values != $tmp ) :
    						delete_post_meta($id, $title);
    						foreach($values as $val)
    							add_post_meta( $id, $title, apply_filters('cft_'.urlencode($title), $val) );
    					endif;
    				else :
    					foreach($values as $val)
    						add_post_meta( $id, $title, apply_filters('cft_'.urlencode($title), $val) );
    				endif;
    			else :
    				delete_post_meta($id, $title);
    			endif;
    		}

    The fix I came up with was this:

    if ( count($values) >= 1 ) :
    				$tmp = get_post_meta( $id, $title, false );
    				if ( count($tmp)>0 ) :
    					if ( $values != $tmp ) :
    						delete_post_meta($id, $title);
    						foreach($values as $val)
    							add_post_meta( $id, $title, apply_filters('cft_'.urlencode($title), $val) );
    					endif;
    				else :
    					foreach($values as $val)
    						add_post_meta( $id, $title, apply_filters('cft_'.urlencode($title), $val) );
    				endif;
    			else :
    				delete_post_meta($id, $title);
    			endif;
    		}

    You may want to revise the code since I did this in about 15 mins but I only changed the conditional statements.

    I hope this helps with your plugin. Extremely useful plugin indeed. Thank you.

  2. Hiroaki Miyashita
    Member
    Posted 4 years ago #

    Thank you for letting me know the bug. I confirmed it also happened to my system. I will fix it in the next release near future. Thanks a lot!

Topic Closed

This topic has been closed to new replies.

About this Topic