PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

tags field character limiter,

  1. Hi all we are currently using this code that will limit the title text to 100 characters, We would like to extend this defined function to the "tags id 8", "email id 11", "companies name id 17", "contact name id 18", "web address id 19", field as well. We have people using 100's of tags that rally ugly's up our results page as you can see on some of the older posts on our new site http://www.adszoom.com Check out the post page preview after the form is rendered as well. We do not want the post's info to over flow the theme so this is a must. You may have to just log in and do a test post to see what i mean. The content is protected so i can't supply the 2nd preview for you to see....

    <?php
    add_filter('gform_validation_2', 'title_length');
    function title_length($validation_result){
    	$form = $validation_result["form"];
    
        //supposing we don't want input_1 to be longer than 100 characters.
        if(strlen($_POST['input_1']) > 100){
    
            // set the form validation to false
            $validation_result["is_valid"] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach($form["fields"] as &$field){
    
                //NOTE: replace 1 with the field you would like to validate
                if($field["id"] == "1"){
                    $field["failed_validation"] = true;
                    $field["validation_message"] = "Your title is too long 100 characters Google max";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    ?>
    Posted 11 years ago on Monday July 23, 2012 | Permalink
  2. I'm not sure I understand the question. You would like to apply this same functionality (100 characters max) to those other fields you listed, in addition to the post title, and you would like help with doing that?

    Instead of that, how about hiding the overflow for those various fields, in the post, with:

    [css]
    overflow: hidden;

    After setting a width on the element? It would not return an error but it would prevent the corruption of your site's layout.

    Please clarify what you would like help with and what you have tried so far.

    Posted 11 years ago on Monday July 23, 2012 | Permalink
  3. Hay Chris thanks for the reply ya that dose solve the issue on the preview page we made but take a look at this http://www.adszoom.com/category/art/ see the tags... out of control! so i figured if i can stop it at the time of the post like i did the title, that by the way had the same issue with people putting in 1000's of words... ugly / just bad . If i can do the same we are good to go. This then will reflect what we need it to, on the preview instead of hiding the extra.. but grate idea.

    by the way i have bin going round and round on this other issue that you looked at,, any ideas ?
    http://www.gravityhelp.com/forums/topic/changing-the-background-color-of-a-post

    Posted 11 years ago on Monday July 23, 2012 | Permalink
  4. what i did so far

    I have tried to copy and past the code in the functions. php under the current snip above replacing the title fields,,, no good. also wp codax but most of the solutions only work if the poster uses the default wp admin to post

    Posted 11 years ago on Monday July 23, 2012 | Permalink
  5. so to clarify i just need the other fields limited like the title.

    Posted 11 years ago on Monday July 23, 2012 | Permalink
  6. I have tried to copy and past the code in the functions. php under the current snip above replacing the title fields,,, no good.

    What do you mean, "no good"? What was the result? Was there a syntax error about not being able to redeclare the function name or something?

    Posted 11 years ago on Tuesday July 24, 2012 | Permalink
  7. Hi

    Sorry Chris ya the hole site went down no syntax errors

    Posted 11 years ago on Tuesday July 24, 2012 | Permalink
  8. the page if you refreshed had an error on the 1st line of the duplicated script
    Fatal error: Cannot redeclare title_length() (previously declared in /home/adszoom/public_html/wp-content/themes/adszoom/functions.php:68) in /home/adszoom/public_html/wp-content/themes/adszoom/functions.php on line 124

    <?php
    add_filter('gform_validation_2', 'title_length');
    function title_length($validation_result){
    	$form = $validation_result["form"];
    
        //supposing we don't want input_1 to be longer than 100 characters.
        if(strlen($_POST['input_8']) > 100){
    
            // set the form validation to false
            $validation_result["is_valid"] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach($form["fields"] as &$field){
    
                //NOTE: replace 1 with the field you would like to validate
                if($field["id"] == "8"){
                    $field["failed_validation"] = true;
                    $field["validation_message"] = "test for tages ";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    ?>
    Posted 11 years ago on Tuesday July 24, 2012 | Permalink
  9. David Peralty

    What it is telling you is that you already have a function called title_length, and can't "change" it. You can either add code to your original function to detect other fields, so doing some more if statements to test for various fields or rename the function so you don't have two attempts to run the same function to do different things.

    Posted 11 years ago on Tuesday July 24, 2012 | Permalink
  10. Yes, what David said. That fatal error is because you can't declare a function more than one time. The function needs to be renamed:

    [php]
    function title_length

    When you copy the function code, rename the function. You're using this a lot, so you are going to have to rename it multiple times:

    tags_length
    email_length
    companies_length
    contact_name_length
    web_address_length

    You need to change the function line, and also in the add_filter line where the function is called.

    HOWEVER, this is a hugely inefficient way to do things. Use the same function (title_length), but maybe rename it to something like "input_length_limiter", then in the function, process all the fields, and return the form one time. It will be easier for maintenance going forward and will run substantially faster.

    Posted 11 years ago on Tuesday July 24, 2012 | Permalink
  11. <?php
    add_filter('gform_validation_2', 'title_length');
    function title_length($validation_result){
    	$form = $validation_result["form"];
    
        //supposing we don't want input_1 to be longer than 100 characters.
        if(strlen($_POST['input_1']) > 100){
    
            // set the form validation to false
            $validation_result["is_valid"] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach($form["fields"] as &$field){
    
                //NOTE: replace 1 with the field you would like to validate
                if($field["id"] == "1"){
                    $field["failed_validation"] = true;
                    $field["validation_message"] = "Your title is too long 100 characters Google max";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    ?>
    <?php
    add_filter('gform_validation_2', 'tags_length');
    function tags_length($validation_result){
    	$form = $validation_result["form"];
    
        //supposing we don't want input_1 to be longer than 100 characters.
        if(strlen($_POST['input_8']) > 100){
    
            // set the form validation to false
            $validation_result["is_valid"] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach($form["fields"] as &$field){
    
                //NOTE: replace 1 with the field you would like to validate
                if($field["id"] == "8"){
                    $field["failed_validation"] = true;
                    $field["validation_message"] = "<strong>100 words and spaces.</strong> Please only use a handful of pinpointed keywords. You do not need many, just pick the ones that are pinpoint. For example: If your title is ( <em>Tile floor cleaning</em> ) do not used keywords like ( <em>carpet cleaning</em> ) You will <strong>confuse the search engines.</strong> Post separate ads for each service or item your offering. See ( <em>Things to know before you post</em> ) here to the right in yellow for more info.";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    ?>
    <?php
    add_filter('gform_validation_2', 'email_length');
    function email_length($validation_result){
    	$form = $validation_result["form"];
    
        //supposing we don't want input_1 to be longer than 100 characters.
        if(strlen($_POST['input_11']) > 50){
    
            // set the form validation to false
            $validation_result["is_valid"] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach($form["fields"] as &$field){
    
                //NOTE: replace 1 with the field you would like to validate
                if($field["id"] == "11"){
                    $field["failed_validation"] = true;
                    $field["validation_message"] = "<strong>50 word and spaces max.</strong>.";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    ?>
    <?php
    add_filter('gform_validation_2', 'companies_length');
    function companies_length($validation_result){
    	$form = $validation_result["form"];
    
        //supposing we don't want input_1 to be longer than 100 characters.
        if(strlen($_POST['input_17']) > 50){
    
            // set the form validation to false
            $validation_result["is_valid"] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach($form["fields"] as &$field){
    
                //NOTE: replace 1 with the field you would like to validate
                if($field["id"] == "17"){
                    $field["failed_validation"] = true;
                    $field["validation_message"] = "<strong>50 word and spaces max.</strong>.";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    ?>
    <?php
    add_filter('gform_validation_2', 'contact_name_length');
    function contact_name_length($validation_result){
    	$form = $validation_result["form"];
    
        //supposing we don't want input_1 to be longer than 100 characters.
        if(strlen($_POST['input_18']) > 50){
    
            // set the form validation to false
            $validation_result["is_valid"] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach($form["fields"] as &$field){
    
                //NOTE: replace 1 with the field you would like to validate
                if($field["id"] == "18"){
                    $field["failed_validation"] = true;
                    $field["validation_message"] = "<strong>50 word and spaces max.</strong>.";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    ?>
    <?php
    add_filter('gform_validation_2', 'web_address_length');
    function web_address_length($validation_result){
    	$form = $validation_result["form"];
    
        //supposing we don't want input_1 to be longer than 100 characters.
        if(strlen($_POST['input_19']) > 50){
    
            // set the form validation to false
            $validation_result["is_valid"] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach($form["fields"] as &$field){
    
                //NOTE: replace 1 with the field you would like to validate
                if($field["id"] == "19"){
                    $field["failed_validation"] = true;
                    $field["validation_message"] = "<strong>50 word and spaces max.</strong>.";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    ?>

    Thanks dave and Chris! again

    Posted 11 years ago on Wednesday July 25, 2012 | Permalink