0

I'm creating a website based on WordPress, but it's not a blog and very much a sui generis website, so I'm developing a plugin that will add the various features I need.

In particular, I have created a custom post type for a special type of pages, which can be created by any person who has subscribed to the website. But in order to create such custom posts, subscribers will need to fill a complicated multistep form, which is used to store data in various custom tables in the database, not just wp_posts. This multistep form is so different from the regular post creation/edit page in WordPress that it doesn't really make sense to try to customize it by adding custom fields, meta boxes, etc.

Moreover, I don't want subscribers to be sent to the dashboard, which is to be seen only by administrators. So I want the custom post creation/edit page to be part of the frontend, not the dashboard, although I want administrators to be able to access it from the list of custom post types in the dashboard. Basically, when the administrator clicks on a post in the list of custom post types in the dashboard, he is sent to the same page, seemingly part of the frontend, as regular users who create/edit such a custom post.

I don't have much experience with WordPress development, so I'm not sure what's the best way to do this. Right now, I'm thinking that I just need to create a creation/edit page from scratch, using get_header and get_footer to include the header and footer so it integrates with the rest of the frontend, and somehow customize the dashboard so that administrators are also sent to that page when they click on a custom post in the dashboard to edit it. I just want to make sure that it's the right way to do this and that I shouldn't use some other method that I don't know about.

  • you can realise this multistep form with what you forecast to do. but if you think you will often work with wordpress, it will be interesting to well separate data handling and data display. the first goes in plugin and the second goes in theme. then your form login may go in the plugin code and is displayed by a shortocde. the aim is that you don't have to copy-paste the form treatment if you change the theme. – Kaperto Nov 15 '19 at 10:28
  • Thanks, I was actually wondering about whether there was a way to separate data handling from data presentation, but I hadn't thought about creating a shortcode, so I will do that. – Philippe Nov 15 '19 at 16:41
0

when a shortcode is defined, he has to return HTML output. then to allow the theme to customise data presentation, you can do something like that :

add_shortcode("form_step_2", function ($attr, $content, $tag) {

    // preparing data
    //...


    // result to display

    $template_name = "form/step_2";

    // search the file wp-content/themes/twentytwenty/my_plugin/form/step_2.php
    $template = locate_template("my_plugin/$template_name.php");

    // if the file doesn't exist in the theme
    if ("" === $template) {
        // use the plugin file wp-content/plugins/my_plugin/templates/form/step_2.php
        $template = __DIR__ . "/templates/$template_name.php";
    }

    ob_start();
    require $template;
    $output = ob_get_clean();


    return $output;

});
| improve this answer | |
  • Thanks, this is helpful, but I'm actually wondering whether I should really do this with a shortcode. Wouldn't it be preferable to create a new page that will contain the multistep form when the plugin is activated, using the method described in this answer, after creating a custom page template specifically for that page that displays the form? I would like to put the function that actually updates the database in the plugin files, but I'm not sure how to do proceed exactly, so data handling and presentation are kept separate. – Philippe Nov 21 '19 at 15:52
  • So basically the custom page template generates the HTML for the multistep form, taking care of internationalizing it (my website will be available in several languages), outputting something like <form action="process_form.php" method="post"> but process_form.php is located in the plugin directory, so I guess I'll have to use a WordPress function to get the correct path and insert it here. – Philippe Nov 21 '19 at 16:14
  • I guess I should use admin-post to process the form instead, as explained in this answer. – Philippe Nov 22 '19 at 15:28

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.