How to call wordpress functions from a form processing script

I’m working on a plugin which submits data via a form from a custom admin page. This is a simplified version of my form:

<form action="<?php echo plugin_dir_path(); ?>/process.php" method="post">     <input type="text" name="keyName">     <input type="submit" value="Update"> </form> 

The form is inside my main php file for the plugin, so it has access to all the wordpress functions like the plugin_dir_path() I called above.

However when the user clicks the “submit” button, and the $ _POST variable is submitted to the “process.php” script, I lose access to all the wordpress functions in that process script.

I searched how to add wordpress functions into external scripts and I saw this question: How can I call WordPress core functions in external scripts?

The answer provided is that I include this line of code at the top of my processing script:

require_once("wp-load.php"); 

However when I do the “wp-load.php” is appended to the end of the current url which results in a 404 type error. I can’t use the “get_site_directory()” function to point to the main wordpress install directory because it’s a wordpress function.

How can I make this work? Is there an action hook I should be using to submit the form vs my own custom submit button?

Service Layer with multiple validation and other functions

I am currently developing a REST Api using Slim Framework with a Layered Architecture:

Controller->Service->Repository->DB

My problem is the save() function on one of my services contains a collection of complex functions which clearly violates the Separation of Responsibility Principle so I grouped the functions based on their “responsibilities”. Here are some of it:

  • Data Validation
  • Record Locking
  • Edit History Logging
  • Destination Delegation

These functions are not always executed. It executes depending on the data passed onto the service and some of the functions blocks the saving if a business rule is violated. Currently,I am planning to solve it using the Chain-of-Responsibility Pattern

class PropertyController {     public function saveAction($  request, $  response, $  args)     {         $  data = $  req->getParsedBody();          $  dataValidation = new DataValidation();         $  recordLocking = new RecordLocking();         $  editHistoryLogging = new EditHistoryLogging();         $  destinationDelegation = new DestinationDelegation();          $  dataValidation->next($  recordLocking);         $  recordLocking->next($  editHistoryLogging);         $  editHistoryLogging->next($  destinationDelegation);          $  propertyService = new PropertyService(new PropertyEloquentRepository());         $  result = $  propertyService->save($  data, $  dataValidation);          return $  response->withStatus(200)             ->withHeader('Content-Type','application/json')             ->write(json_encode($  result));     } } 

Is this implementation fine and is considered a good practice?