WordPress.org

Ready to get started?Download WordPress

Forums

Auto Add Page (8 posts)

  1. driz
    Member
    Posted 4 years ago #

    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?

  2. Christian Schenk
    Member
    Posted 4 years ago #

    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...

  3. driz
    Member
    Posted 4 years ago #

    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?

  4. Christian Schenk
    Member
    Posted 4 years ago #

    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.

  5. driz
    Member
    Posted 4 years ago #

    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.

  6. Christian Schenk
    Member
    Posted 4 years ago #

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

  7. driz
    Member
    Posted 4 years ago #

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

  8. Christian Schenk
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.