Including the necessary functions for a custom ajax registration form

I know there are a few questions on registration forms but I think mine differs a bit as I haven’t found an answer through searching.

I’m making a simple subscribe widget that takes a user’s email and adds it to the WordPress Users table as a subscriber.

Read More

It begins with a simple form with just an email input and submit. The form’s action is to a php file in my plugin folder, ajax-signup.php. I’m using ajax to post the form data to this file which then “registers” the email address and returns an ajax response.

Older plugins just included the registration.php file which gave all the required functions to the ajax php file in order for it to perform the registration (check email validation, write to the database etc) as the file exists outside WP. What do I need to include, or what superior method is there that will allow me to do this? Maybe a function inside the WP environment rather than a standalone file? I’m not sure how to use ajax in this way though…

Related posts

Leave a Reply

1 comment

  1. Here is a basic setup that I use for AJAX with WordPress. instead of loading wp-load.php on a separate php file; just use WordPress default method for AJAX calls. This allows you to also filter function calls from Javascript through a switch. I also added a quick example for wp_localize_script.

    <?php
    
    add_action('wp_enqueue_scripts', 'YOUR_NAME_scripts'); //back end
    
    function YOUR_NAME_scripts( $hook_suffix ) {
    
                wp_enqueue_script('YOUR_NAME-js');
    
                global $blog_id;
                $params = array(
                    'site_url' => site_url(),
                    'admin_ajax_url' => site_url() . '/wp-admin/admin-ajax.php'
                );
    
                wp_localize_script( 'jquery', 'YOUR_NAME', $params );
    
    }
    
    add_action('wp_ajax_nopriv_YOUR_NAME_ajax', 'YOUR_NAME_ajax_function');
    add_action('wp_ajax_YOUR_NAME_ajax', 'YOUR_NAME_ajax_function');
    function YOUR_NAME_ajax_function(){
    
        fobu_load_classes();
    
        switch($_REQUEST['fn']):
            case 'test_ajax':
                $output = $_REQUEST['data'];
            break;
    
    
            default:
                $output = 'No function specified, check your jQuery.ajax() call';
            break;
        endswitch;
    
        ob_clean();
        $output=json_encode($output);
        if(  is_array( $output )  ):
    
            print_r( $output );
        else:
    
            echo $output;
        endif;
        die();
    
    }
    
    
    
    
    
    ?>
    
    <script>
    //in YOUR_NAME.js or whatever
    jQuery(document).ready(function() { 
        (function ($) { 
    
                    jQuery.ajax({
                        url: YOUR_NAME.admin_ajax_url,
                        dataType: 'json',
                        //type:'POST',
                        data: {
                           'action':'YOUR_NAME_ajax',
                           'fn': 'test_ajax',
                           'data': data
                           },
                        success: function(results){
                            //console.log(results);
                            if( results ){
    
                            }
    
                        },// end of success
                        error: function(errorThrown){console.log(errorThrown);}
                    });// end of ajax   
    
        })(jQuery);
    });
    
    </script>
    

    If you use the built-in wordpress AJAX, then you can easily access wordpress functions without having to use wp-load.php.