1

Is it possible to conditionally set the post_content in wp_insert_post? Im using a foreach to add multiple pages at once:

foreach ($create_pages as $new_page) {
    $add_pages = array(
        'post_title' => $new_page,
        'post_content' => '', // lets say, if the page_title is 'Home', set the post_content to 'Default home page content'
        'post_status' => 'publish',
        'post_type' => 'page'
    );
    $page_id = wp_insert_post($add_pages);
}

Would it be also possible to set the default content for all the pages inside a php file?

2
  • Of course it is possible, but what are the conditions you need?
    – s_ha_dum
    Commented Feb 22, 2014 at 16:53
  • @s_ha_dum Well something like, if(post_title == 'Home') { return ''; } elseif(post_title == 'About us') { return 'Some about us content...'; } elseif(post_title == 'Contact') {.....} etc. Would it be possible to put these conditions into a separate file so it doesnt get too messy? Commented Feb 22, 2014 at 20:24

3 Answers 3

2

I can't help but think I am missing something but what seems like the obvious answer is to alter your $create_pages array:

$create_pages = array(
  'one' => 'content for one',
  'two' => 'content for two',
  'three' => 'content for three'
);

foreach ($create_pages as $title => $content) {
    $add_pages = array(
        'post_title' => $title,
        'post_content' => $content, // lets say, if the page_title is 'Home', set the post_content to 'Default home page content'
        'post_status' => 'publish',
        'post_type' => 'page'
    );
    var_dump($add_pages); // debugging
//     $page_id = wp_insert_post($add_pages); 
}

It is possible to use a second array of post content and match it against the titles but based on your description of the problem, I can't see why you'd need that complexity.

Another option would be to use nested arrays, something like this:

$create_pages = array(
  array (
    'title' => 'one', 
    'content' => 'content for one'
  ),
  array (
    'title' => 'two', 
    'content' => 'content for two'
  ),
  array (
    'title' => 'three', 
    'content' => 'content for three'
  ),
);

foreach ($create_pages as $page) {
    $add_pages = array(
        'post_title' => $page['title'],
        'post_content' => $page['content'], // lets say, if the page_title is 'Home', set the post_content to 'Default home page content'
        'post_status' => 'publish',
        'post_type' => 'page'
    );
    var_dump($add_pages);
//     $page_id = wp_insert_post($add_pages);
}

I am fairly sure that either version should work fine ( see this and this), though the latter is probably going to be more readable if you have long titles.

If that array is oppressively large, you could put it in another file and include it.

5
  • Thanks this looks much simpler and cleaner, Im newbie in PHP so I had no idea how its best to do things like this. so it can be anything like 'Some long page title' => 'Content for some long page...'? Commented Feb 22, 2014 at 21:34
  • @RichardMišenčík : see the edit
    – s_ha_dum
    Commented Feb 22, 2014 at 21:43
  • Thanks, the first example is more than enough, no need for nested arrays. The titles wont of course be that long, I was just wondering if they key in array can be uppercase, with spaces and characters like ťžýáí etc. And including seems like the way to go, or will it slow things down much? Also, what do you think about Shazzad's approach? Commented Feb 23, 2014 at 12:54
  • I doubt you would notice any degradation in performance with include. The approach used by @shazzad should work but it looks like overkill to me, not to mention more work and more code. But your description is lacking many of the details of your project. You may need something more complicated. I can't tell.
    – s_ha_dum
    Commented Feb 23, 2014 at 14:49
  • Yeah, its not going to be that much content anyway so no problem. Yeah it would be overkill. Yeah I probably should describe it better but basically its for when theme is activated I want to create default pages so the client doesnt have to do it manually and I dont need to assist them. Also in case the db would get deleted, that code would be ran again. I chose your answer as best, if it would be possible Id choose both of them as best. Commented Feb 23, 2014 at 20:10
2

Yes, you could try this -

foreach( $create_pages as $new_page ){
    $content = "Default ". strtolower($new_page) ." page content";
    $add_pages = array(
        'post_title' => $new_page,
        'post_content' => $content,
        'post_status' => 'publish',
        'post_type' => 'page'
    );
    $page_id = wp_insert_post($add_pages);
}

Edits:

Regarding a an external logic, you could try the following.

foreach( $create_pages as $new_page ){

    $content = "Default ". strtolower($new_page) ." page content";
    // allow external filter to the page content.
    // filter names become available appending the page name with 'external_page_content_'
    $content = apply_filters( 'external_page_content_' . sanitize_title_with_dashes($new_page), $content );

    $add_pages = array(
        'post_title' => $new_page,
        'post_content' => $content,
        'post_status' => 'publish',
        'post_type' => 'page'
    );
    $page_id = wp_insert_post($add_pages);
}

Now, to add the content for a page, ex: About Us

add_filter('external_page_content_about_us', 'my_about_us_page_content');
function my_about_us_page_content($content){
    // here you can define your about us page content
    return $content;
}

Reference:

  1. sanitize_title_with_dashes - this strips space with dashes and non alphanumeric characters, and make all alpha characters lower cased.
  2. apply_filters - this is used to leave callback, as we do with call_user_func
5
  • Thanks that works well, but would it be possible to use an external file for the logic? Because I need a larger content to be set as post_content, not just Default $page content.. For example pages like About us, Contact, etc. Commented Feb 22, 2014 at 20:21
  • Check the updates.
    – Shazzad
    Commented Feb 23, 2014 at 5:44
  • Thanks, this is another cool way, but which would be better? This or just creating an array like 'Home' => 'Content for home' by s_ha_dum. Commented Feb 23, 2014 at 12:26
  • Array would be the right choice ofcourse. I just added the last code to my answer as you wanted to "use an external file for the logic". The method allows you to add hook to change/push content by page name, even from other files. While using array, you can not use other file obviously.
    – Shazzad
    Commented Feb 23, 2014 at 14:59
  • Thanks for your answer and help, I learned something new. I chose the other answer as best because its simpler. But thanks, you described it all very well. Commented Feb 23, 2014 at 20:13
0

Depends on what you want it to be conditional on. E.g.

<?php
foreach( $create_pages as $new_page ){
 if ($new_page == 'Home') {
   $content = "Home is where the heart is";
} else {
   $content = "$new_page is going to be a great page.";
}

$add_page = array(
        'post_title' => $new_page,
        'post_content' => $content,
        'post_status' => 'publish',
        'post_type' => 'page'
    );
    $page_id = wp_insert_post($add_page);
}
?>
1
  • Well, for example, for home I dont need content because I use page template. But for others it will be a few paragraphs with the text for those pages. Im doing this so the client doesnt need to add that content when the page is deleted or on new install etc. Especially when it includes some HTML and they might not even be able to find the HTML tab in the editor. Commented Feb 22, 2014 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.