Support » Fixing WordPress » On page creation set parent based on user

  • Hello,

    I need some help or information about the following:

    I’m creating a website that has different users and they have the possibility to create pages. However, they may only create child pages under their own parent page created by the admin. It sounds more complicated than it is, I think.

    Example:

    When user “user-1” creates a page, his parent page must be automatically selected so that the hierachy is correct.

    Main page
    – Provider page 1
    — Provider package page 1 (created by user 1)

    The parent page has the same name / title as the username of the user who can make a page in it.

    Perhaps there is a possibility that you can see when “user-1” is logged in and if there is a match with a page with the same name. When this is true, he automatically selects the page with the same name as a parent.

    Maybe something like this is possible?

    • This topic was modified 3 years, 3 months ago by precies.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Almost anything is possible with custom code 🙂 I think a good approach would be to hook into the ‘wp_insert_post_data’ filter where you can alter page data as required just before the page is added. You could set the page’s parent ID to that belonging to the user, regardless of what the user may have selected or not selected. If such a page does not yet exist, your code could create one. Just be careful not to get caught in an infinite callback loop because adding a parent page will also fire this same filter hook.

    You don’t really need to name the parent page as the user’s name, though it’s not a problem to do so. The user’s parent page could be stored in user meta so its name would not be critical.

Viewing 1 replies (of 1 total)
  • The topic ‘On page creation set parent based on user’ is closed to new replies.