i want to update a custom user_meta field when a User changed/edit the xProfile field (width the ID 1542).
this does hook not work
function action_xprofile_data_after_save( $x )
{
print_r($x);
// if($field == 1542)
// {
// update_user_meta($user_id, 'field_1542', 'changed');
// }
}
add_action( 'xprofile_data_after_save', 'action_xprofile_data_after_save', 10, 1 );
I believe this approach works for edits made on both front-end and back-end. And it provides the $user_id:
function peter_xprofile_data_after_save( $data ) {
if ( $data->field_id == 1542 ) {
update_user_meta( $data->user_id, 'field_1542', 'changed');
}
}
add_action( 'xprofile_data_after_save', 'peter_xprofile_data_after_save' );
The above works fine in case of an edit, but not working when you "clean / remove" the text from the field. You should use something like this:
function peter_xprofile_data_after_save( $data ) {
$field_content = bp_get_member_profile_data('field=field_name'); // enter your field name here
if($field_content == '') {
update_user_meta( $data->user_id, 'field_1542', '' );
}
if ( $data->field_id == 1542 ) {
update_user_meta( $data->user_id, 'field_1542', $data->value);
}
}
add_action( 'xprofile_data_after_save', 'peter_xprofile_data_after_save' );
Related
I try to send a customized email template when a customer has a ticket (custom product type) in cart.
I have the following:
function bc_customer_completed_order_template($template, $template_name, $template_path)
{
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = wc_get_product( $cart_item['product_id'] );
$type = get_class($product);
if ( $type == 'WC_Product_Tickets' && 'customer-completed-order.php' === basename($template) ) {
$template = trailingslashit(plugin_dir_path( __FILE__ )) . 'templates/customer-completed-order.php';
}
}
return $template;
}
add_filter('woocommerce_locate_template', 'bc_customer_completed_order_template', 10, 3);
The conditionals are working (on cart and checkout page for example), but when the order is placed, the new template is not used.
Anybody?
Your email template will look like this:
Code snippets:
// Suppose orders don't have ticket products.
$has_tickets = false;
// Loop order items.
foreach ( $order->get_items() as $item_id => $item ) {
// Get product object from order item.
$_product = $item->get_product();
// Check if the product object is valid and the class is `WC_Product_Tickets`
if ( $_product && 'WC_Product_Tickets' === get_class( $_product ) ) {
// Change the flag.
$has_tickets = true;
// Break the loop as we alreay have true flag.
break;
}
}
// Check if order have tickets items.
if ( $has_tickets ) {
// Load custom email template.
wc_get_template( 'templates/custom-customer-completed-order.php' );
// Return as we don't need the below code.
return;
}
It turned out, although the above solution is correct in its idea, in reality one cannot load a template and make use of the $order without extending the woocommerce email class.
Therefore i loaded the function inside the email template itself and made an if - else statement so for situation A the layout is different then for situation b.
Like so:
$has_tickets = false;
// Loop order items.
foreach ( $order->get_items() as $item_id => $item ) {
// Get product object from order item.
$_product = $item->get_product();
// Check if the product object is valid and the class is `WC_Product_Tickets`
if ( $_product && 'WC_Product_Tickets' === get_class( $_product ) ) {
// Change the flag.
$has_tickets = true;
// Break the loop as we alreay have true flag.
break;
}
}
// Check if order have tickets items.
if ( $has_tickets ) {
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
//custom email layout here//
}
else{
//Regular email template here
}
Using the the wpcf7 hook wpcf7_special_mail_tags() I want to get the cart contents and return the output, but WC()->cart object is null in this hook, I cant access any methods on WC()->cart as its a call to an undefined method. I cant work out how to get access to WC()->cart in this hook.
I can access WC()->cart fine in the WPCF7 hook wpcf7_add_form_tag()
The wpcf7_special_mail_tags() works fine and inserts other data into email, I just want cart data though.
Thanks for any help.
function my_special_mail_tag( $output, $name, $html ) {
if ( 'mytag' == $name ) {
$cart = WC()->cart;
if(!WC()->cart->is_empty()) {
// do something with output
}
}
return $output;
}
add_filter( 'wpcf7_special_mail_tags', 'my_special_mail_tag', 10, 3 );
Here you go. I'm ok with this.
function my_special_mail_tag( $output, $name, $html ) {
if ( 'mytag' == $name ) {
$cart = WC()->cart;
if(!WC()->cart->is_empty()) {
// do something with output
}
}
return $output;
}
add_filter( 'wpcf7_special_mail_tags', 'my_special_mail_tag', 10, 3 );
In WordPress code, how can I detect a screen option change? Ie, in the below image I want to hook when one of the column screen options changes. Is there a specific hook for this?
My usecase:
I need to add a screen option to the 'All Posts' page. If the user has unchecked my screen option then the All Posts table should not display posts that have a certain category. I have pretty much all my code for this usecase working (see below) apart from being able to hook/detect when 'My Custom Screen Option' is changed (ticked on or off). If I can do that I can then update the users meta with this decision.
function add_custom_columns( $columns ) {
$columns['display_xyz'] = __( 'XYZ Posts' );
return $columns;
}
add_filter( 'manage_posts_columns', 'add_custom_columns' );
function set_sortable_columns( $columns ) {
$columns['display_xyz'] = 'display_xyz';
return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'set_sortable_columns' );
function sort_all_posts( $query ) {
global $current_screen;
// If on 'All Posts' page.
if ( isset( $current_screen ) && is_admin() && $current_screen->id === 'edit-post' && $query->query_vars['post_type'] === 'post' ) {
$show_xyz_posts = get_user_meta( get_current_user_id(), 'display_xyz' );
if ( ! $show_xyz_posts ) {
// Edit $query to not retrieve posts that have a specific category 'xyz'
}
}
}
add_filter( 'parse_query', 'sort_all_posts' );
// How to detect change of my custom screen option so I can hook the below option??
function on_change_screen_option($option, $value) {
if ( $option === 'display_xyz' ) {
update_user_meta( get_current_user_id(), 'display_xyz', $value );
}
}
add_action( '???', 'on_change_screen_option' );
I am using the Yoast SEO plugin in WordPress and wanted to know if there was a way to make it only visible to one specific user in the db or in the functions.php file? Not a role, an actual user.
I tried an universal solution to simply add "plugin-name" and disable it, but failed.
But, to show WPSEO only to a specific user (ID equals 2), the following works:
add_action( 'plugins_loaded', 'seo_so_25654837' );
function seo_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
remove_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
}
Don't add the code to functions.php, use it as a normal plugin.
The following is also needed to remove the SEO menu from the admin bar:
add_action( 'wp_before_admin_bar_render', 'bar_so_25654837' );
function bar_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
global $wp_admin_bar;
$nodes = $wp_admin_bar->get_nodes();
foreach( $nodes as $node )
{
if( !$node->parent )
{
if( 'wpseo-menu' === $node->id )
$wp_admin_bar->remove_menu( $node->id );
}
}
}
You can hook to pre_current_active_plugins to remove elements from the table before it is displayed. Using get_current_user_id() within the function will let you selectively hide a plugin.
function hide_plugins_by_user( $all_plugins=false ) {
global $wp_list_table;
// if the current user ID is not 1, hide it.
if ( 1 != get_current_user_id() ){
// the active plugins from the table
$plugins = $wp_list_table->items;
// loop through them
foreach ( $plugins as $key => $val ) {
// use the dir + filename of the plugin to hide
if ( $key == 'plugindir/plugin.php' ) {
unset( $wp_list_table->items[$key] );
}
}
}
}
add_action( 'pre_current_active_plugins', 'hide_plugins_by_user' );
This code is working fine (Credits goes to Hislop):
// Returns true if user has specific role
function check_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) )
$user = get_userdata( $user_id );
else
$user = wp_get_current_user();
if ( empty( $user ) )
return false;
return in_array( $role, (array) $user->roles );
}
// Disable WordPress SEO meta box for all roles other than administrator and seo
function wpse_init(){
if( !(check_user_role('seo') || check_user_role('administrator')) ){
// Remove page analysis columns from post lists, also SEO status on post editor
add_filter('wpseo_use_page_analysis', '__return_false');
// Remove Yoast meta boxes
add_action('add_meta_boxes', 'disable_seo_metabox', 100000);
}
}
add_action('init', 'wpse_init');
function disable_seo_metabox(){
remove_meta_box('wpseo_meta', 'post', 'normal');
remove_meta_box('wpseo_meta', 'page', 'normal');
}
Just place it in the functions.php file.
To disable the Yoast for all the users and enable it for just for few or specific, just add the following piece of code to your function.php file.
function remove_wpseo(){
/* if you want to keep it enabled for user with id 2 */
if ( '2' == get_current_user_id() ) {
return;
}
global $wpseo_front;
if(defined($wpseo_front)){
remove_action('wp_head',array($wpseo_front,'head'),1);
}
else {
$wp_thing = WPSEO_Frontend::get_instance();
remove_action('wp_head',array($wp_thing,'head'),1);
}
}
add_action('template_redirect','remove_wpseo');
Reference: https://makersbyte.com/disable-yoast-seo-plugin-specific-page/
How add custom column to woocommerce shopping cart and then add info of some input from this column to order, checkout page and to email?
Actually i need add friends list from buddypress to each product row(price must depends on how many friends checked).
Here i found suggestion, but it`s partly helpfull WooCommerce: Add input field to every item in cart
wp community also keep silence
http://wordpress.org/support/topic/woocommerce-custom-column-in-cart?replies=1
what i do - its just add list of avalaible friends and no idea how can I save data on update cart or proceed.
if ( bp_has_members( 'user_id=' . bp_loggedin_user_id() ) ){
function ggUserFrom(){
$arrUsers = array();
while ( bp_members() ){
bp_the_member();
$arrUsers[ bp_get_member_user_nicename() ] = bp_get_member_user_nicename();
}
return $arrUsers;
}
echo "<div class='friends-holder'>";
foreach ( ggUserFrom() as $friend ){
echo '<p><input type="checkbox" name="cart['.$cart_item_key.'][friendsfromcart]" value="'.$friend.'">
<span>'.$friend.'</span></p>';
}
echo "</div>";
}
Im seek ANY info about this question.
Here, since user may choose multiple checkboxes, it may contain multiple values. Therefore, we have used "serialize" function.
for example,
add_filter( 'woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
if(!function_exists('wdm_get_cart_items_from_session'))
{
function wdm_get_cart_items_from_session($item,$values,$key)
{
$item['custom_field_name'] = isset( $values['friendsfromcart'] )? serialize($values['friendsfromcart']) : '';
return $item;
}
}
While you are adding Order meta data, you can fetch individual values and add corresponding keys as follows,
add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
function wdm_add_values_to_order_item_meta($item_id, $values)
{
$user_custom_values = unserialize($values['friendsfromcart']);
if(count($user_custom_values) > 0)
{
foreach($user_custom_values as $single_value)
{
wc_add_order_item_meta($item_id,ucfirst($single_value),single_value);
}
}
}
since order meta will be sent in E - mail.
Thank for reply and - yes, variable isset in checkout page, but his empty in
var_dump($_POST)...
...[custom_field_name] =>
...
and ofc is empty in email.
Perhaps i incorrect send it ?
name="friendsfromcart"
or send values its a array and must send:
name="friendsfromcart[]"
or
name="[friendsfromcart]"
or need session key
name="cart['.$cart_item_key.'][friendsfromcart]" ?
It works, array in cart but something wrong with unserialize:
add_action( 'init', 'update_cart_action', 9);
function update_cart_action() {
global $woocommerce;
if ( ( ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && $woocommerce->verify_nonce('cart')) {
$cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if ( isset( $cart_totals[ $cart_item_key ]['friendsfromcart'] ) ) {
$woocommerce->cart->cart_contents[ $cart_item_key ]['friendsfromcart'] = $cart_totals[ $cart_item_key ]['friendsfromcart'];
}
}
}
}
}
add_filter( 'woocommerce_get_cart_item_from_session', 'wdm_get_cart_items_from_session', 1, 3 );
function wdm_get_cart_items_from_session($item,$values,$key){
$item['friendsfromcart'] = isset( $values['friendsfromcart'] )? serialize($values['friendsfromcart']) : '';
return $item;
}
add_action('woocommerce_add_order_item_meta','wdm_add_values_to_order_item_meta',1,2);
function wdm_add_values_to_order_item_meta($item_id, $values){
$user_custom_values = unserialize($values['friendsfromcart']);
if(count($user_custom_values) > 0){
foreach($user_custom_values as $single_value){
wc_add_order_item_meta($item_id,ucfirst($single_value),single_value);
}
}
}