Skip to content

Instantly share code, notes, and snippets.

@tareq1988
Last active January 7, 2017 00:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tareq1988/809ed96eaaf0ce5480e0 to your computer and use it in GitHub Desktop.
Save tareq1988/809ed96eaaf0ce5480e0 to your computer and use it in GitHub Desktop.
WPUF Action Hook Taxonomy
<?php
/**
* Register a custom taxonomy
*
* @return void
*/
function prefix_register_genere_taxonomy() {
register_taxonomy( 'genere', 'post', array(
'label' => __( 'Genre' ),
'rewrite' => array( 'slug' => 'genre' ),
'hierarchical' => true,
) );
}
add_action( 'init', 'prefix_register_genere_taxonomy' );
/**
* Add the input field to the form
*
* @param int $form_id
* @param null|int $post_id
* @param array $form_settings
*/
function prefix_render_genere_field( $form_id, $post_id, $form_settings ) {
$selected = 0;
if ( $post_id ) {
$terms = wp_get_post_terms( $post_id, 'genere', array( 'fields' => 'ids' ) );
$selected = ( is_array( $terms ) && count( $terms ) ) ? $terms['0'] : 0;
}
?>
<div class="wpuf-label">
<label><?php _e( 'Genere', 'wpuf' ); ?></label>
</div>
<div class="wpuf-fields">
<?php wp_dropdown_categories( array(
'show_option_none' => __( '- Select - ', 'wpuf' ),
'taxonomy' => 'genere',
'hide_empty' => false,
'name' => 'genere',
'selected' => $selected
) ); ?>
</div>
<?php
}
add_action( 'my_awesome_hook', 'prefix_render_genere_field', 10, 3 );
/**
* Update the custom field when the form submits
*
* @param type $post_id
*/
function prefix_update_genere_field( $post_id ) {
if ( isset( $_POST['genere'] ) ) {
wp_set_post_terms( $post_id, $_POST['genere'], 'genere' );
}
}
add_action( 'wpuf_add_post_after_insert', 'prefix_update_genere_field' );
add_action( 'wpuf_edit_post_after_update', 'prefix_update_genere_field' );
@Wolkex3
Copy link

Wolkex3 commented Jan 7, 2017

Thank you for your git.

I have a problem. Could you take a look pls?

http://wordpress.stackexchange.com/questions/251610/wp-set-post-terms-custom-taxonomy-doesnt-work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment