Support » Everything else WordPress » How to crteate a custom post type page with content via plugin activation

  • Resolved starapple

    (@starapple)


    I’m converting a php script to function as a WP plugin. One of the things I need to accomplish is to have front facing forms to collect information. I’ve found how to create new pages when a plugin is activated and how to add blocks. I also know how to create custom post types.

    I need to combine elements of the two operations so that I can create say category pages (a) teachers, (b) students, (c) events, (d) xyz. Why do I need custom types? If I create the pages and make them available on the front to collect new teacher, student records etc on the front, that’d be fine. However, (1) I need to be able to “create new teacher” etc on the back-end as well and (2) I need to have organized searchable archives.

    This gives me my single page:

        $page = array(
          'post_title'  => __( 'New Page' ),
          'post_status' => 'publish',
          'post_content'   => '[content-shortcode]',
          'post_author' => $current_user->ID,
          'post_type'   => 'page',
          'comment_status' => 'closed',
          'ping_status' =>  'closed' ,
        );
        wp_insert_post( $page );

    Does the same snippet offer the creation of a post_type’ => ‘post’ and to also add post_content? I’m not quite clear from what I’ve read.

Viewing 4 replies - 1 through 4 (of 4 total)
  • I need to combine elements of the two operations so that I can create say category pages

    No, you don’t. WordPress handles the archives for you.

    Does the same snippet offer the creation of a post_type’ => ‘post’ and to also add post_content?

    Yes, your code shows the content as being a shortcode. You can put whatever you want in there. See https://developer.wordpress.org/reference/functions/wp_insert_post/

    Thread Starter starapple

    (@starapple)

    Thanks @joyously. I’ll give it a try again. For some reason when I tried to preview the auto created page from the bloc editor I kept getting a message that the save action could not be completed. I’ll review the shortcode function to see what’s what. Good to know it’s not the page creation process that’s a humbug.

    • This reply was modified 5 months, 1 week ago by starapple.

    Wait, what?
    The editor creates pages. Your shortcode should return content, inside the page when viewed on the front end. The wp_insert_post() function creates a separate page from the editor. Hopefully, you’re not running that on the editor’s save action.

    Thread Starter starapple

    (@starapple)

    No, no, no. 😀

    After the page was created, I went to Pages in the admin menu, opened the created page (opened in the Block editor) and when I tried to preview it on the desktop, the preview window opened in a new tab as I had selected. At the top of the block editor was a message that it could not complete the save.

    The good news is that I visited the page just now to recreate the problem and voila, no error when I previewed and when I actually clicked “Update” and it executed correctly with its message that “Page updated”. In between my post here and getting your answer, I disabled a plugin that seemed buggy so that might have been the bugger.

    Thanks again for your guidance.

    • This reply was modified 5 months, 1 week ago by starapple.
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.