I have this code to get the value for custom field myCustomField which works fine:
get_post_meta( get_the_ID(), 'myCustomField', true )
This code gets the value stored in this field.
But now I need a code to dynamically get and echo the Custom field label (not the stored value) which is "My Custom Field".
This code will be fired for this action:
add_action('woocommerce_product_meta_start' , 'add_in_product_page');
If you've created your custom field using ACF plugin, then what you need is the field object. You could call get_field_object function in order to get the object and then find the label returned in the object, like so:
$your_field_name = "your_custome_field_name";
$your_field_object = get_field_object($your_field_name); // You could also pass the field key instead of field name
echo $your_field_object['label'];
echo "<br>";
echo $your_field_object['value'];
echo "<br>";
echo $your_field_object['key'];
echo "<br>";
echo $your_field_object['type'];
echo "<br>";
echo $your_field_object['ID'];
You could also read more about this function on the documentation page:
ACF get_field_object function
UPDATE
Translating the returned label to your local language!
I would usually use these two filter hooks: gettext and ngettext.
You could read more about these on the documentation page:
WordPress gettext filter hook
WordPress ngettext filter hook
So the translation would be something like this:
$awesome_label = $your_field_object['label'];
add_filter('ngettext', 'your_theme_custom_translation', 20, 3);
add_filter( 'gettext', 'your_theme_custom_translation', 20, 3 );
function your_theme_custom_translation( $translated, $text, $domain ) {
if ( ! is_admin() ) {
if ( $awesome_label == $translated ){
$translated = 'etiqueta impresionante'; // This is the place where you could type anything you want in your local language to replace the default english label
}
}
return $translated;
}
Related
The MailChimp for WooCommerce plugin is great, but I've just been given a design where the 'Subscribe to our newsletter' checkbox in the WooCommerce checkout is immediately after the email field rather than at the bottom of a particular section:
The plugin allows you to type in an action hook for where you'd like the checkbox to appear; for example woocommerce_checkout_before_customer_details, woocommerce_after_checkout_billing_form, etc. which allows you to put the checkbox before or after a certain section (as long as there's a hook for it), but not in amongst the various fields.
Is there a way to add the MailChimp checkbox to very specific point before or after a certain field?
The checkout fields are outputted within a loop, which uses the woocommerce_form_field() function to echo out each field:
$fields = $checkout->get_checkout_fields( 'billing' );
foreach ( $fields as $key => $field ) {
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
}
this function has a number of filters for each specific field type (e.g. woocommerce_form_field_email), but no action hooks.
However, you can use one of these filters and just a little trickery to add your own hooks:
///
// Add MailChimp checkbox after email field
///
function vnm_mc_after_email_field($field, $key, $args, $value) {
ob_start();
do_action('vnm_before_email_field');
echo $field;
do_action('vnm_after_email_field');
return ob_get_clean();
}
add_filter('woocommerce_form_field_email', 'vnm_mc_after_email_field', 10, 4);
Since a WordPress filter always has to return a version of the first parameter sent to it (in this case $field), all we do is open the output buffer, add our own actions for before and after the field (vnm_before_email_field, vnm_after_email_field), and then return the output buffer.
Then, in the MailChimp plugin settings under WooCommerce->MailChimp->Audience, just add the hook you've just created for your field:
...and done. Note you could in fact add action hooks around every WooCommerce checkout field if you wanted, just by adding the following (untested):
///
// Add `before` & `after` actions around every WooCommerce checkout field
///
function vnm_wc_form_field_actions($field, $key, $args, $value) {
ob_start();
do_action('vnm_before_' . $args['type'] . '_field');
echo $field;
do_action('vnm_after_' . $args['type'] . '_field');
return ob_get_clean();
}
add_filter('woocommerce_form_field', 'vnm_wc_form_field_actions', 10, 4);
I want to display the value from an Advanced Custom Field (ACF) in Woocommerce and I am using this code in this function hooked:
add_action( 'woocommerce_single_product_summary', 'charting', 20 );
function charting() {
if( get_field('size_chart') ) {
echo '<p>size guide</p>';
}
return;
}
But it is not working, it is displaying the custom field value above the href (size guide) and the href is empty like this:
size guide
Your problem is that you can't use echo with ACF the_field('my_field') , because when using the_field('my_field') is just like using echo get_field('my_field'), so you are trying to echo an echo. Instead use get_field('my_field') this way in your code:
add_action( 'woocommerce_single_product_summary', 'charting', 20 );
function charting() {
if( !empty( get_field('size_chart') ) ) { // if your custom field is not empty…
echo '<p>size guide</p>';
}
return;
}
After, I have add empty() function in your condition…
You can also try to return it instead of echo:
return '<p>size guide</p>';
Reference:
ACF the_field
ACF get_field
I used this code and it worked well in local but when i upload function file in server it doesn,t work and it gives server error 500, so I had to remove this code again
Basically, I want to convert my variation dropdown into something fancy, like for example, make it a radio button.
But this filter is not working. As a sample, I'm using it like this.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'override_color_variation_display', 10, 2 );
public function override_color_variation_display( $html, $args ) {
$html = 'Some override';
return $html;
}
Is this the correct way to get this working? Because it's not showing the "Some override" text anywhere on the product display.
Not sure which WooCommerce version are you using but in 2.5.1 this filter accepts only one variable. Try this code:
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'override_color_variation_display');
function override_color_variation_display( $html ) {
$html = 'Some override';
return $html;
}
I'm trying to remove spaces automatically from data entered into a custom field generated by the ACF plugin when a custom post is updated or saved in wordpress.
I believe I need to use the acf/save_post hook but I'm struggling to get the preg_replace to work. I wonder if I'm not using the right identifier as the custom field name has field name postcodes but when inspected it has name fields[field_55c7969262970]. Can't seem to make it work with that either.
function remove_spaces( $post_id ) {
if( empty($_POST['postcodes']) ) {
return;
} else{
$postcodes = $_POST['postcodes'];
$postcodes = preg_replace('/\s+/', '', $postcodes);
return $postcodes; }
}
add_action('acf/save_post', 'remove_spaces', 1);
I think you are better off using the acf/update_value filter. From thr docs: "This hook allows you to modify the value of a field before it is saved to the database."
function remove_spaces($value, $post_id, $field) {
if(empty($value)) {
return;
}
$value = preg_replace('/\s+/', '', $value);
return $value;
}
add_filter('acf/update_value/name=postcodes', 'remove_spaces', 10, 3);
Hi all, thanks for reading.
Environment :
Wordpress + Advanced Custom Fields plugin
Problem :
I have searched for hours now and I can't seem to find the correct syntax to do the following:
When posting a new post, get custom field value in order to automatically replace the title of the post by that value. Example: I create a post and set '10am' in my 'time' custom field. The title of the post is automatically replaced by '10am'.
Example:
So I'm adding a filter with the following :
add_filter('wp_insert_post_data', 'change_title') ;
function change_title($data)
{
$time = XXX ; // How should I get this custom field value ?
$new_title = 'Topic created at'.$time ;
$data['post_title'] = $time ;
return $data;
}
It must be very simple but I have tried every function available on both WP and the plugin's documentations. I would be very thankful if anyone passing by gave me the solution.
Thanks in advance !
Tweak to Riadh's accepted answer (would add as a comment but haven't got enough rep yet):
As documented in the WordPress Codex wp_update_post includes the save_post hook so calling wp_update_post() inside the save_post hook creates an infinite loop. To avoid this, unhook then rehook your function like so:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
You can actually access the global $_POST variable for your field value , but i guess you can do it in a cleaner way by using the save_post action to update your post's title, eg:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
assuming that your ACF fieldname is "time".
Edit: Updated the answer as per Mark Chitty's answer.
You may try this
add_filter( 'wp_insert_post_data', 'change_title', '99', 2 );
function change_title($data , $postarr){
$custom_field = 'custom_filed_name';
$post_id = $postarr['ID'];
$time = get_post_meta( $post_id, $custom_field, true );
// Now you have the value, do whatever you want
}
Advanced Custom Fields creates a 'field key' for each custom field that is created. I was able to refer to this key value when trying to access the custom fields. The field key value can be found by viewing page source when viewing the post type within the Wordpress admin section.
Look for data-field-key. You will see a value similar to data-field-key="field_5847b00820f13" in the page source. Use this value when accessing the value in the $postarr argument in the wp_insert_post_data filter. The custom fields will be in a nested array named fields within the $postarr argument.
Alternatively, the field key value can be located by navigating to the Advanced Custom Fields / Export option from within the admin section. Once you are on the export page for Advanced Custom Fields, select the export to PHP option and you will see the value in the resulting PHP code.
In the example below, I am concatenating two Advanced Custom Fields and updating the post_title in the $data array returned from the function.
The result is that the post_title value will be saved to the database via the built in Wordpress save post logic.
add_filter('wp_insert_post_data', 'slb_set_title', '99', 2);
function slb_set_title ($data, $postarr){
if($data['post_type']==='slb_subscriber'){
$data['post_title'] = $postarr['fields']['field_5847b00820f13'] .' '.
$postarr['fields']['field_5847b03f20f14'];
}
return $data;
}