Conditionally set post_content in wp_insert_post

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?

Related posts

3 comments

  1. 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.

  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
  3. 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);
    }
    ?>
    

Comments are closed.