• Hi,

    I run an Article directory from wordpress and the registered users always go overboard when adding tags. I don’t display tags on the theme but use tags to determine what other posts are related to it.

    I want to be able to limit the number of tags added to a new post to 3 but I cant find a hack or plugin on Google to do it.

    Here is a js hack from a plugin that displays an alert if the user tries to select more than 1 category and it works great. Shouldnt it work the same for tags, if so, can someone edit this code for me and get it to work with tags also.

    <script type="text/javascript"><!--
    	      jQuery("form#post").submit(function(){
    	        var ln = jQuery("ul#categorychecklist input:checkbox:checked").length;
    	        if ( ln > 1 ) {
    	          alert("<?php _e('Attention! You can select only ONE category.', 'article-directory'); ?>");
    	          return false;
    	        } else {
    	          return true;
    	        }
    	      });
    	      jQuery('#categorychecklist label').click(function(){
    	        var ln = jQuery("ul#categorychecklist input:checkbox:checked").length;
    	        if ( ln > 1 ) {
    	          alert("<?php _e('Attention! You can select only ONE category.', 'article-directory'); ?>");
    	          return false;
    	        } else {
    	          return true;
    	        }
    	      });
    	    //--></script>

    Thanks to anybody that can help.

    Bob

Viewing 9 replies - 1 through 9 (of 9 total)
  • Maybe..

    <script type="text/javascript"><!--
    	jQuery("form#post").submit(function($){
    		var ln = $("ul#categorychecklist input:checkbox:checked").length;
    		var tag_ln = $('.tagchecklist span').length;
    		if ( ln > 1 ) {
    			alert("<?php _e('Attention! You can select only ONE category.', 'article-directory'); ?>");
    			return false;
    		}
    		if( tag_ln > 3 ) {
    			alert("<?php _e('Attention! Too many tags, please provide no more than three tags.', 'article-directory'); ?>");
    			return false;
    		}
    		return true;
    	});
    	jQuery('#categorychecklist label').click(function($){
    		var ln = $("ul#categorychecklist input:checkbox:checked").length;
    		if ( ln > 1 ) {
    			alert("<?php _e('Attention! You can select only ONE category.', 'article-directory'); ?>");
    			return false;
    		}
    		return true;
    	});
    //--></script>

    Does that help? ??

    Thread Starter Melbourne Cup Sweep

    (@hayzie)

    Hi Mark / t31os,

    Thank you so very much for taking the time to help me with my issue. I owe you big time. Sorry for late reply, my internet has been down.

    I tried your changes but it didn’t quite work, it actually stopped the category limiter from working also.

    Im not sure how much code I’m allowed to post to this forum otherwise I would post the whole file and that would probably make it make more sense.

    This may help a little more.

    if ($options['sel_only_one_cat'] == 1) {
    
    	class SelectOnlyOneCategory {
    	  function AddToEditPage() {
    		?>
    	    <script type="text/javascript"><!--
    	      jQuery("form#post").submit(function(){
    	        var ln = jQuery("ul#categorychecklist input:checkbox:checked").length;
    	        if ( ln > 1 ) {
    	          alert("<?php _e('Attention! You can select only ONE category.', 'article-directory'); ?>");
    	          return false;
    	        } else {
    	          return true;
    	        }
    	      });
    	      jQuery('#categorychecklist label').click(function(){
    	        var ln = jQuery("ul#categorychecklist input:checkbox:checked").length;
    	        if ( ln > 1 ) {
    	          alert("<?php _e('Attention! You can select only ONE category.', 'article-directory'); ?>");
    	          return false;
    	        } else {
    	          return true;
    	        }
    	      });
    	    //--></script>		<?php
    	  }
    	}
    	add_action("edit_form_advanced", array("SelectOnlyOneCategory", "AddToEditPage"));
    }
    
    if ($options['sel_only_child_cat'] == 1) {
    
    	function artdir_SelectOnlyChildCategory() {
    ?>
        <script type="text/javascript"><!--
    			var $j = jQuery.noConflict();
    			$j('#categorychecklist li:has(ul.children) > label').css({borderBottom: '1px dashed #666'});
    			$j('#categorychecklist .children').hide();
    			$j('#categorychecklist li:has(ul.children) > label').toggle(
    				function() {
    					$j(this).parent('li').find('ul.children').slideDown();
    					return false;
    				},
    				function() {
    					$j(this).parent('li').find('ul.children').slideUp();
    					return false;
    				}
    			);
        //--></script>

    Be fantastic if we could get this working as iv been searching for a solution for so long. I would owe you for life.

    Bob

    Hi,

    This works for me…

    <script type="text/javascript">
    /* <![CDATA[ */
    jQuery(document).ready( function( $wpjq ) {
    	$wpjq("form#post").submit( function() {
    		var ln = $wpjq("#categorychecklist input:checkbox:checked").length;
    		if ( ln > 1 )
    		{
    			// Uncheck the categories
    			$wpjq("ul#categorychecklist input:checkbox:checked").attr( 'checked',false );
    			// Hide the ajax loading image (gets fired when you hit the publish/update button)
    			$wpjq("#ajax-loading").hide();
    			// Remove the class that disables the publish/update button after it's clicked
    			$wpjq("#publish").removeClass('button-primary-disabled');
    			// Now fire off the alert
    			alert("<?php _e('Attention! You can select only ONE category.', 'article-directory'); ?>");
    			// And return false
    			return false;
    		}
    	});
    	$wpjq('#categorychecklist label').click( function() {
    		var ln = $wpjq("#categorychecklist input:checkbox:checked").length;
    		if ( ln > 1 )
    		{
    			alert("<?php _e('Attention! You can select only ONE category.', 'article-directory'); ?>");
    			return false;
    		}
    	});
    });
    /* ]]> */
    </script>

    Thread Starter Melbourne Cup Sweep

    (@hayzie)

    Hi Mark, thanks for your help.

    Is this code also meant to work on the “Post Tags”.

    Bob

    Meant to do both, but copied the first part and forgot about the second bit.. *oops*

    Did the category code work ok?, i did some moderate testing and it seemed to work just fine..

    I’ll have a look at the tag code for you when i’m back at my main PC… ??

    Thread Starter Melbourne Cup Sweep

    (@hayzie)

    Ok cool, thanks mate, thought I may have been missing something. Fantastic that its actually possible, the category code works great.

    Your a champion and I owe you one.

    I’ve simply used the tag code from my previous response, working it into the code i posted above..

    <script type="text/javascript">
    /* <![CDATA[ */
    jQuery(document).ready( function( $wpjq ) {
    	$wpjq("form#post").submit( function() {
    		var ln = $wpjq("#categorychecklist input:checkbox:checked").length;
    		var tag_ln = $wpjq('.tagchecklist span').length;
    		if ( ln > 1 )
    		{
    			// Uncheck the categories
    			$wpjq("ul#categorychecklist input:checkbox:checked").attr( 'checked',false );
    
    			// Hide the ajax loading image (gets fired when you hit the publish/update button)
    			$wpjq("#ajax-loading").hide();
    
    			// Remove the class that disables the publish/update button after it's clicked
    			$wpjq("#publish").removeClass('button-primary-disabled');
    
    			// Now fire off the alert
    			alert("<?php _e('Attention! You can select only ONE category.', 'article-directory'); ?>");
    
    			// And return false
    			return false;
    		}
    		if( tag_ln > 3 )
    		{
    			// Hide the ajax loading image (gets fired when you hit the publish/update button)
    			$wpjq("#ajax-loading").hide();
    
    			// Remove the class that disables the publish/update button after it's clicked
    			$wpjq("#publish").removeClass('button-primary-disabled');
    
    			// Now fire off the alert
    			alert("<?php _e('Attention! Too many tags, please provide no more than three tags.', 'article-directory'); ?>");
    
    			// And return false
    			return false;
    		}
    	});
    	$wpjq('#categorychecklist label').click( function() {
    		var ln = $wpjq("#categorychecklist input:checkbox:checked").length;
    		if ( ln > 1 )
    		{
    			alert("<?php _e('Attention! You can select only ONE category.', 'article-directory'); ?>");
    			return false;
    		}
    	});
    
    });
    /* ]]> */
    </script>

    ??

    Thread Starter Melbourne Cup Sweep

    (@hayzie)

    Mark,

    Wow, you are a magician mate. Thanks so much for the fix. Works fantastic.

    I owe you big time.

    I think this would make a great little plugin by itself now that wordpress mu has been integrated into wordpress 3.0 and there are many wordpress sites accepting users to post content.

    As I have no idea, was it very hard to word out and add the extra code?

    Thanks again mate

    Bob

    Easy peasy, but easy is relative to the person.. i like jQuery because it’s pretty easy for anyone familiar with PHP and/or CSS, much nicer/easier to write then regular old Javascript.. (although it is essentially Javascript)

    Happy i could help Bob.. ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Limit the number of Tags added to a Post’ is closed to new replies.