[Resolved (for now)] Custom forms without plugin question

I’m discovering Roots at the moment and it looks promising. The concept of a theme wrapper however confuses me when working with custom forms. Where do I put my form processing code? With other themes, I throw the $_POST code on the same page-template above the <?php get_header(); ?>.

Any tips how to process my form data with Roots?

( I’m no fan of form plugins like Gravity Forms, so I do not intend to use them. :wink: )

At first, put all your php stuff in custom.php or any other lib file you want :slight_smile:

Secondary, for security, use wp_nonce for example in this way

In form directly:

<?php wp_nonce_field('update_profile','update_profile_hidden'); ?>

And in your custom.php or any other file:

if ( !empty($_POST) || wp_verify_nonce($_POST['update_profile_hidden'], 'update_profile') ) {
#process form
}

Thanks for your swift reply. Putting the custom.php file in the Roots-Master root (or in the /lib subfolder for that matter) will lead to a 404 error. It will work however when I put the custom.php file in the WordPress root, but that’s not the proper way to build a theme or plugin.

I’ve been Googleing around to solve this 404 without success. I Hope you have the golden tip for me.

Edit 1: this is the code in the top of my form: action="<?php bloginfo('stylesheet_directory'); ?>/lib/custom.php"

Edit 2: It works when I hardcode the action URL: http://www.domein.tld/wp-content/themes/roots-master/lib/custom.php, though. But again, that’s not proper theme development ofcourse.

Edit 3: The code from “Edit 1” does work in the TwentyTwelve theme. Somewhere, somehow bloginfo() breaks in Roots and that will cause a whole lot of trouble with a lot of plugins.

I made the custom.php file work with the following workaround:

In the Roots functions.php I’ve put the following line of code above the require_once lines:
<?php define( 'ROOTS_URL', get_theme_root_uri() . '/' . get_template() ); ?>

Put the following code in the top of your form:
<form id="form_name" name="form_name" method="post" action="<?php echo ROOTS_URL . '/custom.php'; ?>" enctype="multipart/form-data">
And Bob’s your uncle!

I dont’t know why functions like get_bloginfo() don’t work, but at the moment this is a working solution.

If anyone has a better solution, please let me know!

Cheers!

DaRubster,

Do have advice on creating a custom user profile update form for the front end?

I’m not having much success getting it to work.