Hi, I have read many answers about using “implode” to solve the issue about arrays and commas. Those where applied on a template file or code. But my case is that I am calling the value via [acf field="myfield"], which is inserted on a WordPress Post.
I was able to create and display the value, however, the outcome shows a comma right after each value. How can I remove it, or is there any other way to do it other than CheckBox? Here is an example of my output:
This is sentence one.,
This is sentence two.,
This is sentence three.
How can I remove those commas?
Thanks in advanced.
Russ
I want to remove the comma separator and make each name/object in the repeater field separated per line. Is that possible to do?
I would love to hear some thoughts on this one.
I have multiple blocks on which I have a custom field for adding width classes to the block (I don’t like Gutenbergs approach, I need my own). The selected class is appended to the blocks other regular css classes on the object.
On my ACF blocks I use get_block_wrapper_attributes() to get the classes and then add them to a wrapper div. Apparently these classes is also added to the preview wrapping div in admin which means I get a double effect of my css rules.
It removes the attribute classes on the inner div whenever clicking on the block but it’s back again whenever I reload the page.
Anyone got a clue on why this happens and how I can solve it (my soultion mught be bad, I’m of course open for other totally different solutions)? This worked fine, as far as I know, before the summer (just got back to work and discovered this after updating WP 6.2.2 -> 6.3. and ACF 6.1.7 -> 6.2.0).
Preferably I would not want these classes on the preview wrapping div, however they should in that case not be removed on the inner div when clicking on the preview.
My block code:
<?php
$custom_wrapper_classes = 'my-block';
include_once (get_stylesheet_directory() . '/includes/blocks/block-functions.php');
if (function_exists('hest_get_wrapper_attr')) :
$wrapper_attributes = hest_get_wrapper_attr($block, $custom_wrapper_classes);
endif;
$posts = get_field('my_relationship_field');
if( $posts ): ?>
<div <?php echo wp_kses_data($wrapper_attributes); ?>>
<div class="my-grid">
<?php
global $post;
foreach( $posts as $post):
setup_postdata($post);?>
<!-- Code for outputting data -->
<?php
endforeach; ?>
</div><!-- .my-grid -->
</div><!-- .wrapper -->
<?php
wp_reset_postdata();
endif; ?>
hest_get_wrapper_attr() looks like this:
<?php
/*
* Converts Gutenberg saved data attributes (input field by IPM plugin) to
* a string to echo on the wrapping div
*/
function conv_data_attributes($data_attributes) {
$data_attr = '';
// Regular expression pattern to match the value inside the brackets
$pattern = '/\[(.*?)\]/';
// Perform the regular expression match
if (preg_match_all($pattern, $data_attributes, $matches)) {
// Extract the matched values
$values = $matches[1];
// Remove the commas from each extracted value
$data_attr = array_map(function ($value) {
return str_replace(',', '', $value);
}, $values);
// Convert the array into a space-separated string
$data_attr = implode(' ', $data_attr);
}
return $data_attr;
}
/*
* Converts Gutenberg saved gap value to CSS
*
*/
function conv_blockgap_value($gap) {
$pattern = "/var:([^|]+)\|([^|]+)\|([^|]+)/";
$result = preg_replace_callback($pattern, function ($matches) {
$lastValue = $matches[3];
$updatedLastValue = preg_replace("/(\d+)([a-zA-Z]?)/", "$1-$2", $lastValue);
return "var(--wp--{$matches[1]}--{$matches[2]}--{$updatedLastValue})";
}, $gap);
$gap = "gap: " . $result . ";";
return $gap;
}
/*
* Main function to prepare all wrapper classes, styles and attributes
*
*/
function hest_get_wrapper_attr($block, $custom_wrapper_classes) {
$wrapper_attributes = '';
$alignment_classes = '';
$style = '';
// Support custom anchor values.
$anchor = '';
if ( ! empty( $block['anchor'] ) ) {
$anchor = 'id="' . esc_attr( $block['anchor'] ) . '" ';
$wrapper_attributes .= $anchor;
}
// Create class attribute allowing for custom "className" and "align" values.
if ( ! empty( $block['align'] ) ) {
$alignment_classes .= ' align' . $block['align'];
}
if ($block['supports']['alignContent'] == 'matrix') {
// If matrix
// Replace spaces: center left becomes center-left
$alignment_classes .= ' has-custom-content-position is-position-' . str_replace(" ", "-", $block['alignContent']);
} else {
if ( ! empty( $block['alignContent'] ) ) {
// If not matrix, get the alignContent
// either top, center, or bottom
$alignment_classes .= ' is-vertically-aligned-' . $block['alignContent'];
}
}
// Block text alignment
if( !empty($block['alignText']) ) {
$alignment_classes .= ' has-text-align-' . $block['alignText'];
}
// Check if the block has fullHeight turned on
if( !empty($block['fullHeight']) ) {
$style .= 'min-height: 100vh;';
}
// Check if we got custom data attributes to add to our main div
$data_attr = $block['dataAttributes'] ?? null;
if ($data_attr && function_exists('conv_data_attributes') ) :
$data_attr = conv_data_attributes($data_attr);
endif;
// Add blockGap value to wrapper attributes
$gap = $block['style']['spacing']['blockGap'] ?? null;
if ($gap && function_exists('conv_blockgap_value') ) :
$gap = conv_blockgap_value($gap);
endif;
$style .= $gap;
$wrapper_attributes .= get_block_wrapper_attributes(
[
'class' => $custom_wrapper_classes . $alignment_classes,
'style' => $style,
]
);
$wrapper_attributes = $wrapper_attributes . ' ' . $data_attr;
return $wrapper_attributes;
}
I’m using ACF Pro to create a Learning Management System. I’ve only recently started having problems. The main one that’s giving me the most trouble is that update_field()
is not actually updating the field, despite me giving it accurate information and it working on a local environment (but not a WP Engine Dev environment).
Currently I’m allowing the user to create a new Course (or “Module”), based on a previous one, specifically for their class. They choose which students they’ll be including, the Module to use a template, then the Chapters (or “Documents”) from that Module.
Then, on the final screen, they review information, and the “Submit” button starts through a series of AJAX calls that sends the information to the server, sends back the pertinent info, then moves on to the next step.
I’m sending back the results from each delete_field()
and update_field
command. When replacing values, I am deleting the current value (with delete_field()
) before using update_field(). This has helped some of my issues, but not all.
Here is the pertinent PHP code:
<?php
// I've made the CPT "Module" its own Class to contain its helper methods
$module = new ACF_Module( intval($module_id) );
// I store the Documents (or "Chapters") for each Module (or "Course") as a
// repeater field containing the document_id and the order it should
// appear within the Module.
$document_ids = json_decode( $_POST['document_ids']);
$documents_field = [];
$count = 1;
foreach( $document_ids as $document_id ) {
$documents_field[] = [
'document_id' => intval($document_id),
'order' => floatval($count),
];
$count++;
}
// In JS, $documents_field_deleted = (bool) true
$documents_field_deleted = delete_field($module->documents_field, $module->ID);
// In JS, $documents_field_updated = (int) 625785,, or a similarly large number,
// which does not correlate to ANY existing post ID or user ID, as the
// newest post_id in this example is 43113 and largest user ID is 1126
$documents_field_updated = update_field($module->documents_field, $documents_field, $module->ID);
I don’t know that this will matter, but just in case, here’s my JS doing the AJAX calls:
// Triggered when clicking submit button labelled "Next"
$('#submit').click(function(e){
e.preventDefault();
// I'm just storing values in hidden <code><input></code> fields. Since these
// classes are all taken on a job-site, where the student is
// monitored, I'm not concerned as much for the user being able to
// change these.
var nextAction = '';
actionStep = $('input[name="acf_action_step"]').val();
var payload = {
action: 'acf_action_function_name',
action_step: actionStep,
agency_id: parseInt($('input[name="acf_agency_id"').val()),
module_id: parseInt($('input[name="acf_module"]').val())
};
switch (actionStep) {
case 'module':
payload.module_title = $('input[name="acf_module_title"]').val();
nextAction = 'chapters';
break;
case 'chapters':
payload.chapter_ids = $('input[name="acf_chapters"]').val();
nextAction = 'user';
break;
case 'user':
var user_object = JSON.parse( $('input[name="acf_users"').val() );
nextAction = 'user';
if( user_object.length == 1 ) {
nextAction = 'finish';
}
payload.user_id = parseInt( user_object.shift() );
user_ids = JSON.stringify(user_object);
$('input[name="acf_users"]').val(user_ids);
break;
case 'finish':
$('#submit').prop('disabled', true);
$('#submit').addClass('disabled');
return;
default:
break;
}
$.ajax({
url: ajax_object.ajax_url,
type: 'post',
data: payload,
beforeSend: function(){
$('#submit').prop('disabled', true);
$('#submit').addClass('disabled');
},
success: function(response) {
var response_object = JSON.parse(response);
progress.append(response_object.message);
if( response_object.new_module_id ) {
$('input[name="acf_module"]').val( parseInt(response_object.new_module_id) );
}
},
complete: function(response) {
$('input[name="acf_action_step"]').val( nextAction );
$('#submit').prop('disabled', false);
$('#submit').removeClass('disabled');
}
});
return;
});
I am using the Short Text field in ACF, but when I add a number like, for example, $4,000 it displays as $4, 000, with a space after the comma. How can I remove that?
hello i got problem , i was using stackable to create some dynamics blocks
i using multiple checkbox as like photo
when i output as content will showing with “,” i want to change to <breank> or just blank
how i can do it ?
Hello, please tell me how can I remove the last comma in the photo output loop
it’s my loop code:
<img class="location__image" src="<?php echo get_the_post_thumbnail_url(get_the_ID() , 'medium_large'); ?>"
data-hover-slides='<?php $location_gallery_urls = get_field('location_gallery'); ?><?php if ($location_gallery_urls): ?><?php foreach (array_slice($location_gallery_urls, 0, 5) as $location_gallery_url): ?><?php echo esc_url($location_gallery_url); ?>, <?php
endforeach; ?><?php
endif; ?>'>
Hello.
I need help changing the output numbers 44, 22, 33, 55 to 44-22-33-55 or just remove the comma.
The result from the table column is generated using a shortcode plugin using the field name ‘winning-numbers-major’.
I make sure to group all the number field so I can display using the shortcode.
I appreciate any help. Thanks.
Hi everyone,
I know that the problem has often been asked on the forum but despite having tried everything I do not understand how to remove the commas on the front when using checkboxes.
I am not a developer and I have trouble understanding how to do it.
Could someone help me? 🙂
Thanks in advance !
Hi there,
I’m sure this is quite trivial for your but I’m not a coder eh eh…
I’ve manage to achieve what I want but I think it can be improved.
I have a checkbox field, and I want to retrieve both label and value and want to separate them with a comma. This part is working fine. However, I want to remove the comma from the last item. I can add the comma in a span and then target the last span with CSS and remove it but I want it to be better.
This is what I came up with by reading the docs and searching other topics
<?php
$plans = get_field('plans');
if ( $plans) : ?>
<div>
<?php foreach( $plans as $plan ): ?>
<a target="_blank" title="<?php echo $plan['label']; ?>" href="<?php echo $plan['value']; ?>"><?php echo $plan['label']; ?></a><span>,</span>
<?php endforeach; ?>
</div>
<?php endif; ?>
How To remove commas when using checkboxes
I am trying to insert some custom tags for each product in my website. I use checkboxes in order to select tags and display on frontend. But the tags are displayed each item seperated with a comma (,). Can someone advice to how to remove this?
hello, I’m making a website for a real estate company, and I’m registering the leisure areas, such as soccer fields, gyms, pet places, these things … but when I look at the website, these elements are separated by commas and I want to remove these commas, is there any way other than by PHP? I don’t know anything about programming and I need to remove these commas. If the only solution is for PHP, send me the code already ready and the directory where I must insert this code.
Advanced Custom Fields PRO
Version 5.9.5
I have created a category with this code
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'products', 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'product', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( 'Products', 'twentythirteen' ),
'parent_item_colon' => __( 'Parent Product', 'twentythirteen' ),
'all_items' => __( 'All products', 'twentythirteen' ),
'view_item' => __( 'View Product', 'twentythirteen' ),
'add_new_item' => __( 'Add New Product', 'twentythirteen' ),
'add_new' => __( 'Add New', 'twentythirteen' ),
'edit_item' => __( 'Edit Product', 'twentythirteen' ),
'update_item' => __( 'Update Product', 'twentythirteen' ),
'search_items' => __( 'Search Product', 'twentythirteen' ),
'not_found' => __( 'Not Found', 'twentythirteen' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentythirteen' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'products', 'twentythirteen' ),
'description' => __( 'product news and reviews', 'twentythirteen' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields','tags' ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
// Registering your Custom Post Type
register_post_type( 'products', $args );
register_taxonomy('categories', array('products'), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => array( 'slug' => 'products', 'with_front'=> false )));
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
//create two taxonomies, genres and tags for the post type "tag"
function create_tag_taxonomies()
{
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
'search_items' => __( 'Search Tags' ),
'popular_items' => __( 'Popular Tags' ),
'all_items' => __( 'All Tags' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Tag' ),
'update_item' => __( 'Update Tag' ),
'add_new_item' => __( 'Add New Tag' ),
'new_item_name' => __( 'New Tag Name' ),
'separate_items_with_commas' => __( 'Separate tags with commas' ),
'add_or_remove_items' => __( 'Add or remove tags' ),
'choose_from_most_used' => __( 'Choose from the most used tags' ),
'menu_name' => __( 'Tags' ),
);
register_taxonomy('tag','products',array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'tag' ),
));
}
add_action( 'init', 'create_tag_taxonomies', 0 );
How can I display the list of posts of that taxonomy using the ÙŽACF plugin’s taxonomy feature?
Multiple Select Field – remove commas on elementor?
Hi!
I use this code with my checkbox, but would like to remove the commas.
`
<p class=”shop-link”><?php the_field(‘shop’); ?></p>
`
I’ve searched the forum and tried different codes without any luck.
For example, this didn’t work so I appreciate all help!
`
<p class=”shop-link”><?php $values = get_field(‘checkbox’);
if ($values) {
foreach ($values as $value) {
echo str_replace(‘, ‘, ‘<br />’, $value);
}
} the_field(‘shop’); ?></p>
`
Thank you,
Caroline
I have an ACF repeater field field_5f71d4eaaf381
for email addresses and I want to output a comma separate list of the sub field values (measurement_extra_email_address) to create a $to
for the WordPress mail function.
$values = $_POST['acf'];
$extra_recipients = $_POST['acf']['field_5f71d4eaaf381'];
if ( $extra_recipients ) {
$list = array();
foreach( $extra_recipients as $extra_recipient ) {
$list[] = $extra_recipient['measurement_extra_email_address'];
}
$to = implode(',', $list);
}
I thought above code should/would do it but no email is being send so I guess the code is incorrect..?
Just a note: When I remove above part of the code and just try $to = '[email protected]';
an email is being send so the error should be in there…
Hello
I’ve been using ACF and a few things have got me stumped.
1st
I have a site setup in elementor and have tried using the URL and Link fields in ACF to allow users to put in a website domain in a field for a member profile.
However when ever it displays via ACF shortcode, the url is not linkable, how do i make it link to the URL thats been put in the ACF field?
2nd
https://www.cider.ordesign.me/member/westons/
How do i remove the commas between the icons, under whats on offer?
Thanks
Hi, when sending a reply, my post stopped working. https://support.advancedcustomfields.com/forums/topic/remove-the-comma-in-the-line-break/#post-134784
I would like to be able to remove (,) every time a newline occurs in the labels field.
Can you help me, please?
Thank you
<h6 class="audio-tags">
<?php
$tags = get_field("etiquetas");
$tagArray = explode(",", $tags);
foreach($tagArray as $tag){
echo("<div class='tag-wrap'>".$tag."</div>");
}
?>
</h6>