I use ACF Pro google maps.
The Plugin stores the map data in an array. So far so good.
Now, I like to run through the array and directly store the date in new custom fields.
This works, but I like to give the fields my own name, not the names from the ARRAY (lat & lng).
Can anyone explain how I can use my own names for the custom fields spot_lat & spot_lng?
function my_copy_date_filter($post_id)
{
$post_type = get_post_type($post_id);
if ($post_type != 'spot') {
return;
}
$date = get_field('spot_location', $post_id);
if ($date) {
$lat_lng = [];
foreach (array('lat', 'lng') as $i => $k) {
if (isset($date[$k])) {
update_post_meta($post_id, $k, $date[$k]);
}
}
}
}
add_filter('acf/save_post', 'my_copy_date_filter', 20);
Thanks for any suggestion,
Denis
Create 2 new acf fields named spot_lat and spot_lng
Then try this code
function my_copy_date_filter($post_id) {
$post_type = get_post_type($post_id);
if ($post_type != 'spot') {
return;
}
$date = get_field('spot_location', $post_id);
if( $date ) {
update_field( 'spot_lat', $date[ 'lat' ], $post_id );
update_field( 'spot_lng', $date[ 'lng' ], $post_id );
}
}
add_filter('acf/save_post', 'my_copy_date_filter', 20);
Related
I'm currently trying to modify the name column in the WordPress admin dashboard. I've tried this code here but it's not working:
add_action('manage_users_custom_column', 'modify_users_column_content', 10, 3 );
function modify_users_column_content( $value, $column_name, $user_id ) {
if ( $column_name === 'name' ) {
$value .= '<span> |</span>';
}
return $value;
}
When I error_log the column_name parameter, I get only the last two columns from the user management plugin UltimateMember:
The first columns are not within the array. I've tried to understand it but no chance. I don't get it.
The first columns are not within the array. I've tried to understand
it but no chance. I don't get it.
Because the manage_users_custom_column filter is meant to be used to generate the output of a custom column and not default columns like the "Name" column.
However, you can achieve what you want by replacing the default "Name" column (keyed name) with a custom one like so:
add_filter( 'manage_users_columns', function( $columns ){
$columns2 = [];
// We could do $columns['name2'] = 'Name'; - but we are replacing a column.
foreach ( $columns as $key => $label ) {
if ( 'name' === $key )
$columns2['name2'] = 'Name';
else
$columns2[ $key ] = $label;
}
return $columns2;
} );
And then use the manage_users_custom_column filter to generate the output which is displayed in the custom column (name2):
add_filter( 'manage_users_custom_column', function( $output, $column_name, $user_id ){
if ( 'name2' === $column_name ) {
$user_object = get_userdata( $user_id );
$name = trim( $user_object->first_name . ' ' . $user_object->last_name );
$output = $name ? $name . '<span> |</span>' : '—'; // the custom output
}
return $output;
}, 10, 3 );
Here is a simple example of what I am trying to achieve:
I have a gravity form with different sections that will be shown conditionally by pre-populating dynamically. The thing is that I can't seem to populate them based on my ACF data (which are checkboxes as well).
If I put the values into the code it works like this:
add_filter( 'gform_pre_render_2', 'my_populate_checkbox' );
function my_populate_checkbox( $form ) {
foreach( $form['fields'] as &$field ) {
if( 11 === $field->id ) {
foreach( $field->choices as &$choice ) {
if( 'mychoice' === $choice['value'] || 'anotherchoice' === $choice['value'] ) {
$choice['isSelected'] = true;
}
}
}
}
return $form;
}
To get it to be populated dynamically I was trying something like this which didn't work (not that good with php):
add_filter( 'gform_pre_render_2', 'my_populate_checkbox' );
function my_populate_checkbox( $form ) {
foreach( $form['fields'] as &$field ) {
if( 11 === $field->id ) {
foreach( $field->choices as &$choice ) {
$addons = get_field('addons');
if( $addons === $choice['value'] ) {
$choice['isSelected'] = true;
}
}
}
}
return $form;
}
It's not working and I know I am missing something here but can't figure out what it is:/ Any help or pointers would be highly appreciated! I tried keeping it precise but if any more information is required please let me know and I will update the post accordingly.
Figured it out with some help:) In case anybody needs this functionality, here is how it works:
add_filter( 'gform_pre_render_2', 'my_populate_checkbox' );
function my_populate_checkbox( $form ) {
global $post;
$adfields = get_field( 'addons', get_the_ID() );
foreach( $form['fields'] as &$field ) {
if( 11 === $field->id ) {
foreach( $field->choices as &$choice ) {
if( in_array( $choice['value'] ,$adfields )) {
$choice['isSelected'] = true;
}
}
}
}
return $form;
}
I would like to add a dynamic value to a custom field in wordpress.
I do not want to use update_post_meta() because it is a dynamic value that I want to add every time when the post is loaded ..
The custom field is tm_video_file, and retrieved with get_post_meta().
This will return a URL, I beed to append a query string to it.
I am trying to hook a function that will change the value of "tm_video_file", but without success.
// URL will be called as:
get_post_meta($post_id, 'tm_video_file', true);
// I try to add a filter for these meta data
add_filter('get_post_metadata', array($this,'add_hash_key'),1,4);
// And try to append the tm_video_file value here:
public function add_hash_key ($meta_value, $post_id, $meta_key, $single ) {
if($meta_key == 'tm_video_file') {
var_dump($meta_value); // ALWAYS NULL ???
var_dump($post_id);
var_dump($meta_key);
var_dump($single);
}
return $meta_value.'?hash=something-dynamic-here';
}
UPDATE:
I have found out some information on the net, and I think I am a bit closer to the solution:
public static function add_hash_key($meta_value, $object_id, $meta_key, $single) {
if ($meta_key == 'tm_video_file') {
$meta_type = 'post';
$meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
if ( !$meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
$meta_cache = $meta_cache[$object_id];
}
if ( isset($meta_cache[$meta_key]) ) {
if ( $single ) {
$meta_value = maybe_unserialize( $meta_cache[$meta_key][0] );
} else {
$meta_value = array_map('maybe_unserialize', $meta_cache[$meta_key]);
}
}
// At this point $meta_value contains the right data!
$meta_value .= '?Hash-Here';
return array($meta_value); //
}
}
In wordpress file meta.php there is a function called:
get_metadata()
This will apply filters, and returns the value in the end:
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
if ( null !== $check ) {
if ( $single && is_array( $check ) )
return $check[0];
else
return $check;
}
//For some reason $check is ALWAYS null .. ? But the filter function did run for sure..
I've noticed a strange behaviour of one of my functions. Just take a look. Using first function I'm displaying some custom fields data in the backend:
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION', 10 );
function MY_COLUMNS_FUNCTION( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns['order_actions'] );
//edit this for you column(s)
//all of your columns will be added before the actions column
$new_columns['product_name'] = 'Product';
$new_columns['authors_income'] = 'Author';
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION( $column ) {
global $post;
$order = new WC_Order( $post->ID );
$items = $order->get_items();
//start editing, I was saving my fields for the orders as custom post meta
//if you did the same, follow this code
if ( $column == 'authors_income' ) {
foreach ( $items as $item ) {
echo $item['PLN-dla-autora'];
echo ' zł';
}
}
if ( $column == 'product_name' ) {
foreach ( $items as $item ) {
echo $item['name'];
}
}
}
add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
function MY_COLUMNS_SORT_FUNCTION( $columns ) {
$custom = array(
//start editing
'authors_income' => 'PLN-dla-autora',
'product_name' => 'name'
//stop editing
);
return wp_parse_args( $custom, $columns );
Everything works there like a charm. Then I have second function, which somehow does the same, yet it will print the data into the CSV file. Unfortunatelly, this code doesnt work (it display only column header, no data included). I dont see any major differences between those functions so why is that?
function wc_csv_export_modify_column_headers( $column_headers ) {
$new_headers = array(
'author_income' => 'PLN dla autora',
// add other column headers here in the format column_key => Column Name
);
return array_merge( $column_headers, $new_headers );
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'wc_csv_export_modify_column_headers' );
// set the data for each for custom columns
function wc_csv_export_modify_row_data( $order_data, $order ) {
$custom_data = array(
'author_income' => get_post_meta( $order->id, 'PLN dla autora', true ),
// add other row data here in the format column_key => data
);
return array_merge( $order_data, $custom_data );
}
add_filter( 'wc_customer_order_csv_export_order_row', 'wc_csv_export_modify_row_data', 10, 2 );
First off, I'm using advanced custom fields plugin, and I'm using a repeater. I have a custom post type that triggers when pressing "publish" when creating a new post, what it does, is that it adds a row to my repeater field.
Basiclly it takes the info from other fields and comes up with the proper information that the row created must have by making some calculations.
this is my function:
function investment_save($post_id)
{
$post = get_post();
$poststat = get_post_status($post_id);
$fecha = current_time(d-M-Y);
$fecha2 = "Inversión ".$fecha;
if($poststat == "publish" && !have_rows('datos_especificos'))
{
remove_action( 'save_post', 'investment_save' );
$my_post = array(
'ID' => $post_id,
'post_title' => $fecha2,
);
wp_update_post( $my_post );
$fech = get_field('fecha_de_inicio_de_la_inversion');
$sald = get_field('monto_de_la_inversion');
$tasa = get_field('tasa_de_interes');
$cap = get_field('uso_de_interes');
$elsald0 = $tasa/12*0.01*$sald;
$elsald=number_format($elsald0,2);
if($cap=="pagar")
{
$cap2=$elsald;
$cantr=0;
}else{
$cap2=0;
$cantr=$elsald;
}
$sf = $sald+$cantr;
$field_key = "datos_especificos";
$value = get_field($field_key, $post_id);
$value[] = array("fecha" => $fech,
"saldo" => $sald,
"inversion_en_el_periodo" => "0",
"interes_causado_en_el_periodo" => $elsald,
"cantidad_pagada" => $cap2,
"cantidad_reinvertida" => $cantr,
"saldo_final" => $sf);
update_field( $field_key, $value, $post_id );
}
}
add_action( 'save_post', 'investment_save' );
This works properly, however, I have a new task in which everytime I press update, I must check if $cr+$cp equal $icelp in all rows, if they not, i must adjust the values from them, so this is the kind of code I'd like to use:
$sum = $cp + $cr;
if($sum>$icelp)
{
$dif=$sum-$icelp;
if($cp>=$cr)
{
$cp=$cp-$dif;
}else
{
$cr=$cr-$dif;
}
}
if($sum<$icelp)
{
$dif=$icelp-$sum;
if($cp>=$cr)
{
$cr=$cr+$dif;
}else
{
$cp=$cp+$dif;
}
}
Ive tried using an"else", also using an "if", but I just can't make it work. Please Someone help me I've been breaking my head for days