0

How do I make the HTML form's action target file 'aware' of classes that WordPress has already loaded?

File A -> Creates a WordPress shortcode that inserts a form that has 'File B' as the target. File B goes to process the submitted data, but throws "Class Not Found" whenever I try to instantiate an object of the class that contains the function responsible for processing the data.

Code:

File A Psudeocode (works)

add_action('plugin_loaded',array('wordpress_plugin_bootstrap','load'),5);
class wordpress_plugin_bootstrap{

   public static function load() {
     //check for required class
     require_once('FileB.php'); //contains the class 'PluginClass'
     //register the class 
   }

}

function class_instance_getter(){
    return PluginClass::get_instance():
}

function create_html_form($atts){
//creates and returns the form with action attribute set to 'FileB.php'
}
add_shortcode('ju_lee_do_the_thing','create_html_form');

File B Pseudocode

REQUIREDCLASS::include_addon_framework();

class PluginClass extends OtherClass {

        //init stuff

        private static $_instance = null;

        public static function get_instance(){
            if ( self::$_instance == null){
                 self::$_instance = new PluginClass();
            }
            return self::$_instance;
        }

        public function init(){
            parent::init();
            add_action('wp_loaded',array($this,'handle_form')); //me, trying desperately to get this thing to hold it's fire until everything is loaded
        }

        public static function handle_form($postArray){
            //do stuff with the data using the REQUIREDCLASS
        }
    
} // End of class code

$obj = new PluginClass; // Blows up here
$obj->handle_form($_POST);

Error

Fatal error: Uncaught Error: Class 'PluginClass' not found in /../../FileB.php on line ['blows up here']

It dawned on me that when execution 'drops in' to the HTML form action target file (whether that's in FileB or elsewhere) it doesn't 'know' what WordPress has (or hasn't) loaded; I can observe everything loading and registering correctly by debugging FileA on a page refresh.

So the questions become these:

Do I need to do something to make WP 'aware' of my action target file?

Should I actually be re-loading everything I need upon code entry of my target action file? Even if that means reloading an entire WP plugin (this seems wrong to me)?

While were on the subject...a HTML form action target is kind of acting like a callback handler (once the user hits submit, dial this number sort of thing), but it isn't technically - so what is it? This will greatly inform my Google searching. Also, I've tried moving all of the form_handler code to another file (uh...'FileC' ) which sort of works in that I can actually get into the handler code which works great until I need to reference the REQUIREDCLASS...then it just blows up there instead.

I think I'm about 20 hours in, give or take, in trying to figure this out. Feel free to light up the comments with 'Stuff about PHP and/or WP I really ought to research'.

Also - a HUGE thank you to anyone that can offer guidance on this. I feel like a cat in a laser pointer store.

9
  • Does this answer your question? Plugin Form Submission Best Practice Aug 12 '20 at 13:34
  • How is class_instance_getter called?
    – Tom J Nowell
    Aug 12 '20 at 14:01
  • @JacobPeattie I hesitate to say 'no', but I've tried sending it to a file that doesn't contain the class (which, to be fair, does allow execution to actually enter the handle_form function), but I get hit with the same error once I try to pass execution back to the class I need to use. Aug 12 '20 at 14:43
  • @TomJNowell I'm having some issues with my XAMPP install atm. I will hopefully have a more satisfactory answer for you soon. For now, all of this code exists as an attempt to use the Gravity Forms AddOn Framework. The load() function in the bootstrap, and even my init() function in the PluginClass run immediately after Gravity Forms loads. It's like it 'forgets' that it already loaded the class I'd like to use by the time it's revisited from the form's target action. Aug 12 '20 at 15:03
  • @TomJNowell Hmm, class_instance_getter is never called. I would have thought this is what "$obj = new PluginClass;" would have fired to create the instance no? Aug 12 '20 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.