Update user repeater meta (ACF) with another user meta field value - php

My goal is to get a signed-in user to select a color( via front end form and saved in ACF user meta field group) that will be applied to another user meta field inside a repeater. The field must be the same for each row inside the repeater ( for front-end design reasons ). I am using ACF pro, and a plugin called ACF Front end admin (let's call it FEA from now on) for the front-end form. I'm pretty new to PHP and have referenced ACF & FEA's s documentation, which can be found below, spin up a custom function. I am currently running this through the code snippets plugin if this helps. I've run an error log and nothing related to this shows. Tried running this through wp-config and it crashes my site.
Front end admin documentation
ACF documentation - update sub field
ACF documentation - getting values from a user
Edit, Additional information:
ACF Field composition of 'button color' (color of the buttons that user wants displayed in front end, and saved as user meta):
acf_add_local_field_group(array(
'key' => 'group_60000aaa00000',
'title' => 'Color Selector',
'fields' => array(
array(
'key' => 'field_60000aaa00000',
'label' => 'Button color',
'name' => 'button_color',
'type' => 'color_picker',
Button color in repeater field that needs to updated based on the above mentioned 'button_color' meta value:
acf_add_local_field_group(array(
'key' => 'group_70000bbb00000',
'title' => 'front end user profile',
'fields' => array(
array(
'key' => 'field_70000bbb00000',
'label' => 'Quick Link selector',
'name' => 'quick_link_selector',
'type' => 'repeater',
),
array(
'key' => 'field_70000ccc00000',
'label' => 'repeater button color',
'name' => 'repeater_button_color',
'type' => 'text',
The title of the forum is: "QL color selector form" (created through the FEA plugin) and has a short code of [frontend_admin form="3030"] if this helps.
I'm running the following code with no luck and would really appreciate any help!
/// Hooks into any(?) form and does something
add_action('acf_frontend/save_user', 'retrieve_colors_2_update', 10, 2);
function retrieve_colors_2_update( $form, $user_id ) {
$user = get_user_by('ID',$user_id);
//get important fields
$btn_color = get_field('button_color', 'user_' .$user_id);
//// update a repeater loop (ACF)
if( have_rows('quick_link_selector', 'user_'.$user_id) ) {
$i = 0;
while( have_rows('quick_link_selector') ) {
the_row();
$i++;
update_sub_field('repeater_button_color', $btn_color );
}
}
}

Related

Customizing checkout Postcode field into a custom drop-down menu

I am building a WooCommerce based store. I have a list of postcodes, each one has a different shipping cost attached through Shipping Zones (some provide free shipping, some have a flat rate).
When the customer goes to the checkout page, he needs to type his postcode number in the input field. Depending on postcode, an order preview will show different shipping total (free or flat rate).
Here's how the input field looks like in class-wc-countries.php:
public function get_default_address_fields() {
$fields = array(
'postcode' => array(
'label' => __( 'Postcode/ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
),
);
However, what I want to do is to turn this field into a drop-down menu, so the customer could just select his postcode option rather than type it.
I managed to make it drop-down, but whenever I choose any option it doesn't seem to change shipping total as it would with input field.
Here's what I did:
public function get_default_address_fields() {
$fields = array(
'postcode' => array(
'label' => __( 'Postcode/ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
'type' => 'select',
'options' => array(
'opt1' => "001122", "112200", "334400")
),
);
But this don't work.
Am I missing something?
How do I make these drop-down options change shipping total?
Thanks
This will answer very partially to your question, and just show you the way to customize checkout fields.
Overriding core files is not really something to do, as you will loose everithing each time Woocommerce is going to be updated and is not recommended.
To override checkout fields in a clean way, first you need to use a custom function hooked in one of that 2 filter hooks:
woocommerce_default_address_fields (when customizing billing and shipping address default fields)
woocommerce_checkout_fields (when customizing billing or shipping address fields and also others fields).
Related official documentation: Customizing checkout fields using actions and filters
So here I have chose the first hook, and I have corrected your post codes array. You will get that:
Here is that functional and tested code:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_postcode_field' );
function custom_override_default_postcode_field( $address_fields ) {
// Your postcodes array
$postcode_array = array(
'opt1' => "001122",
'opt2' => "112200",
'opt3' => "334400"
);
$address_fields['postcode']['type'] = 'select';
$address_fields['postcode']['options'] = $postcode_array;
return $address_fields;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
As selecting a post-code is a live front-end event, you will need to use a complex javascript/ajax script with some remote php function to achieve what you want to do, and this is a real development... It also depends on your settings and is complex to handle as there is already some woocommerce ajax scripts operating in that checkout page.

Populate ACF Checkboxes with Post Types

I'm trying to populate a checkbox ACF field with the various post types of a WP site. This is for a plugin, so the post types will vary based on the location of install.
By default, the plugin uses pages and posts as it's post types, but need to give user option to use checkboxes to select other CPT's on the site. How can I populate the checkbox field with the list of all CPT's on a site. Here is my current section of PHP code to load the field within the plugin
array (
'key' => 'field_56e6d87b6c7be',
'label' => 'Add to Custom Post Types',
'name' => 'fb_pixel_cpt_select',
'type' => 'checkbox',
'instructions' => 'Select which Custom Post Types you would like to use.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array (
),
'default_value' => array (
),
'layout' => 'vertical',
'toggle' => 0,
),
You could use the ACF load field function here to auto populate your field. See more here: http://www.advancedcustomfields.com/resources/acfload_field/
Then, using the Wordpress get_post_types call (https://codex.wordpress.org/Function_Reference/get_post_types) you could retrieve those values and populate your field like this.
add_filter('acf/load_field/name=fb_pixel_cpt_select', 'acf_load_post_types');
function acf_load_post_types($field)
{
foreach ( get_post_types( '', 'names' ) as $post_type ) {
$field['choices'][$post_type] = $post_type;
}
// return the field
return $field;
}
Make sure that your field is already created before you try to populate it though.
This will create a select field for your ACF block or whatever that has all the post types except those that don't have a nav menu (such as the attachment post type).
add_filter('acf/load_field/name=select_post_type', 'yourprefix_acf_load_post_types');
/*
* Load Select Field `select_post_type` populated with the value and labels of the singular
* name of all public post types
*/
function yourprefix_acf_load_post_types( $field ) {
$choices = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
foreach ( $choices as $post_type ) :
$field['choices'][$post_type->name] = $post_type->labels->singular_name;
endforeach;
return $field;
}
When I need to do this (choose from list of post types) I would be creating a list of posts based on post-type to feed into WP Query. So I use a select field in ACF, and add the post type names into the select options when creating the field, e.g.
posts : Posts
which I then use in the arguments for the WP Query, like so:
$theselectfield = get_field('theselectfield');
Then use it in the arguement of the query:
$args = array(
'post_type' => $theselectfield,
'some_other' => 'argument',
);

wordpress how to add wp_editor in an array

I had a small problem in my wordpress code I need to show a wordpress wp_editor in my page where it has array of values.the values are defined like the following
$fields[] = array(
'name' => __('Class', 'my-theme'),
'desc' => __('', 'my-theme'),
'id' => 'class'.$n,
'std' => ( ( isset($class_text[$n]['class']) ) ? $class_text[$n]['class'] : '' ),
'type' => 'text');
When I define my wp_editor like the above array it doesn't display where I want. Instead all the editors displayed at the top before any content in all pages.
I tried like the following set of array for the editor:
$fields[] = array(
'name' => __('My Content', 'my-theme'),
'id' => 'sectioncontent'.$n,
'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
'type' => wp_editor( '', 'sectioncontent'.$n ));
Attached the image of my problem:
Cause
By default wp_editor prints the textarea that's why you cannot assign it to any variable or array.
Solution
you can use output buffering of php to get printed data in variable like this:
ob_start(); // Start output buffer
// Print the editor
wp_editor( '', 'sectioncontent'.$n );
// Store the printed data in $editor variable
$editor = ob_get_clean();
// And then you can assign that wp_editor to your array.
$fields[] = array(
'name' => __('My Content', 'my-theme'),
'id' => 'sectioncontent'.$n,
'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
'type' => $editor); // <-- HERE
Looks to me like you're using Redux Framework to set up your theme/plugin option page - If you are looking to add the default Wordpress WYSIWYG (What You See Is What You Get - the same editor from the edit post page in the backend) editor in there you'll need to use the type: 'editor'.
It can be confusing - the wp_editor() function you are using is the right place to start if you were setting up this options page from scratch, but you'd need to do quite a bit of work to have it display where and how you want it. Redux et al make this quite a bit easier for you by generating the editor for you, so instead of you using the wp_editor function at all you just tell Redux that you want an editor field called 'My Content' to be one of the fields on the page.
The documentation for the editor field is here: https://docs.reduxframework.com/core/fields/editor/
If I am correct that you are using redux, the correct code to replace what you have is:
$fields[] = array(
'name' => __('My Content', 'my-theme'),
'id' => 'sectioncontent'.$n,
'std' => ( ( isset($class_text[$n]['content']) ) ? $class_text[$n]['content'] : '' ),
'type' => 'editor');
To explain the other parts of this field array:
'Name' will be what shows up in the label for this field. In this case you are using the localization functions in wordpress (__()) to get a phrase from the local dictionary in the 'my-theme' domain.
'id' is what you will use to retrieve what has been entered into this field. It will also affect the ID attribute assigned to the HTML elements in the options page.
'std' is the default for the field, which will be the value of the field when the options page is first displayed, before the user has set any options
On the editor documentation page linked above, you'll see the details of various other options you can define, like whether to show Media Upload buttons, and whether to run the input through wpautop to replace line breaks in the editor with <p> tags (by default both of these are true).

PHP adding count to function

I am building a new site in woo/ WordPress and a little stuck, basically the client wants to be able to add custom fields to the checkout (done) but they need to be in a repeatable field so the customer can add the field as many times as they need (done) but I am stuck on the actual integration into the database.
My code is at http://pastebin.com/vR4GWV7w
Basically I need to add a custom ID to each repeatable field input e.g.
child_name1
child_name2
If I was doing this in a template file I would use PHP count but this needs to work in the code below which creates the field.
function my_custom_checkout_field( $checkout ) {
echo '<div id="entry1" class="clonedInput"><h2>' . __('Child Details') . '</h2>';
woocommerce_form_field( 'child_name', array(
'type' => 'text',
'class' => array('child_field form-row-wide'),
'label' => __('Child Name'),
'placeholder' => __('Enter the child\'s name'),
), $checkout->get_value( 'child_name' ));
}
Any help / guidance would be much appreciated, I posted this on the WordPress exchange and got put on hold so trying here as it's a PHP question

WPAlchemy Meta Box: Place meta box on specific post / page... conflict with "types" argument

I'm using the WPAlchemy class to create a metabox. I want to place this metabox in a number of post editors in the backend.
Currently it's working just fine with the following code:
$video_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_videoMeta',
'title' => 'Videos',
'types' => array('characters','homepage'),
'template' => THEMEASSETS . '/functions/video_meta.php'
));
What I want to do though is additionally place the metabox on the post editor for post ID #22. Supposedly the following code should work:
$video_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_videoMeta',
'title' => 'Videos',
'types' => array('characters','homepage'),
'template' => THEMEASSETS . '/functions/video_meta.php',
'include_post_id' => 22
));
But it doesn't work unless I add in 'page' into the array of post types, which adds the metabox to all pages (not just post ID 22).
Is there a way to use poth the types and the include post ID arguments?
I had the same issue. Acutally, I had set a metabox to two custom post types and wanted it to be displayed on a specific page.
$video_metabox = new WPAlchemy_MetaBox(array(
'id' => '_videoMeta',
'title' => 'Videos',
'types' => array('characters','homepage', 'page'),
'template' => THEMEASSETS . '/functions/video_meta.php',
'include_post_id' => 22
));
Just add the bult in 'page' post type and everything should work fine. It did for me.

Categories