• Hi, I’m building a Theme and need it to auto add a page when the user uses the theme.

    What I need is for my functions.php file to check if the page exists and if not create it, if the user deletes it then functions.php (as it runs when a person accesses the site) will auto-recreate it again.

    This page needs to have a slug of ‘add’ and also have a template assigned to automatically.

    How can I do this thanks?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hm, I just thought about it, but why do you want to do that? Don’t you think that it’s pretty annoying from a users perspective to delete a page and see it being recreated again? Maybe there’s another solution to your problem…

    Thread Starter driz

    (@driz)

    Well deleting it would be the last thing a user would do when using the theme as its a core part of the theme.

    What about adding the page and linking it with a template file. How do I do that? Would you be able to provide the code for that thanks?

    I haven’t implemented this yet so I can’t copy and paste some code in here. But I can tell you how I would start working on this. Try inserting your new page into the posts table. This requires setting a valid ID (e.g. “SELECT MAX(ID) + 1 AS new_id FROM wp_posts“), post_author and so on.

    Regarding the template file you’ll have to insert a new row into the postmeta table. It should use a unique meta id (“SELECT MAX”, etc.), reference your post’s id, using the meta key “_wp_page_template” and setting the value to something like my-template-file.php which denotes your template file.

    Thread Starter driz

    (@driz)

    Yup with you so far. Would you be able to post up the full code that will do the function of adding it to the DB when you get chance? Thanks again.

    Sorry, haven’t got the time to do this right now. Hope you can do it yourself.

    Thread Starter driz

    (@driz)

    Bummer ?? Anyone else able to help with this? Thanks again.

    I know it’s been a while and I hope you were able to figure it out yourself. For those who weren’t able to do this, have a look at this nice post.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Auto Add Page’ is closed to new replies.