-1

I made some research and found a way how to create custom pages using content (post) types. However, I do not want to create a new post type, I just want to create a page having a certain URL (/form) with a form on it. I was not able to find how to do it without custom post types. Can someone show me the correct direction? Thanks a lot.

  • 2
    I'm not sure I understand. Why can't you just go to Pages > Add New, create a page called "Form" and put your form on it? If you needs something more than that you'll need to be more specific with your question. – Jacob Peattie May 21 at 15:36
  • Because I want to do it programmatically. It's in the title. I do not need to explain why do I need to do it that way. – petiar May 22 at 15:49
  • Can someone clear up what is wrong with this question? It is perfectly clear what I want to achieve (create a page programmatically, not via admin interface, obviously - it's right in the title!) and I also made my effort to try to find it out, however, no luck. An accepted answer bellow proves that the question is perfectly alright. Obviously, most of the WP "developers" just know how to click mouse and install plugins. – petiar May 22 at 21:15
1

You can use wp_insert_post() function with 'after_theme_setup' action hook to programatically create pages. Here is a short example,

add_action( 'after_setup_theme', 'create_form_page' );
function create_form_page(){

    $title = 'Form';
    $slug = 'form';
    $page_content = ''; // your page content here
    $post_type = 'page';

    $page_args = array(
        'post_type' => $post_type,
        'post_title' => $title,
        'post_content' => $page_content,
        'post_status' => 'publish',
        'post_author' => 1,
        'post_slug' => $slug
    );

    if(post_exists($title) === 0){
        $page_id = wp_insert_post($page_args);
    }

}   
0

Install a form plugin. Contact form 7 is a good one : https://en-au.wordpress.org/plugins/contact-form-7/

Create a form using the plugin.

Create a normal WP page and add your form to it by pasting the contact-form-7 shortcode into the page.

  • Noticed the word "programmatically" in the question title? – petiar May 21 at 18:18
  • So "How to programmatically create a page with a form in WP?" not "How to create a page with a form programmatically in WP?" – bjornredemption Jun 9 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.