Save editor's value in user meta and retrieve it - wordpress

I am making user meta fields. I have to implement a field named business_profile and implement editor on it. I have implemented the editor but I can't seem to either save its value or retrieve it. Below is my code.
<tr>
<th>
<label for="address">Business Profile</label></th>
<td><?php
$content = get_the_author_meta('business_profile', $user->ID);
$editor_id = 'mycustomeditor';
wp_editor($content, $editor_id);
?>
</td>
</tr>
And for update:
update_usermeta($user_id, 'business_profile', $_POST['business_profile']);
Where am I wrong?

Your code should read
update_usermeta($user_id, 'business_profile', $_POST['mycustomeditor']);
since mycustomeditor is the $editor_id than this is how the $_POST variable would be named .
business_profile on the other hand is just the meta_data for user or user_meta name .
It appears you just confused the two .

Related

Add several fields in user-edit.php

I want to insert several fields 'User detail' page NOT when registering, only for the back-end.
I added some codes in user-edit.php and I made a field named 'Company Name' but was NEVER saved!!!
Could you let me know what is wrong or is needed for saving it?
<tr class="user-company-name-wrap">
<th><label for="company_name"><?php _e( 'Company Name' ); ?></label></th>
<td><input type="text" name="company_name" id="company_name" value="<?php echo esc_attr( $profileuser->company_name ); ?>" class="regular-text" /></td>
</tr>
user-edit.php is a core file, and you should never add code to core files. Your code will be erased if any updates to Wordpress happen. I think this stack exchange post should help you add the functionality you want. I would suggest reading through all the answers to find which one would work best, and recommend using Advanced Custom Fields:
https://wordpress.stackexchange.com/questions/214719/how-do-i-add-a-field-on-the-users-profile-for-example-country-age-etc

WooCommerce how to display specific "Additional Information" fields

I am a total WooCommerce noob and I am struggling with something that I feel should be pretty simple...
How can I display specific "Additional Information" fields on a product page? I suppose I need to add some shortcode in the PHP files, but I am lost on what exactly to add...
I have ~15 additional info fields that I created under "Attributes" on the product.
I would like to display specific attributes on various parts of the page, but not all in one spot, much like the "Additional Fields" tab does.
I can't seem to find a straight answer and I am really struggling with this.
So, I realize there is a lack of information.
To further explain what I was trying to do, I was attempting to create a shortcode function in order to display specific "Attribute" fields from the woocommerce product.
In order to do this, I found a solution. This code can be added to your theme's function.php file:
// To diplay formula via shortcode within product page
add_shortcode( 'show_formula', 'show_additional_info_formula' );
function show_additional_info_formula() {
global $product;
$formula = $product->get_attribute('Formula');
?>
<table class="woocommerce-product-attributes shop_attributes">
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--formula">
<th class="woocommerce-product-attributes-item__label"><?php echo ucfirst( 'Formula' ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo $formula; ?></td>
</tr>
</table>
<?php
}
Then, the shortcode to use, is (in this instance), [show_formula].
You can modify this to match your desired outcome.
Change:
add_shortcode( 'show_your_info', 'show_additional_info_attribute_field' );
$your_field = $product->get_attribute('actual_woocommerce_field_name');
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--unique_name">
<th class="woocommerce-product-attributes-item__label"><?php echo ucfirst( 'word_to_be_displayed' ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo $your_field; ?></td>
Then, once modified, the shortcode you'd use:
[show_your_field]
This answer led me down the right path

Woocommerce Cart - parsing shortcode within cart output

Bit of an unusual customisation in WooCommerce that I require...
I've modified the standard WooCommerce cart.php so that the customer can't edit the quantity or remove items from cart. Basically, the customer first visits a seating plan page, selects their seat, and this then adds the relevant ticket (which is just a WC product) to their cart, and displays the cart page.
I want a column in the cart output, which has a button next to every row (every ticket) directing the customer back to the relevant seating plan for that ticket.
I've stored the seating plan shortcode in the WooCommerce product details, and I'm able to recall this in cart.php, and it displays in the right place as text using this line of code:
<td class="product-seating" data-title="<?php esc_attr_e( 'Seating Plan', 'woocommerce' ); ?>">
<?php echo the_field('wccaf_seating_plan_link', $product_id) ; ?>
</td>
This returns the following text nicely under a Seating Plan column in the cart for every ticket:
[tc_seat_chart id="3818" show_legend="true" button_title="Select your seat(s)" subtotal_title="Subtotal" cart_title="Continue to Checkout"]
However, what I actually want it to do is parse this shortcode, which should create a nice button with a link to the correct seating plan.
How would I make it parse the shortcode and not just display it as text? I tried playing around with do_shortcode but didn't have any luck.
Try this:
<td class="product-seating" data-title="<?php esc_attr_e( 'Seating Plan', 'woocommerce' ); ?>">
<?php $seating_plan = get_field('wccaf_seating_plan_link', $product_id) ;
echo do_shortcode($seating_plan);
?>
</td>

Pass dynamically generated select box name into $_POST array

To make an WordPress widget I created a select box which gets options generated dynamically from $post object. The select box's name is also generated by dynamically. Here is the code
<select class="widefat" name="<?php echo $this->get_field_name( 'employee' );?>" id="<?php echo $this->get_field_id( 'employee' ); ?>">
Results this :
<select class="widefat" name="widget-employee[13][department]" id="widget-employee-13-department">
To check the value of the select box I need to pass it to $_POST Array.
How do I pass the select box's value to $_POST array ?
Thanks in advance.
On submit, the $_POST variable will contain all the input fields/selected values etc. from your form in it. You don't have to manually add anything. You can manually pull info from your $_POST variable if you wish.
You said you want to check $_POST against your value ($post->post_title), in that case, since your select has a name
widget-employee[13][department]
You'll use something like
$_POST['widget-employee'][13][department]
The best bet is to just use print_r($_POST) to see the contents of your $_POST variable and see what is in it, so that you can check against.

Wordpress save metabox checkboxes selection

Having an issue whereby the checkboxes inside a metabox aren't saving. I can save one value and have it return the value of checked to the checkboxes, just not multiple values, which I need. I've tried adding a foreach loop to the update_post_meta code but it didn't work. Slightly confused as to where I'm going wrong.
checkbox code is:
$areas = $wpdb->get_results("SELECT * FROM locations ORDER BY locationName ASC");
if( count($areas) ) :
?>
<div id="locationAssignedBoxes" size="1">
<?php
foreach($areas as $area) :
?>
<input type="checkbox" id="locationAssigned" name="locationAssigned" value="<?php echo $area->id; ?>"<?php if(get_post_meta($post->ID, 'locationAssigned', true) == $area->id) { ?> checked="checked"<?php } ?> /> <?php echo $area->locationName; ?><br>
<?php
endforeach;
?>
</div>
<?php
endif;
?>
Update_post_meta code is:
update_post_meta($post->ID, 'locationAssigned', $_POST['locationAssigned']);
Thanks very much!
This isn't a cut-and-paste answer to your question, but it should help. (I'm trying to solve a similar problem.)
Your problem in general: update_post_meta() saves a single meta value, not a collection of values. Because you want to save the values of multiple checkboxes, you have two choices:
Call update_post_meta() once for each of the checkboxes, creating a
collection of meta values associated with the post.
Combine all of the checkbox values into a single string and save those as a single value with one update_post_meta() call.
These two earlier questions are related and might point you in the right direction.
Save checkboxes of a metabox with a foreach loop (invalid argument)
Listing Pages With Checkboxes In a Metabox (and saving them)

Resources