1

I'm creating my first Wordpress plugin. It's goal is to collect information from users by way of a custom HTML registration form. Essentially, when the user wants to sign up, I want this form to display.

I've created a rather extensive tabbed form (over 500 lines) and have saved it in the /public/partials section of the plugin's directory. I want to display this form and store the inputs, after sanitation and validation, to user_meta.

Now that the form is created, how do I reference the registrationform.php in my plugin to ensure it displays?

1 Answer 1

1

Here is the process I follow.

Step 1. specify the slug of the page you are putting the page on. To avoid hard coding this into the plugin, I add a setting option to store the slug of the page. So if I change the page, I only have to specify the slug of the new page in the settings page.

function my_plugin_settings_page() {
//Add a settings page for this plugin to the Settings menu.
add_options_page( 'My Plugin Settings', 'My Settings', 'manage_options', 'my-plugin-settings', 'display_my_plugin_settings' );
}


function display_my_plugin_settings{?>
<div class="wrap">

<h2><?php echo esc_html(get_admin_page_title()); ?></h2>
<?php 
$options = get_option('my-plugin');
settings_fields('my-plugin');
?>
    <form method="post" name="my-plugin-settings" action="options.php">
    <fieldset>
        <legend class="screen-reader-text"><span><?php _e('Signup Page', 'my-plugin'); ?></span></legend>
        <label for="my-plugin-signup">
            <?php echo home_url('/');?><input type="text" id="my-plugin-agent_signup" name="my-plugin[signup]" value="<?php echo $options['signup'];?>"/>
            <span><?php esc_attr_e('Signup Page', 'my-plugin'); ?></span><br>
            <span class="description"><?php esc_attr_e('The signup form will be added to the end of this page', 'my-plugin'); ?></span>
        </label>
    </fieldset>

<?php submit_button('Save', 'primary','submit', TRUE); ?>
</form>
</div>
<?php}

add_action( 'admin_menu', 'my_plugin_settings_page' );

Step 2. You have to change the template to be used on that page. For that we use the hook page_template.

public function signup_template($page_template) {
        $options = get_option('my-plugin'); //get plugin options
         if (is_page( $options['signup'] )) {
              $page_template = dirname( __FILE__ ) . '/public/partials/registrationform.php'; 
              //change the template if the page is the one specified
         }
         return $page_template;
    } 

add_filter( 'page_template', 'user_profile_template', 11 ); //filter hook to change the page template
3
  • Thanks for this answer. To be clear, this code would go into options.php in the root directory of the plugin? Or should it go into the admin subdirectory of the plugin?
    – SidC
    Commented Jul 8, 2016 at 22:17
  • The first part is just the required HTML for a menu page with only that option. You can display it by putting it in the callback function of the add_menu_page or add_options_page hook. Both front functions can be placed in the admin subdirectory. I'm not sure the structure of your plugin. So I cannot say for sure Commented Jul 9, 2016 at 5:27
  • I have edited the answer to include the action hook for adding the settings page. So you can just put both codes in the base my-plugin.php file. This is also true because in the code, the path to the registrationform.php file assumes that the file the code is will be in the same folder as the public folder. Commented Jul 9, 2016 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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