Skip to content

Instantly share code, notes, and snippets.

@vfontjr
Last active April 1, 2020 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfontjr/e5526352c2140cecb446445cfbfaa064 to your computer and use it in GitHub Desktop.
Save vfontjr/e5526352c2140cecb446445cfbfaa064 to your computer and use it in GitHub Desktop.
<?php
add_action( 'frm_after_create_entry', 'create_new_cpt_taxonomy', 30, 2 );
function create_new_cpt_taxonomy( $entry_id, $form_id ) {
/* first thing to do is to make sure this runs on the correct form */
if( $form_id == 32 ) { //change 32 to the ID of your form
if( isset( $_POST['item_meta'][254] ) ) { //change 254 to the ID of the field used to add a category
$taxonomy = 'city'; //if you're using a custom taxonomy, change this to the correct name
$term = $_POST['item_meta'][254];
/* first retrieve the entry object */
$entry = FrmEntry::getOne( $entry_id );
/* if there's no post_id, exit the function. There's nothing for us to do */
if ( ! $entry->post_id ) {
return;
} else {
/* make the association */
wp_set_object_terms( $entry->post_id, $term, $taxonomy, true );
}
}
}
}
<?php
add_action( 'frm_after_create_entry', 'create_new_cpt_taxonomy', 30, 2 );
function create_new_cpt_taxonomy( $entry_id, $form_id ) {
switch( $form_id ) {
case 32 :
if( isset( $_POST['item_meta'][254] ) ) {
$term = $_POST['item_meta'][254];
$taxonomy = 'city';
process_new_cpt_taxonomy( $entry_id, $taxonomy, $term );
}
break;
case xx : // second form id
if( isset( $_POST['item_meta'][xxx] ) ) {
$term = $_POST['item_meta'][xxx];
$taxonomy = 'city';
process_new_cpt_taxonomy( $entry_id, $taxonomy, $term );
}
break;
}
}
function process_new_cpt_taxonomy( $entry_id, $taxonomy, $term ) {
/* first retrieve the entry object */
$entry = FrmEntry::getOne( $entry_id );
/* if there's no post_id, exit the function. There's nothing for us to do */
if ( ! $entry->post_id ) {
return;
} else {
/* make the association */
wp_set_object_terms( $entry->post_id, $term, $taxonomy, true );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment