Multiple array for post_content on plugin activation

sorry for my bad English.

I want to create multiple pages when activating a plugin. This also works, but how do I get different content in the different pages?

if (!current_user_can('activate_plugins')) return;

        $page_titles = array(
            'Login',
            'Dashboard'
        );

        foreach($page_titles as $page_title) {

            $page = get_page_by_title( $page_title );

            if( ! isset ( $page ) ) {

                // create post object
                $my_post = array(
                    'post_title'    =  $page_title,
                    'post_content'  =  '[shortcode]',
                    'post_status'   =  'publish',
                    'post_author'   =  get_current_user_id(),
                    'post_type'     =  'page',
                );

                // insert the post into the database
                wp_insert_post($my_post);

            }
        }

I would be very happy to receive help

Topic wp-insert-post activation plugin-development pages Wordpress

Category Web


You just add the content to your initial array. The most simple way is using the titles as array indices like this:

$pages = [
    'Login'     => 'Some content',
    'Dashboard' => 'Some other content'
];

foreach ( $pages as $title => $content ) 
{       
    if ( get_page_by_title( $title ) ) {
        continue; // skip this page
    }
    
    wp_insert_post([
        'post_title'    =>  $title,
        'post_content'  =>  $content,
        'post_status'   =>  'publish',
        'post_author'   =>  get_current_user_id(),
        'post_type'     =>  'page',
   ]);
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.