Simplify Creating Article Category Custom Field Code with Nette Form
WordPress added support for taxonomy item custom fields in version 4.3 to facilitate theme and plugin development. While adding the API, WordPress does not provide a UI interface for adding and managing taxonomy items. Since this is a newly added feature, plugins that support taxonomy items have not yet caught up. Although WordPress provides us with sample code to add taxonomy custom fields, unfortunately the code provides a method to build a user input form directly using HTML, mixing PHP and HTML code together, which is inefficient and very difficult to manage and maintain.
While developing a WordPress theme, I realized that we could use the previously described on this site Nette Form Class LibraryAdding a custom field form to a custom taxonomy project saves a lot of time compared to the official WordPress method, and managing and maintaining custom field forms becomes easier.
First, introduce Nette Form and create the form for editing custom fields
Nette Form supports Autoload, we have already installed this library through Composer, so we can use it directly. The method of creating the form is very simple, first get the value of the taxonomy custom field has been saved as the default value of the form for the user to edit, if you have not added the custom taxonomy custom field, the default value is empty, the user to add their own content can be. The following sample code can clearly illustrate the use of Nette Form to add the method of the form, more detailed library articles can be referred to Nette Form Official Documentation。
require_once( dirname( __FILE__ ) . '/... /vendor/autoload.php' );
use Nette\Forms\Form.
// Add custom fields to the edit category form
add_action( 'product_cat_edit_form_fields', 'edit_feature_group_field', 10, 2 );
function edit_feature_group_field( $term, $taxonomy ) {
$form = new Form;
// Modify the form output to fit the form structure in the WordPress backend
$renderer = $form->getRenderer();
$renderer->wrappers[ 'controls' ][ 'container' ] = '';
$renderer->wrappers[ 'pairs' ][ 'container' ] = 'tr';
$renderer->wrappers[ 'label' ][ 'container' ] = 'th'; $renderer->wrappers[ 'label' ][ 'container' ] = 'th';
$renderer->wrappers[ 'control' ][ 'container' ] = 'td';
// Get the form defaults
$hlf_url = get_term_meta( $term->term_id, 'hlf_url', true );
$hlf_text = get_term_meta( $term->term_id, 'hlf_text', true );;
$show_in_home = get_term_meta( $term->term_id, 'show_in_home', true );
// Add form fields and set default values
$form->addText( 'hlf_url', 'custom link:' )
->setAttribute( 'size', '80' )
->setDefaultValue( $hlf_url );
$form->addText( 'hlf_text', 'Display name:' )
->setAttribute( 'size', '80' )
->setDefaultValue( $hlf_text );
$form->addCheckbox( 'show_in_home', 'Show in home' )
->setDefaultValue( $show_in_home ); $form->addCheckbox( 'show_in_home', 'show in home' )
// Show the form
echo $form.
}
Save user-submitted custom field data
If a form that just adds taxonomy item custom field data doesn't save the user input to the database, then the form is pointless, and the way to save it is very simple. It's basically a matter of getting the data submitted by the user and then using the function provided by WordPress to update the custom fields of the taxonomy item. update_post_meta Just save the fetched data inside the database.
// Save taxonomy custom field data
add_action( 'edited_product_cat', 'update_feature_meta', 10, 2 );
function update_feature_meta( $term_id, $tt_id ) {
$hlf_url = $_POST[ 'hlf_url' ];
$hlf_text = $_POST[ 'hlf_text' ];
$show_in_home = sanitize_title( $_POST[ 'show_in_home' ] );
update_term_meta( $term_id, 'hlf_url', $hlf_url );
update_term_meta( $term_id, 'hlf_text', $hlf_text ); update_term_meta( $term_id, 'hlf_text', $hlf_text )
update_term_meta( $term_id, 'show_in_home', $show_in_home );
}
In addition to the direct use of the official function to save to the database, we can also do a pre-processing before the save, get the use of our own function to save the user to submit data to other data tables, there is a lot of room for us to play in this period of imagination. The above methods can also be used to add article custom fields, user-defined fields and increase the setup options form, there is a need for friends can try, I believe it will save a lot of development time to see the U.S. drama to put the girls and so on.