• Resolved kona023

    (@kona023)


    I’ve noticed when you install the Woocommerce plugin it automatically creates several pages for you with header nav links as well (shop, account, cart, and checkout). Is this a well-received practice for plugins to do this upon install or is it a bit intrusive?

    Now that I’ve asked that I’d like to know how to do it. My googling keeps bringing me to articles not having anything to do with plugin development and trying to find anything in the woocommerce code is daunting.

    Basically I just want to create a two empty pages (with associated menus) and place my short codes in there as a convenience for the site owner who could then go in there an modify the pages to their liking.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Yes, it is common for plugins to make setup as easy as possible. They usually contain checks for whether it’s been done already, so it doesn’t get out of hand.
    But this is not a thing for themes to do. Themes should never create content or have “side effects” on installation, because of the ease of switching themes needed for previewing and all around flexibility (user should not be locked in to one theme).

    So, in your plugin, think of a way to determine if you’ve already created the pages, and check for that. If not, call wp_insert_post to create the pages and wp_update_nav_menu_item to create the menu items. Putting the menu items into the menu might be a bit intrusive.
    https://developer.wordpress.org/reference/functions/wp_insert_post/
    https://developer.wordpress.org/reference/functions/wp_update_nav_menu_item/

    Thread Starter kona023

    (@kona023)

    Thanks for the info Joy! Interestingly enough, wp_insert_post gave me both of the results I was looking for. My searching brought me to get_page_by_path for checking first whether the desired page exists before creating it so it looks like I’m good to go. Thanks again 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Plugin dev and adding pages and menu items to theme’ is closed to new replies.