Create page when plugin is activated

Since you want to check for a certain page, you could use one of the following functions, for instance:

  • get_post: Takes a post ID and returns the database record for that post, which can also be a page.
  • get_page_by_title: Retrieves a post given its title. If more that one post uses the same title, the post with the smallest ID will be returned.

Or you could write your own little function to get the page by its slug:

function get_page_by_slug($slug) {
    if ($pages = get_pages())
        foreach ($pages as $page)
            if ($slug === $page->post_name) return $page;
    return false;
} // function get_page_by_slug

You would use it like so then:

if (! get_page_by_slug('kns-products')) {
    ...
}

Leave a Comment