Dynamically set taxonomy term and show admin notice on post save
-
24-02-2021 - |
Question
I have a taxonomy of "date" listing all months of the year. And I have a custom field of "start_date". I would like to update the 'date' taxonomy dynamically so the user doesnt need to click both when setting up a post. So far my efforts have not shown results. What am I doing wrong?
/**
* Check value of date, set date taxonomy on post save, then show admin notice
* @param string $id post id
* @return void
*/
function trip_location_post_published_notification( $ID, $post ) {
// get acf value of start date
$date = get_field( 'start_date', $ID );
// get the month off the beginning of the string (ex. 11/01/2017), 11 is month
$moNum = explode('/',$date);
$moNum = $moNum[0];
$moList = array("January","February","March","April","May","June","July","August","September","October","November","December");
$monthName = $moList[$moNum];
// set taxonomy to this post ID
wp_set_object_terms($ID,$monthName,'date');
// setup notice message via WP_Error class?
if ( $date ) {
$notice = new WP_Error('date_notice','This trip location start date month is set to '.$monthName);
} else {
$notice = new WP_Error('date_notice','You did not set a trip start date.');
}
add_filter('redirect_post_location', function( $location ) use ( $notice) {
return add_query_arg( 'date_notice', $notice->get_error_code(), $location );
});
// now add an admin notice
add_action( 'admin_notices', 'tax_date_set_notice' );
function tax_date_set_notice(){
if ( array_key_exists( 'date_notice', $_GET) ) {
$notice = $_GET['date_notice'];
?>
<div class="update-nag notice is-dismissable">
<p><?php _e( $notice, 'mydomain' ); ?></p>
</div>
<?php
}
}
}
add_action( 'save_post_trip-location', 'trip_location_post_published_notification', 12, 2 );
Solution
Okay, so just in case anyone needs help on the same thing I did... here is my solution. Instead of a php/wordpress solution, I used javascript. Id love to see the php/wordpress solution if someone wants to post it.
<?php
// Admin Custom JS
add_action('admin_head', 'admin_custom_js');
function admin_custom_js() {
?>
<script>
// update date (month) taxonomy term on change of start date acf field
jQuery(function($){
var $startDateInput = $('div[data-name="start_date"] input');
if ($startDateInput.length) {
$startDateInput.on('change',function(){
var val = $(this).val();
var moList = ["January","February","March","April","May","June","July","August","September","October","November","December"];
if (val === ''){
} else {
var moNum = val.substr(0,2);
moNum = moNum.replace('0','');
moNum = Number(moNum) - 1;
var moName = moList[moNum];
var termVal = $('#datechecklist').find('label:contains("'+moName+'")').find('input').val();
var msg = 'Start date month set to '+moName;
$('#datechecklist input').prop('checked',false);
$('input[value="'+termVal+'"]').prop('checked',true);
$('div[data-name="start_date"] .acf-label p').remove();
$('div[data-name="start_date"] label').after('<p class="description" style="font-weight:bold;color: #0a81d2">'+msg+'</p>');
}
});
}
});
</script>
<?php
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange