WordPress wp_get_current_user returning blank values until refresh

Your header.php file is going to be called after the init action. If you want the logic to run in your header.php file first, maybe you can hook into another action done at a later point in WordPress: add_action(‘wp_head’, ‘ax_get_user_info’); function avox_get_user_info() { if (is_user_logged_in()) { $currentUser = wp_get_current_user(); $fName = $currentUser->user_firstname; } return $fName; … Read more

Get Category in save_post Hook

Unfortunately, no success after a test 🙁 I put the follwing: if ( get_post_status( $post_id ) === ‘publish’ ) { $post_title = apply_filters(‘the_title’, get_post_field(‘post_title’, $post_id)); $post_content = apply_filters(‘the_content’, get_post_field(‘post_content’, $post_id)); $taxonomy = “category”; $args = array(‘orderby’ => ‘name’, ‘order’ => ‘ASC’); $cat = wp_get_post_terms( $post_id, $taxonomy, $args ); $categorie = $cat[0]->name; I get the count … Read more

Remove username and account password field from checkout page

Here is my answer and it’s working perfectly. function conditionally_remove_checkout_fields( $fields ) { // HERE the defined product Categories $categories = array(‘age-defying-skincare’,’az-brand-accessories’,’az-healthy-supplements’,’az-other-natural-products’,’Not available mwb_wgm_giftcard’,’little-geniuses-children’); $found = false; // CHECK CART ITEMS: search for items from our defined product category foreach ( WC()->cart->get_cart() as $cart_item ){ if( has_term( $categories, ‘product_cat’, $cart_item[‘product_id’] ) ) { $found = … Read more

Add JQuery As Code Snippet

Create js file and add it to your child theme script folder. Then just add code to functions.php, example if page is checout wp_enqueue that file. Example: https://sridharkatakam.com/how-to-load-woocommerce-css-and-js-only-on-shop-specific-pages-in-wordpress/

Is there an option to execute javascript file only on plugin activation

Here is the solution: register_activation_hook( __FILE__, ‘rest_api_hook’ ); /** * Runs only when the plugin is activated. */ function rest_api_hook() { /* Create data */ set_transient( ‘rest_api’, true, 5 ); } /* Add notice */ add_action( ‘admin_notices’, ‘rest_api_hook_exec’ ); /** * Rest API Notice on Activation. */ function rest_api_hook_exec() { /* Check transient, if is … Read more

Can’t get ID of post that relates to the comment

comment_post hook pass to attached functions 3 parameters. The fourth parameter of add_action() indicates to how many parameters the attached function will be able to access. add_action( ‘comment_post’, ‘save_chbox_in_db’, 10, 3 ); function save_chbox_in_db( $comment_ID, $comment_approved, $commentdata ) { $post_id = (int)$commentdata[‘comment_post_ID’]; // // other code // }

How do I get the value of a current user’s custom field?

Add this to the frontend and you should be able to adjust it to display whatever field you’ve added. $user_info = get_userdata(get_current_user_id()); echo ‘Username: ‘ . $user_info->user_login . “<br>”; echo ‘User roles: ‘ . implode(‘, ‘, $user_info->roles) . “<br>”; echo ‘User ID: ‘ . $user_info->ID . “<br>”; echo ‘User Agent Department: ‘ . $user_info->agentdept . … Read more