tags:

views:

30

answers:

1

I have created a simple WordPress plugin that automatically sets my new sites up with the default settings that are shared across all of them.

As part of the install, it creates my Privacy Policy page. However, currently, I'm just inserting "This is the privacy policy page" for the content, since it's just stored in a variable that I send to the wp_insert_post function. I'd like to actually insert my full, html formatted privacy policy instead of the dummy text.

I'm just looking for some ideas as to how I can do this.

Here's the code I'm using currently for the hard coded privacy policy insert...

    $my_post3 = array();
    $my_post3['post_title'] = 'Privacy Policy';
    $my_post3['post_content'] = 'Insert your privacy policy content here';
    $my_post3['post_type'] = 'page';


    //insert the default pages into the site
    wp_insert_post($my_post1);
    wp_insert_post($my_post2);
    wp_insert_post($my_post3);

So I just need to perhaps parse an external .html or txt file and stream it into that $my_post3['post_content'] variable, keeping all the html formatting intact.

Any ideas?

A: 

Well, if you have the content in a file that's accessible, you can just use file_get_contents():

$my_post3 = array();
$my_post3['post_title'] = 'Privacy Policy';
$my_post3['post_content'] = file_get_contents('privacy_policy.txt');
$my_post3['post_type'] = 'page';

That of course requires that the privacy_policy.txt file is in the same directory with your PHP script. If you have the privacy policy on another server, you can also use an url in the file_get_contents:

$my_post3['post_content'] = file_get_contents('http://www.domain.com/privacy_policy.txt');
Tatu Ulmanen
Or that you provide the full path and that it is accessible.
Vinko Vrsalovic
@Vinko Vrsalovic, that's true. I've updated my post to take this into account.
Tatu Ulmanen
Sweet! Thanks Tatu, you da man! (as usual :-)
Scott B
One problem I have is that the .txt file has some ' characters that are apparently tripping up the insert. How can I sanitize the file_get_contents function?
Scott B
@Scott B, I thought Wordpress would take care of that itself but you can use the `mysql_real_escape_string` or `addslashes` functions for that.
Tatu Ulmanen