Add custom fields depending on the category
-
What I try to do is to create a custom field that only show up if a specific event categorie has been used.
I’m having a really hard time following the docs at Custom Field API. So far I found out that you would need to create a folder in /wp-content/plugins/<new folder> and create a new php file. Afterwards you need to activate this plugin in wordpress.
Here is my code so far (currently all fields are set to global):
<?php /* Plugin Name: My Tickets: Custom Fields Plugin URI: https://www.joedolson.com/my-tickets/ Description: Custom Fields in My Tickets Version: 1.0.0 Author: Daniel Klauck Author URI: https://www.joedolson.com/ */ /* Copyright 2014-2018 Joseph C Dolson (email : [email protected]) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ add_filter( 'mt_custom_fields', 'create_custom_fields', 10, 1 ); function create_custom_fields( $array ) { // Other fields: sanitize callback; input type; input values; display_callback $array['message_to_organizer'] = array( 'title'=>"Nachricht an den Veranstalter:", 'sanitize_callback'=>'sanitize_callback', 'display_callback'=>'display_callback', 'input_type'=>'input', 'context'=> 'global' ); $array['motocycle_type'] = array( 'title'=>"Welches Motorrad f?hrst du?:", 'sanitize_callback'=>'sanitize_callback', 'display_callback'=>'display_callback', 'input_type'=>'input', 'context'=> 'global' ); $array['drivers_experience'] = array( 'title'=>"Meine Off Road Erfahrung:", 'sanitize_callback'=>'sanitize_callback', 'display_callback'=>'display_callback', 'input_type'=>'select', 'input_values'=>array( 'keine Off Road Erfahrung', 'wenig Off Road Erfahrung', 'viel Off Road Erfahrung' ), 'context'=> 'global' ); return $array; } /* Display callback formats the saved data. $context is 'payment' or 'cart', depending on whether it appears in an admin payment or the user's shopping cart. */ function display_callback( $data, $context='payment' ) { return urldecode( $data ); } /* Sanitize callback cleans the data before saving to the DB */ function sanitize_callback( $data ) { return esc_sql( $data ); } ?>
I would like to set “motocycle_type” and “drivers_experience” to only show up if the event category is set to “Off-Road Trainings”.
The page I need help with: [log in to see the link]
- The topic ‘Add custom fields depending on the category’ is closed to new replies.