wp_insert_post question [closed]

with the wp_insert_post i want to insert text from a html-form. This is my string i want to insert:

[code]
#include<stdio.h>

int main() {
printf("Hello World\n");
return 0;
}[/code]

when i publish the post it looks like this:
Sourcecode in WordPress post

“Hello Worldn” should be “Hello World\n” and stdio.h after } should not be there.
EDIT:
It should look like this:
enter image description here

1 Answer
1

You need to add slashes as wp_insert_post will remove them:

$data = array(
    'post_content' => '[code]
#include<stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
}
[/code]',
);

wp_insert_post( wp_slash( $data ) );

Update: After running the above, this is exactly what I get in the editor:

[code]
#include

int main() {
printf("Hello World\n");
return 0;
}
[/code]

Leave a Comment