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.

limiting the title of a post characters

  1. Hi

    So i tried to limit the # of characters in the post title using the wp functions and they did not work because all posts come from G-F input field 1 on my form so i tried

    <?php
    add_filter('gform_validation', 'custom_validation');
    function custom_validation($validation_result){
    
        //supposing we don't want input 1 to be a value of 86
        if($_POST['input_1'] == 86){
    
            // set the form validation to false
            $validation_result["is_valid"] = false;
    
            //finding Field with ID of 1 and marking it as failed validation
            foreach($validation_result["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"] = "This field is invalid!";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    
    ?>

    for form validation in my title field ( 1 ) of my form but it then will not move on if you click " continue "
    it just keeps thinking.............
    can someone help me with this? in a perfect world i would want say 100 characters + i have added the city state zip to the end of the title. I can take that out if need be.... Help!

    Posted 11 years ago on Wednesday June 13, 2012 | Permalink
  2. David Peralty

    You don't test for length anywhere. You need to look at PHP code related to testing a string for its length. Have a look at the following:
    http://php.net/manual/en/function.strlen.php

    Posted 11 years ago on Thursday June 14, 2012 | Permalink
  3. Hi

    Thanks for the reply Dave.. Ya we looked into this and have solved the issue if your posting threw the word press default admin but this solution dose not work if we try it in the gf post, so we are looking for a solution like the one i posted above. Will the solution above work with a tweak or two?
    it makes sense to limit the title on the form to me but for some reason the form gets stuck and will not go further, do you know why ? here is the code Dian posted that we use for the preview / next page that the form will not move on too.

    <?php
    add_filter("gform_pre_render_4", "populate_html");
    function populate_html($form)
    {
    	//this is a 2-page form with the data from page one being displayed in an html field on page 2
    	$current_page = GFFormDisplay::get_current_page($form["id"]);
    	$html_content = "<strong>The Following Will Be Submitted To Google, Yahoo & Bing. Please Make Sure All Information Is Correct If You See An Error, Please Click Previous At The Bottom Of Page.<br><br><br><br>We Will Index This Ad ASAP Getting This Ad Viewed By Millions. Please Make Sure The Keywords Are Correct (Watch Video) <br> We Guarantee Page 1 Of All 3 Top Search Engines For Your Ad If You Follow The Easy Instructions. ( Videos )<br><br>Note: The Images Are Actual Size But Will Be Resized Automatically For Your Convenance.</strong><br/><ul>";
    	if ($current_page == 2)
    	{
    		foreach($form["fields"] as &$field)
    		{
    			//gather form data to save into html field (id 3 on my form), exclude page break
    			if ($field["id"] != 15 && $field["type"] != "page")
    			{
    				//see if this is a complex field (will have inputs)
    				if(rgar($field, "inputs"))
    				{
    					//this is a complex fieldset (name, adress, etc.) - get individual field info
    					foreach($field["inputs"] as $input){
    						//get name of individual field, replace period with underscore when pulling from post
    						$input_name = "input_" . str_replace(".","_",$input["id"]);
    						$value = rgpost($input_name);
    						$html_content .= "<li>" . $input["label"] . ": " . $value . "</li>";
    					}
    				}
    				else
    				{
    					//this can be changed to be a switch statement if you need to handle each field type differently
    					//get the filename of file uploaded or post image uploaded
    					if ($field["type"] == "fileupload" || $field["type"] == "post_image")
    					{
    						$input_name = "input_" . $field["id"];
    						//before final submission, the image is stored in a temporary directory
    						//if displaying image in the html, point the img tag to the temporary location
    						$temp_filename = RGFormsModel::get_temp_filename($form["id"], $input_name);
    						$uploaded_name = $temp_filename["uploaded_filename"];
    						$temp_location = RGFormsModel::get_upload_url($form["id"]) . "/tmp/" . $temp_filename["temp_filename"];
    						if (!empty($uploaded_name)) {
    							$html_content .= "<li>" . $field["label"] . ": " . $uploaded_name . "<img src='" . $temp_location . "' height='150' width='150'></img></li>";
    						}
    					}
    					else
    					{
    						//get the label and then get the posted data for the field (this works for simple fields only - not the field groups like name and address)
    						$html_content .= "<li>" . $field["label"] . ": " . rgpost('input_' . $field['id']) . "</li>";
    					}
    				}
    			}
    		}
    		$html_content .= "</ul>";
    		//loop back through form fields to get html field (id 3 on my form) that we are populating with the data gathered above
    		foreach($form["fields"] as &$field)
    		{
    			//get html field
    			if ($field["id"] == 15)
    			{
    				//set the field content to the html
    				$field["content"] = $html_content;
    			}
    		}
    	}
    	//return altered form so changes are displayed
    	return $form;
    }
    ?>

    could this be some kind of conflict ?? just dono what do you think?

    Posted 11 years ago on Thursday June 14, 2012 | Permalink
  4. ya just tried with out the preview code and it dose the same,

    Posted 11 years ago on Thursday June 14, 2012 | Permalink
  5. this is the solution we used for the default wp posting in admin and it works fine

    <?php if (strlen($post->post_title) > 40) {
    echo substr(the_title($before = '', $after = '', FALSE), 0, 40) . '...'; } else {
    the_title();
    } ?>
    Posted 11 years ago on Thursday June 14, 2012 | Permalink
  6. David Peralty

    Using the validation hook, you just need to test the length of the field versus the maximum length you want it to have, and then what Gravity Forms should return if it exceeds this value.

    <?php
    add_filter('gform_validation', 'title_length');
    function title_length($validation_result){
    
        //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($validation_result["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";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    ?>
    Posted 11 years ago on Thursday June 14, 2012 | Permalink
  7. ok will this test the maximum length above? or do i need to ad something to it? if so where?

    Posted 11 years ago on Thursday June 14, 2012 | Permalink
  8. David Peralty

    You'll need to replace out the right form elements where the POST detail is and the field ID, but otherwise, it should be good to go. It will only test on submit. You'll have to find some JavaScript if you want it to test live while they are typing.

    Like I said though, all the example is missing is a string length call.

    Posted 11 years ago on Thursday June 14, 2012 | Permalink
  9. so if your saying to only copy this snip into functions.php and replace the input_1 to the correct input field
    which is the title, on my form it is input 1 as well, then this snipit should test length and work fine? if so i just tested and it dose not. please check out http://nichedeliverysystem.com/post-new-ad/ and you will see what it dose..

    i did not do anything else but make shore the field is correct did i miss something?

    Posted 11 years ago on Thursday June 14, 2012 | Permalink
  10. David Peralty

    Can you send me a WordPress login and password to peralty@rocketgenius.com so I can take a look? Please include a link to this thread in your e-mail so I know which issue it is pertaining to.

    Posted 11 years ago on Thursday June 14, 2012 | Permalink
  11. ok its done thanks

    Posted 11 years ago on Thursday June 14, 2012 | Permalink
  12. any luck? worked on it all day and cant get it to move on to page 2 of form.

    Posted 11 years ago on Friday June 15, 2012 | Permalink
  13. Hay dave question have you made any headway with the form not moving forward page 2 and question, could this be a problem with the header.php file?
    here is the code for header.php

    <?php
    /**
     * The Header for our theme.
     *
     * Displays all of the <head> section and everything up till <div id="main">
     *
     * @package WordPress
     * @subpackage Twenty_Eleven
     * @since Twenty Eleven 1.0
     */
    ?><!DOCTYPE html>
    <!--[if IE 6]>
    <html id="ie6" <?php language_attributes(); ?>>
    <![endif]-->
    <!--[if IE 7]>
    <html id="ie7" <?php language_attributes(); ?>>
    <![endif]-->
    <!--[if IE 8]>
    <html id="ie8" <?php language_attributes(); ?>>
    <![endif]-->
    <!--[if !(IE 6) | !(IE 7) | !(IE 8)  ]><!-->
    <html <?php language_attributes(); ?>>
    <!--<![endif]-->
    <head>
    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    <meta name="viewport" content="width=device-width" />
    <title><?php
    	/*
    	 * Print the <title> tag based on what is being viewed.
    	 */
    	global $page, $paged;
    
    	wp_title( '|', true, 'right' );
    
    	// Add the blog name.
    	bloginfo( 'name' );
    
    	// Add the blog description for the home/front page.
    	$site_description = get_bloginfo( 'description', 'display' );
    	if ( $site_description && ( is_home() || is_front_page() ) )
    		echo " | $site_description";
    
    	// Add a page number if necessary:
    	if ( $paged >= 2 || $page >= 2 )
    		echo ' | ' . sprintf( __( 'Page %s', 'twentyeleven' ), max( $paged, $page ) );
    
    	?></title>
    <link rel="profile" href="http://gmpg.org/xfn/11" />
    <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
    <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
    <!--[if lt IE 9]>
    <script src="<?php echo get_template_directory_uri(); ?>/js/html5.js" type="text/javascript"></script>
    <![endif]-->
    <?php
    	/* We add some JavaScript to pages with the comment form
    	 * to support sites with threaded comments (when in use).
    	 */
    	if ( is_singular() && get_option( 'thread_comments' ) )
    		wp_enqueue_script( 'comment-reply' );
    
    	/* Always have wp_head() just before the closing </head>
    	 * tag of your theme, or you will break many plugins, which
    	 * generally use this hook to add elements to <head> such
    	 * as styles, scripts, and meta tags.
    	 */
    	wp_head();
    ?>
    </head>
    
    <body <?php body_class(); ?>>
    <div id="page" class="hfeed">
    <div>
    <p class="classifieds1">Classifieds 2.0, Inject Your Ad!</p>
    </div>
    <br>
    
    <div class="menu">
    <a href="">Home</a>
    <a href="http://nichedeliverysystem.com/post-new-ad/">Post new ad</a>
    <a href="">Register</a>
    <a href="">Help / Faq</a>
    <a href="">View / edit ads</a>
    <a href="">2.0 = Google</a>
    </div>
    <div id="main">
    
    <br>
    
    </header> 
    
    				<h3 class="assistive-text"><?php _e( 'Main menu', 'twentyeleven' ); ?></h3>
    				<?php /*  Allow screen readers / text browsers to skip the navigation menu and get right to the good stuff. */ ?>
    				<div class="skip-link"><a class="assistive-text" href="#content" title="<?php esc_attr_e( 'Skip to primary content', 'twentyeleven' ); ?>"><?php _e( 'Skip to primary content', 'twentyeleven' ); ?></a></div>
      <div class="skip-link"><a class="assistive-text" href="#secondary" title="<?php esc_attr_e( 'Skip to secondary content', 'twentyeleven' ); ?>"><?php _e( 'Skip to secondary content', 'twentyeleven' ); ?></a></div>
    			<?php /* Our navigation menu.  If one isn't filled out, wp_nav_menu falls back to wp_page_menu. The menu assiged to the primary position is the one used. If none is assigned, the menu with the lowest ID is used. */ ?>
    		</nav>
    		<!-- #access -->
    </header><!-- #branding -->

    or the page that the form sits here is the code from the template

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <?php /* Template Name: loaded preview 1
    */ ?>
    <?php get_header(); ?>
    <title>Classifieds With 2.0 Platform</title>
    </head>
    
    <div id="homebox">
    <body>
    <p class="perview20tagline1">
    <strong>Ok last step!</strong> choose your ads visibility before your ad is published and completed.</p>
    <p class="discription">
    Classified ads 2.0 Unlimited Ads Package Features For Your Ads.</p>
    <div class="gybpreview">
    <!--======================== BEGIN COPYING THE HTML HERE ==========================-->
    <img name="previewrealadsgoogle" src="/wp-content/themes/adszoom/images/preview-real-ads-google.gif" width="863" height="392" border="0" alt="" />
    <!--========================= STOP COPYING THE HTML HERE =========================-->
    </div>
    <div class="fulladsbox">
    <div class="adstitle">
    <?php
    $title = $_GET['title'];
    echo $title;
    ?>
    </div>
    <br />
    <?php
    $category = $_GET["category"];
    echo $category;
    ?>
    <br />
    <?php
    $phone = $_GET["phone"];
    echo $phone;
    ?>
    <br />
    
    </div>
    </body>
    
    </html>
    
    <?php get_footer(); ?>
    Posted 11 years ago on Friday June 15, 2012 | Permalink
  14. David Peralty

    I've e-mailed you twice today without response. As I said in my e-mail, the Twenty-Eleven theme works fine, so it is either your theme, and I can't say that anything stands out here, or the code in your functions.php file. Can you back up your functions.php file, strip out your custom code, test your form, and if it works, we can go from there. If it doesn't work, we know it is the theme, and can work on troubleshooting that.

    Posted 11 years ago on Friday June 15, 2012 | Permalink
  15. Hi

    So sorry i did not see the mail anyway i took out all of the code from the child them function.php. The only code there is the gf preview and the snip from you above. I then tested the form and the form works with no preview and no title limits.
    I then activated the 2011 theme went to functions.php and pasted in your snip tested and had the same issue, i could not get the 2nd page to load at all using the code in 2011.

    I then un installed 2011 thinking somehow its corrupt ?? and reinstalled a fresh copy repeated the steps and had the same affect.

    Posted 11 years ago on Friday June 15, 2012 | Permalink
  16. i also deactivated all plug ins and tested with same issue

    Posted 11 years ago on Friday June 15, 2012 | Permalink
  17. question this form was imported from the current live site. Maybe this is the issue? i dont wanna change anything incase your working on it,

    Posted 11 years ago on Friday June 15, 2012 | Permalink
  18. David Peralty

    Hi Scott, I have the development team helping me with this now. We will let you know asap what is going on.

    Posted 11 years ago on Friday June 15, 2012 | Permalink
  19. cool here is the code from the child theme functions.php just in case

    <?php
    add_filter("gform_pre_render_4", "populate_html");
    function populate_html($form)
    {
    	//this is a 2-page form with the data from page one being displayed in an html field on page 2
    	$current_page = GFFormDisplay::get_current_page($form["id"]);
    	$html_content = "<strong>The Following Will Be Submitted To Google, Yahoo & Bing. Please Make Sure All Information Is Correct If You See An Error, Please Click Previous At The Bottom Of Page.<br><br><br><br>We Will Index This Ad ASAP Getting This Ad Viewed By Millions. Please Make Sure The Keywords Are Correct (Watch Video) <br> We Guarantee Page 1 Of All 3 Top Search Engines For Your Ad If You Follow The Easy Instructions. ( Videos )<br><br>Note: The Images Are Actual Size But Will Be Resized Automatically For Your Convenance.</strong><br/><ul>";
    	if ($current_page == 2)
    	{
    		foreach($form["fields"] as &$field)
    		{
    			//gather form data to save into html field (id 3 on my form), exclude page break
    			if ($field["id"] != 15 && $field["type"] != "page")
    			{
    				//see if this is a complex field (will have inputs)
    				if(rgar($field, "inputs"))
    				{
    					//this is a complex fieldset (name, adress, etc.) - get individual field info
    					foreach($field["inputs"] as $input){
    						//get name of individual field, replace period with underscore when pulling from post
    						$input_name = "input_" . str_replace(".","_",$input["id"]);
    						$value = rgpost($input_name);
    						$html_content .= "<li>" . $input["label"] . ": " . $value . "</li>";
    					}
    				}
    				else
    				{
    					//this can be changed to be a switch statement if you need to handle each field type differently
    					//get the filename of file uploaded or post image uploaded
    					if ($field["type"] == "fileupload" || $field["type"] == "post_image")
    					{
    						$input_name = "input_" . $field["id"];
    						//before final submission, the image is stored in a temporary directory
    						//if displaying image in the html, point the img tag to the temporary location
    						$temp_filename = RGFormsModel::get_temp_filename($form["id"], $input_name);
    						$uploaded_name = $temp_filename["uploaded_filename"];
    						$temp_location = RGFormsModel::get_upload_url($form["id"]) . "/tmp/" . $temp_filename["temp_filename"];
    						if (!empty($uploaded_name)) {
    							$html_content .= "<li>" . $field["label"] . ": " . $uploaded_name . "<img src='" . $temp_location . "' height='150' width='150'></img></li>";
    						}
    					}
    					else
    					{
    						//get the label and then get the posted data for the field (this works for simple fields only - not the field groups like name and address)
    						$html_content .= "<li>" . $field["label"] . ": " . rgpost('input_' . $field['id']) . "</li>";
    					}
    				}
    			}
    		}
    		$html_content .= "</ul>";
    		//loop back through form fields to get html field (id 3 on my form) that we are populating with the data gathered above
    		foreach($form["fields"] as &$field)
    		{
    			//get html field
    			if ($field["id"] == 15)
    			{
    				//set the field content to the html
    				$field["content"] = $html_content;
    			}
    		}
    	}
    	//return altered form so changes are displayed
    	return $form;
    }
    ?>
    <?php
    add_filter('gform_validation', 'title_length');
    function title_length($validation_result){
    
        //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($validation_result["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";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    ?>
    Posted 11 years ago on Friday June 15, 2012 | Permalink
  20. ok thanks my email is acting up so lets just use this tread

    Posted 11 years ago on Friday June 15, 2012 | Permalink
  21. David Peralty

    Change the validation like the following:

    add_filter('gform_validation_3', '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";
                    break;
                }
            }
    
        }
    
        //Assign modified $form object back to the validation result
        $validation_result["form"] = $form;
        return $validation_result;
    
    }
    ?>
    Posted 11 years ago on Friday June 15, 2012 | Permalink
  22. yes!!!!!!!!!!!!!!!!!!! it works thank god! is it posible thou to make the title brake say after 50 characters ?
    ( using 100 characters total )
    if someone dose this wrbgwrbtwrtbwrtbwrtbwrtbwrtbwrbtwrbtwrbtwrtbwrtb or word-word-word- ecs it extends threw the side of the theme but if there was a brake in there there would not be an issue.. it did brake before the validation i believe.

    ps what was the fix for the 1 st issue so i know?

    Posted 11 years ago on Friday June 15, 2012 | Permalink
  23. By The way Dave you the man!!!! ;)

    Posted 11 years ago on Friday June 15, 2012 | Permalink
  24. David Peralty

    So the issue was that the way we were handling arrays in the example was causing issues with the latest versions of Gravity Forms, but we've updated our documentation. :) You could force breaks in titles, but that wouldn't be a validation thing, also, it would require some more complex PHP code.

    And the validation doesn't change the data, so it probably wasn't working before either. WordPress determines where to break things up if people do extended words like that, not Gravity Forms, but again, you could create a PHP script that added a space after 50 characters under certain circumstances, or make it have a line break, but that's beyond what I could do here for Gravity Forms support.

    Posted 11 years ago on Friday June 15, 2012 | Permalink

This topic has been resolved and has been closed to new replies.