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.

Submit HTML code snippets

  1. So you want to create a form designed for posting an HTML code snippet to a Wordpress Blog. Go for it, first create a new form, and then add the necessary post input fields. The ‘Body’ input field is what you should use for the snippet, it can be found under the heading ‘Post Fields’. And then.

    Not quite that easy.

    The Gravity Forms Plugin sends the entry typed into the ‘Blog’ field directly into the Wordpress Blog as the content of a post. The PHP script uses the wp_insert_post() function to accomplish the task of sending all of the post data to Wordpress and its SQL Database. The process sends the character string as is, hence if you enter HTML <tags> into the form, the code snippet will actually render in the browser.

    For example, you want to display the snippet like this on your blog:

    <div class=”example”><p>This is my Snippet</p></div>

    It wont work. When you copy this into your form and then submit it, the blog post will look like this: This is my Snippet

    This is no good if you are trying to display the code snippets on your blog for people to copy for use in their web sites. So here is my solution:

    1.Open up forms_model.php file within the Gravity Forms plugins folder
    2.Copy the three lines code shown below and paste them on line 904 (line number may change on future versions of GF)
    3.The pasted code should be placed just before the line that posts to Wordpress that looks like this: $post_id = wp_insert_post($post_data); hopefully line 904

    $str = $post_data["post_content"];
    $escaped_html = htmlentities($str);
    $post_data["post_content"] = $escaped_html;

    That is it. Now your code will display in the browser with the tags so that it can be copied and uses in a functional html document. The script works by escaping the html type characters using PHP’s htmlentities(); function. Essensialy converting the greater than and less than symbols to > < for the HTML within Wordpress. Then Wordpress will display the tag properly on the post. You can check out the end result by toggling the HTML and Visual windows on the Wordpress post editor.

    I hope that helped you, if you have problems with the script you could probably add a conditional statement that checks for a certain category for the incoming post and only escapes the posts of a certain category. This would prevent the escape sequence from happening to every post.

    Posted 14 years ago on Saturday January 30, 2010 | Permalink
  2. I've found another solution for this which might work better.

    Install the plugin Syntaxhighlighter evolved:

    http://www.viper007bond.com/wordpress-plugins/syntaxhighlighter/

    Then put the following in the field description for your users:

    "If you want to embed code you can put it in brackets with the programming language of choice! Example: [php] my php code [/php]"

    Problem solved and it allows you users to put the code directly to clipboard and read it properly (syntax highlighting)

    Hope this helps :)

    Posted 14 years ago on Wednesday February 3, 2010 | Permalink