WordPress provides amazing support for applications to submit forms. Whether you are adding forms to an administrator or public area, you can efficiently process form requests through a built-in mechanism that includes admin post and admin Ajax scripts. This article describes how to use the WordPress API to handle custom form submissions. Describes the process of adding custom forms to the management area of the plug-in, processing form submissions through HTML and Ajax requests, and creating a form handler using PHP to validate, delete, and process forms. Input. The
The same concept can be applied when using forms in public areas during your stay in the WordPress administration area. We will also use the object-oriented programming configuration of the plug-in. However, you can also use procedural code to achieve the same results. Here, you can an internship plug-in and imitate the article. Note: this document is for intermediate word press developers. Suppose you have operational knowledge of HTML, JavaScript, jQuery, PHP, and the word press plug-in API. If you want to review, please read the following: The
Developing WordPress for beginners: start developing WordPress for beginners: build WordPress for plug-ins developers develop javascript: develop advanced WordPress using Ajax: an introduction to object-oriented programming begins with understanding the WordPress mechanism provided to handle general form publishing requests. Admin post in WordPress. With PHP, you can submit various hooks provided by WordPress through forms to fully control the application execution flow. This makes no difference when dealing with forms. All you need is the correct hook up, that is, connect and add a custom form processor. Hooks that handle custom forms are dynamic in nature. That is, the mount name depends in part on the user. The
To process only form related submissions, you must control them more carefully, as shown below: Male admin post Submitting a WordPress form using PHP is to submit the form to adm post in the WP admin directory of WordPress. Specify as a PHP file, and the form contains the custom name of the action. In this way, WordPress triggers two task hooks based on the user’s login status. Admin\u post\u{$action} for logged in users and admin\u post\nopriv\u{$action} for non logged in users, where $action is the name of the task passed through the form. The
Then you can use add\u action. You must fully control the form data processing, bind the PHP form handle to the trigger hook, and use $\u get and $\u post variables. As you may have guessed, admin-post. PHP can handle post and get requests, as well as requests for administrative and non administrative areas of applications, even names. Let’s use a custom plug-in to see this. In the object-oriented plug-in structure, my goal is to help you understand everything you need to process custom forms in word press, rather than using or using Ajax. This article provides a custom plug-in, which you can and imitate here. It is recommended that you open it in the appropriate editor and install it only in your local word press settings. The
With the help of common plug-in tools, we use object-oriented programming method to build plug-ins. The business district starting point is one of many best practices listed in the WordPress plug-in manual. Ensure the consistency of plug-ins, and put the standard code
This is a good way to save a lot of time. Over time, you may create custom common phrases based on your coding preferences. That’s what I did. The plug-in was originally based on the in fork plug-in template of the WordPress plugin boilerplate project. Similar to the original project in many ways, but also supports namespaces and automatic loading. This eliminates the need to have a unique prefix for all classes or functions, and does not end with many include and require statements. But the minimum version of PHP required for my plug-in is 5.6.0. The
Note: if you do not use namespaces or use procedural code, you must prefix all projects. The plug-in is configured in the back end as follows: Inc\/core\/* – the core function of the plug-in inc\/admin\/* – the function related to the management domain inc\/frontend\/* – the function related to the public domain inc\/common\/* – the function shared between the administrator and the front end and the plug-in structure plug-in at the back end. There are top-level management menus with two menu items. To view how to add an administrator menu page in the administrator menu structure of the plug-in, view a, and then use the define_admin_hooks() method inc\/core\/class-init. PHP and add\u plugin\admin\menu() methods inc\/admin\/class-admin. PHP plug-in. The
To learn more about how to add an administration page to a plug-in, see the article on creating a WordPress administration page here. Add a form on the management page of the plug-in when adding the submit HTML form menu page of the plug-in, you must also specify a callback to load the page content. The form will be added here. Important points nuancedesignstudio\/afc5dac6bc5c36291b824e21a2832555 is loading. Instead of writing HTML directly in the html\u form\u page\content method, it is in other files in inc\/admin\/views, partials HTML form view. Load it into the callback in PHP HTML format as follows: The
Key points: the nstudio\/9361c83a07f8c63343a79a59bfbb09d41 is being loaded. This is purely a coding preference. Separate the HTML to read the code, and there is no difference in the form output of the plug-in page. The form added above the HTML form security, structure and submission understanding on the plug-in management page has two text fields, including the selection field of the existing WordPress user drop-down list and the text field entered by the user. But in this simple example, a lot of things are going on later. The following form code is self explanatory. Let’s take a look at the important elements. The
Key points during the loading process of nstudio\/48ebf27160bcc2a99e20ca0845b252e3, in the form security WordPress management area, the most important thing to consider when processing forms is security. Protect forms using a combination of word press nonce and current\u user\u can ($capability). In my example, if (current\u user\u can (\
We also created a custom nonce using wp_create_nonce() and added it as a hidden form field. Instead, you can add it directly using wp\u nonce\u field(). This is a good article to understand non in detail. To ensure the uniqueness of the form structure, we prefix the plug-in name with all form elements. Via JavaScript
Since only my form elements can be specified as targets, this is again a personal encoding preference. We also use the HTML5 required attribute to give the form validation to the browser. Male management form check form submission form uses the admin_url (\
The ax library is automatically set to XMLHttpRequest. It can also be used to identify Ajax requests, but is not always reliable. The Ajax () method in jQuery submits the request to the server. Key points fine design admin Ajax in nstudio\/9fb35cd0c0cd80a9e53b7ea51365f2c7 loading. Params gets the form submitted to PHP. Params passed from PHP using ajaxurl. Use the ajaxurl array wp\u localize\u script. Note: the form data in my example contains the actions word press uses to generate hooks for Ajax requests. The following hook is triggered by word press. Wp\u ajax\nds\form\response is the wp\u ajax\nopriv\nds\form\response JavaScript file of the login user, as shown below: class admin. Add it to the enqueue\u scripts() method of PHP enqueue\u scripts(). Key points in nuancedesignnstudio\/99a46c09d583a65c2d0554f781ab6b85 loading, the ajaxurl global variable admin Ajax in PHP. You can also use the global JavaScript variable ajaxurl instead of passing the PHP URL. However, variables are only available on the administrator side, not when AJAX is processed on the front end. According to the response of the server, Ajax. Fail() callback. Done() and. Fail() will run with it. In my example, an empty div container \nds\u form\u feedback response is added for a successful request, which is part of my form HTML. Finally, reset the form to clear the fields. The form handle of the Ajax request the same form handle the_form_response is also attached to the Ajax request. Important: during the loading process of nuance designstudio\/7dc320db5b1787f6cb2256b48b2970e, the form handler uses $\u post[\