If you're experienced with programmatically creating pages, you may wish to skip down to the code.

I’m currently working on two applications that I’m building on top of WordPress (of which I hope to share once they’re complete).

Both projects have views – basically pages with templates – that need to be created when the application is installed and the pages aren’t meant to be deleted.

Though I’ve covered how to programmatically create a post in WordPress, I thought I’d share a quick example of how you can programmatically prevent a page from being deleted in WordPress, too.

Post Statuses

In WordPress, a page is nothing more than a post type and, like all other post types, pages can have a certain status:

  • Publish
  • Pending
  • Draft
  • Auto-Draft
  • Future
  • Private
  • Inherit
  • Trash

In project, I’m programmatically creating pages so I’m setting their status to publish, by default.

In my particular case, the rest of the statuses don’t really matter; however, your mileage may vary if you’re working with pre-existing pages or pages that users are able to edit.

Prevent a Page From Being Deleted

When pages are deleted within the WordPress Dashboard, they are actually moved to the Trash. Technically speaking, their post_status is changed to trash.

In order to prevent a page from being deleted, it’s a matter of updating the post_status whenever it’s been changed.

Here are the requirements for the function listed below:

  • Try to find the page.
    • If it’s in the trash, change it’s status;
    • Otherwise, programmatically create it.

And here we go:

 * Programmatically creates a page with the specified title, slug, and
 * an optional template. Make sure that the page cannot be deleted.
function example_create_page() {

	// Initialize the page ID. This indicates no action has been taken.
	$page_id = -1;

	// Example titles and slugs
	$title = 'My Example Page';
	$slug = 'example-page';

	// First, try to get the page
	$page = get_page_by_title( $title, OBJECT, 'page' );

	// If the page doesn't exist, create it
	if( null == $page ) {

		// Create the page saving the ID
		$page_id = wp_insert_post(
				'comment_status'	=>	'closed',
				'ping_status'		=>	'closed',
				'post_author'		=>	1,
				'post_title'		=>	$title,
				'post_name'		=>	$slug,
				'post_status'		=>	'publish',
				'post_type'		=>	'page'

	// Otherwise, if the page is in the trash, update its status
	} elseif( 'trash' == strtolower( $page->post_status ) ) {

		$page->post_status = 'publish';
		wp_update_post( $page );

	} // end if/else

} // end example_create_page
add_action( 'after_setup_theme', 'example_create_page' );

The comments in the code should be clear as to what it’s doing. The only other thing worth mentioning is that this function  is after_setup_theme action. I’ve added it here because it’s part of the theme’s requirements.

Straight from the Codex:

This hook is called during a themes initialization. Is generally used to perform basic setup, registration, and init actions for a theme.

Again, this is a simple example and your particular use case may be a bit different, but the general principles should still apply.