{"id":1959558,"date":"2024-03-12T09:30:40","date_gmt":"2024-03-12T13:30:40","guid":{"rendered":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/?p=1959558"},"modified":"2024-05-02T07:42:43","modified_gmt":"2024-05-02T11:42:43","slug":"how-to-add-custom-data-to-csv-imports","status":"publish","type":"post","link":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/how-to-add-custom-data-to-csv-imports\/","title":{"rendered":"How to Add Custom Data to CSV Imports"},"content":{"rendered":"\n<iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/O0qNWjLVxb8?si=SKsHSMOIpIgk_26h\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n\n\n\n<p>The <a href=\"https:\/\/staging.theeventscalendar.com\/knowledgebase\/importing-data-from-a-csv-file\/\">CSV importer tool<\/a> of The Events Calendar has a lot of pre-set fields that you can use. They cover all the basic data, that you would need to add events to your calendar.<\/p>\n\n\n\n<p>If you have some custom data that is not included in the basic set, then &#8211; like for most things &#8211; there is a snippet you can use.<\/p>\n\n\n\n<p>In the below example we add three columns. One for the event status, and two more for custom data.<\/p>\n\n\n\n<p>The only thing you need to do is define the meta key and the label for the data in the <code>$custom_import_data<\/code> array. The snippet takes care of the rest.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nclass TEC_CSV_Import_Custom_Metadata {\n\t\/**\n\t * Define your custom data here.\n\t * key   =&gt; the meta key that will be used to save the data\n\t * value =&gt; The label of the data used for column mapping\n\t *\n\t * @var string&#x5B;]\n\t *\/\n\tpublic $custom_import_data = &#x5B;\n\t\t&#039;_tribe_events_status&#039; =&gt; &#039;Event Status&#039;,\n\t\t&#039;meta_key_1&#039;           =&gt; &#039;Meta Data 1&#039;,\n\t\t&#039;meta_key_2&#039;           =&gt; &#039;Meta Data 2&#039;,\n\t];\n\n\t\/**\n\t * Constructor.\n\t *\/\n\tpublic function __construct() {\n\t\tadd_filter( &#039;tribe_events_importer_event_column_names&#039;, &#x5B; $this, &#039;add_column_name&#039; ] );\n\n\t\tadd_filter( &#039;tribe_events_csv_import_event_additional_fields&#039;, &#x5B; $this, &#039;process_custom_data&#039; ] );\n\n\t\tadd_action( &#039;tribe_events_update_meta&#039;, &#x5B; $this, &#039;save_custom_data&#039; ], 10, 3 );\n\t}\n\n\t\/**\n\t * Add the field name to the dropdown.\n\t *\n\t * @param array&lt;string|string&gt; $column_names An array of column names for event import.\n\t *\n\t * @return string&#x5B;]\n\t *\/\n\tpublic function add_column_name( $column_names ) {\n\t\tforeach ( $this-&gt;custom_import_data as $key =&gt; $value ) {\n\t\t\t$column_names&#x5B; $key ] = $value;\n\t\t}\n\n\t\treturn $column_names;\n\t}\n\n\t\/**\n\t * Make sure that the added column gets processed on import.\n\t *\n\t * @return array\n\t *\/\n\tfunction process_custom_data() {\n\t\t\/\/ key (meta field name, table column name) =&gt; CSV column ID, the key from the above function.\n\t\t$extra_fields&#x5B; &#039;tec_import_event_status&#039; ] = &#039;tec_import_event_status&#039;;\n\n\t\tforeach ( $this-&gt;custom_import_data as $key =&gt; $value ) {\n\t\t\t$extra_fields&#x5B; $key ] = $key;\n\t\t}\n\n\t\treturn $extra_fields;\n\t}\n\n\t\/**\n\t * Save metadata in database.\n\t *\n\t * @param int     $event_id The event ID we are modifying meta for.\n\t * @param array   $data     The meta fields we want saved.\n\t * @param WP_Post $event The event itself.\n\t *\n\t * @return void\n\t *\/\n\tfunction save_custom_data( $event_id, $data, $event ) {\n\t\tforeach ( $this-&gt;custom_import_data as $key =&gt; $value ) {\n\t\t\tif ( isset( $data&#x5B; $key ] ) &amp;&amp; $data&#x5B; $key ] != &#039;&#039; ) {\n\t\t\t\tupdate_post_meta( $event_id, $key, esc_html( $data&#x5B; $key ] ) );\n\t\t\t}\n\t\t}\n\t}\n}\n\nnew TEC_CSV_Import_Custom_Metadata();\n<\/pre><\/div>\n\n\n<p>Note, that the Event Status in this example is hooked up to the event status dropdown that comes with The Events Calendar. This supports the following 3 statuses out of the box, which are also supported by <a href=\"https:\/\/schema.org\/EventStatusType\" target=\"_blank\" rel=\"noreferrer noopener\">schema.org<\/a>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Scheduled (default)<\/li>\n\n\n\n<li>Canceled<\/li>\n\n\n\n<li>Postponed<\/li>\n<\/ul>\n\n\n\n<p>If you&#8217;d like to set up custom event statuses, then check out <a href=\"https:\/\/staging.theeventscalendar.com\/knowledgebase\/adding-a-custom-event-status\/\" target=\"_blank\" rel=\"noreferrer noopener\">this article<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The CSV importer tool of The Events Calendar has a lot of pre-set fields that you can use. They cover all the basic data, that you would need to add events to your calendar. If you have some custom data that is not included in the basic set, then &#8211; like for most things &#8211;&#8230;<\/p>\n","protected":false},"author":17,"featured_media":1955565,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_swpsp_post_exclude":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"ep_exclude_from_search":false,"footnotes":""},"categories":[24,59,79],"tags":[229,45],"stellar-product-taxonomy":[161],"class_list":["post-1959558","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-customizing","category-customizing-resources","category-snippets","tag-csv","tag-importing","stellar-product-taxonomy-the-events-calendar"],"acf":[],"taxonomy_info":{"category":[{"value":24,"label":"Customizations"},{"value":59,"label":"PHP &amp; Functions"},{"value":79,"label":"Snippets"}],"post_tag":[{"value":229,"label":"CSV"},{"value":45,"label":"Importing"}],"stellar-product-taxonomy":[{"value":161,"label":"The Events Calendar"}]},"featured_image_src_large":["https:\/\/images.theeventscalendar.com\/kb\/uploads\/2023\/02\/social-share-1024x538.png",1024,538,true],"author_info":{"display_name":"Andras Guseo","author_link":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/author\/andras\/"},"comment_info":0,"category_info":[{"term_id":24,"name":"Customizations","slug":"customizing","term_group":0,"term_taxonomy_id":24,"taxonomy":"category","description":"","parent":0,"count":110,"filter":"raw","term_order":"0","cat_ID":24,"category_count":110,"category_description":"","cat_name":"Customizations","category_nicename":"customizing","category_parent":0},{"term_id":59,"name":"PHP &amp; Functions","slug":"customizing-resources","term_group":0,"term_taxonomy_id":59,"taxonomy":"category","description":"","parent":24,"count":101,"filter":"raw","term_order":"0","cat_ID":59,"category_count":101,"category_description":"","cat_name":"PHP &amp; Functions","category_nicename":"customizing-resources","category_parent":24},{"term_id":79,"name":"Snippets","slug":"snippets","term_group":0,"term_taxonomy_id":79,"taxonomy":"category","description":"","parent":0,"count":136,"filter":"raw","term_order":"0","cat_ID":79,"category_count":136,"category_description":"","cat_name":"Snippets","category_nicename":"snippets","category_parent":0}],"tag_info":[{"term_id":229,"name":"CSV","slug":"csv","term_group":0,"term_taxonomy_id":229,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw","term_order":"0"},{"term_id":45,"name":"Importing","slug":"importing","term_group":0,"term_taxonomy_id":45,"taxonomy":"post_tag","description":"","parent":0,"count":35,"filter":"raw","term_order":"0"}],"_links":{"self":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1959558","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=1959558"}],"version-history":[{"count":4,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1959558\/revisions"}],"predecessor-version":[{"id":1959919,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1959558\/revisions\/1959919"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/media\/1955565"}],"wp:attachment":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=1959558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=1959558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=1959558"},{"taxonomy":"stellar-product-taxonomy","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/stellar-product-taxonomy?post=1959558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}