Title: Programmatically adding raw HTML
Last modified: February 6, 2023

---

# Programmatically adding raw HTML

 *  [thisusernameisnolongeravailable](https://wordpress.org/support/users/thisusernameisnolongeravailable/)
 * (@thisusernameisnolongeravailable)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/programmatically-adding-raw-html/)
 * Is there a programmatic way to add HTML to a post (or create a new post) and 
   have the HTML left as is? (no converting new lines into <br> or <p>, no wrapping
   <img> in <figure>, etc.)
 * I have been trying with **wp_insert_post** but no matter what I try extra cruft
   gets added in. even inserting directly into the DB has not helped (nor would 
   this be a good final solution). As soon as I open it up in the wordpress editor,
   modifications can be seen.
 * I’ve tried many things, but this is the base case of what I am trying to do while
   having wordpress leave my html as is.
 *     ```wp-block-code
       function createPage($title, $src)
       {
           echo "Creating $title";
           $content = file_get_contents(__DIR__ . '/vanilla/' . $src); // just loading the html from another file. html is safe/clean
           $content = updateMediaUrls($content); // updating media (images etc) urls to proper path. quite irrelevant.
   
           $array = array(
               'post_tile' => $title,
               'post_content' => $content,
               'post_status' => 'publish',
               'post_type' => 'page',
               'post_author' => 1,
               'comment_status' => 'closed',
           );
           $page = wp_insert_post($array);
           return $page;
   
       }
       ```
   
    -  This topic was modified 3 years, 4 months ago by [thisusernameisnolongeravailable](https://wordpress.org/support/users/thisusernameisnolongeravailable/).
    -  This topic was modified 3 years, 4 months ago by [thisusernameisnolongeravailable](https://wordpress.org/support/users/thisusernameisnolongeravailable/).

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/programmatically-adding-raw-html/#post-16445394)
 * As you’ve observed, saving works well enough. It’s viewing it in the editor or
   on the front end where things get altered. When either of those things happen,
   content gets filtered through “the_content” and possibly other filters, depending
   on the situation.
 * It is in theory possible to remove offending filter callbacks like wpautop and
   wptexturize, but doing so tends to have a negative impact on existing content
   that requires these to present properly.
 * If you require adding unadulterated HTML, it’s best accomplished by keeping it
   all in a custom field where the problematic filters are not applied. The custom
   field could be output where desired by way of a shortcode, which is processed
   after the problematic filters are applied, thus avoiding any undesired changes.
 * Instead of a custom field, it is possible to contain shortcode content between
   shortcode tags. For example: [my-shortcode]my raw HTML I don’t want processed[/
   my-shortcode]
    I’m not so sure the WP filters will ignore such content. You could
   set up a test to verify, but IMO you’re better off keeping the content in a custom
   field.
 *  [pichichi](https://wordpress.org/support/users/pichichi/)
 * (@pichichi)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/programmatically-adding-raw-html/#post-16445399)
 * Hi, see here:
 *  [https://developer.wordpress.org/reference/functions/wpautop/](https://developer.wordpress.org/reference/functions/wpautop/)
 * If this is the issue you can turn this off for content and excerpt output with
   the folowing filter:
 *     ```wp-block-code
       remove_filter( 'the_content', 'wpautop' );
       remove_filter( 'the_excerpt', 'wpautop' );
       ```
   
 * Edit:
 * I did not see the answer above. Please take that as a more accurate answer, especially
   the points about affecting existing content using this method.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/programmatically-adding-raw-html/#post-16448949)
 * [@pichichi](https://wordpress.org/support/users/pichichi/) — Thanks for helping
   to answer this topic! In case you were not aware, the forums here do have a tool
   to you help you [avoid overlapping replies](https://make.wordpress.org/support/handbook/appendix/helpful-tools/)
   like what happened here. It will show you other members who are looking at the
   same topic as you are. It also notes when someone is actively typing a response.
 * It’s not a perfect tool, but it does help. Some don’t like others to know what
   they are looking at and intentionally avoid enabling this tool. That is fine 
   as well. No pressure, just FYI.
 *  [pichichi](https://wordpress.org/support/users/pichichi/)
 * (@pichichi)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/programmatically-adding-raw-html/#post-16449108)
 * It never even occured to me there would be a_ *x-is-typing -reply* _behind that“
   Also Viewing” link. Thanks for the tip [@bcworkz](https://wordpress.org/support/users/bcworkz/)!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Programmatically adding raw HTML’ is closed to new replies.

## Tags

 * [html](https://wordpress.org/support/topic-tag/html/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 3 participants
 * Last reply from: [pichichi](https://wordpress.org/support/users/pichichi/)
 * Last activity: [3 years, 4 months ago](https://wordpress.org/support/topic/programmatically-adding-raw-html/#post-16449108)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
