2

I have a project where I'll be using WP as a customer portal to an external application. The application is accessed via web services (soap/xml) calls.

The application has quite a few forms and for that I plan on using Formidable's Form plugin. I have the login process modified so the login actually is done remotely and a local user is created if one doesn't already exist.

Looking ahead a bit I'm trying to understand how I distribute the custom pages I'll need. I don't plan on being theme specific. So I imagine I'll be installing plugins and modify the database (for the custom pages).

My question is there a guide/standard methodology for distributing plugins that is applicable to my situation? When plugins have specific pages how are those installed? and how are the menus adjusted accordingly?

1

Well, not quite sure if I understood this, but I see that you want to 'simulate' pages using a plugin, and my best bet is to build your own posts on-the-fly using WordPress queries and rewrite rules. Let's try this:

Create a variable to respond to your view.

add_action( 'query_vars', 'add_query_vars' );
function add_query_vars( $vars ) {
    array_push( $vars, 'form_id' );
    return $vars;
}

Create a rewrite rule to fill this variable:

add_action( 'rewrite_rules_array', 'rewrite_rules' );
function rewrite_rules( $rules ) {
    $new_rules = array(
        'forms/([^/]+)/?$' => 'index.php?form_id=$matches[1]'
    );
    return $new_rules + $rules;
}

Now, visit your site's options-permalink.php page to flush the rules and make the above one valid (http://yourdevsite.com/wp-admin/options-permalink.php).

You'll be able to access custom URLs like http://yourdevsite.com/forms/some-form or the equivalent http://yourdevsite.com/?form_id=some-form.

Now, as in WordPress we can't suppress the main query, let's just override it when a matching form_id occurs:

add_action( 'wp', 'custom_wp_query' );
function custom_wp_query( $wp ) {

    // Don't do anything for other queries
    if ( ! $form_id = get_query_var('form_id') )
        return false;

    global $wp_query;

    // Let's respond this request with this function
    $func = 'form_' . str_replace( '-', '_', $form_id );

    // Throw a 404 if there's no function to deal with this request
    if ( ! function_exists( $func ) ) {
        $wp_query->is_404 = true;
        return false;
    }

    // Set as a valid query for this case
    $wp_query->is_404 = false;
    $wp_query->is_single = true;
    $wp_query->found_posts = 1;

    // Call the function
    $post = call_user_func( $func );

    // Stick this post into the query
    $wp_query->posts = array( $post );
    $wp_query->post = $post;

}

And finally create your post:

function form_some_form() {
    return (object) array(

        // Put a negative ID as they don't exist
        'ID' => rand() * -1,

        // What matters for us
        'post_title' => 'Form title',
        'post_content' => 'Some post content (the form itself, presumably)',

        // It is important to maintain the same URL structure of 'add_rewrite_rules',
        // otherwise wrong links will be displayed in the template
        'post_name' => 'forms/' . get_query_var( 'form_id' ),
        'post_guid' => home_url( '?form_id=' . get_query_var( 'form_id' ) ),

        // Admin
        'post_author' => 1,

        // Straighforward stuff
        'post_date' => date( 'mysql' ),
        'post_date_gmt' => date( 'mysql' ),
        'post_status' => 'publish',
        'post_type' => 'page',
        'comment_status' => 'closed',
        'ping_status' => 'closed'
    );
}

So, if you want now to create more 'custom pages' for -- let's say -- a form with a some-other-form URL, just create a function named form_some_other_form just like form_some_form.

Obviously, the edit link won't work as will send you to an nonexistent post in admin.

And for the menus, as you ask, I suggest inserting these pages as custom links.

| improve this answer | |
  • Pretty well thought out answer for no reply. This is why I never answer questions to new people. Helped me out at least! – Bryan Willis Aug 19 '14 at 2:33
  • Well, we never know, so it's better to answer if we know the problem. Glad it helped you, anyway. =) – vmassuchetto Aug 19 '14 at 16:03

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.