Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Conditionally run function based on custom meta value?

Instead of conditionally adding an add_action() call, you can just add it and run the function it hooks to—its callback—conditionally instead.

For example:

add_action( 'woocommerce_product_additional_information', 'wpse407073_callback' );
add_action( 'woocommerce_after_shop_loop_item', 'wpse407073_callback' );

function wpse407073_callback( $product = null ) {
    if ( empty( $product ) ) {
        global $post;
        $product = $post;
    }
    if ( 'X' === get_post_meta( $product->ID, 'my-custom-meta-field', true ) ) {
        echo '<img src="image.jpg" alt="Image Name">';
    }
}

A few points:

  • Having looked quickly at WooCommerce’s code, woocommerce_after_shop_loop_item doesn’t pass anything to the callback, and woocommerce_product_additional_information passed 1 parameter, $product, which I’m assuming is a custom post. That’s why I added a check for an empty $product in the callback.
  • This code hasn’t been tested, and is meant as a possible starting point, not a finished product.
  • If you have questions about WooCommerce’s hooks, you should check with WooCommerce’s documentation and/or their support team.

Related Posts:

  1. How to update custom field in WooCommerce
  2. How to add custom checkboxes from loop to WooCommerce product variation options?
  3. acf_form() – “On Update” action [closed]
  4. WooCommerce: Can’t use wc_get_products for custom REST API endpoints
  5. How to get rid of the hover zoom in WooCommerce single products
  6. What’s the difference between WC() and $woocommerce
  7. Hook and send Woocommerce data after click Place Order button
  8. WooCommerce conditional meta query
  9. WooCommerce: How to display item meta data in email confirmation using woocommerce_order_item_meta_end
  10. Get updated meta data after save_post hook
  11. Show only geolocated user country into Woocommerce checkout country fields
  12. Better post meta efficiency?
  13. post meta data clearing on autosave
  14. Extend Woocommerce rest api routes fails
  15. Display info from custom fields in all images’ HTML
  16. Update meta values with AJAX
  17. Remove action on product archive page [closed]
  18. The correct way to override WooCommerce product gallery from a plugin [closed]
  19. display available size on hover, in woocommerce shop page [closed]
  20. Customize WooCommerce orders displayed shipping
  21. What is the action hook for an order that fails on frontend checkout in WooCommerce?
  22. WooCommerce: Adding Order Item Meta Data That’s Hidden
  23. Get rid of product images mobile swipe functionality from WooCommerce single product [closed]
  24. Which hook should I use to capture $_POST(‘password’) via profile update and password reset
  25. Conditionally hide or show woocommerce product variation in fontend by custom field
  26. Edit WooCommerce product content based on category
  27. woocommerce_package_rates not fired everytime
  28. differentiate hooks and filters on the same page [closed]
  29. ‘profile_update’ hook alternative for WooCommerce user meta data
  30. How to add a user custom field into Woocommerce emails?
  31. Woocommerce custom meta fields, sort by date
  32. Redirect customer to login page (with other signup plugin) if user not logged in when proceeding to checkout
  33. How to subscribe free subscription on user registration in woocommerce subscription plugin?
  34. Woocommerce Moving Review Below add to cart breaks button
  35. How to upload multiple image using wp_insert_post
  36. WooCommerce order complete email logic
  37. How to override Woocommerce functionality in Reports section?
  38. how can use woocommerce add to cart popup?
  39. How to Update the Order-Items While Editing an Order on the WooCommerce Admin Order Screen [closed]
  40. WooCommerce Hook: Content get’s displayed twice and not AFTER the order total
  41. Is it possible to update the total price on the checkout page in woocommerce from a custom field
  42. Adding Product Name on Admin Panel Order list and User’s My-Account Order List
  43. update_post_meta not work [closed]
  44. How to exclude a taxonomy from shop & search page wooCommerce?
  45. Redirect no product url’s to static url
  46. Adding a custom Shipping methods field to an order
  47. What Hook/Action is performed when a field in checkout form is changed in WooCommerce
  48. How to change a WooCommerce Subscription Deposit and Monthly Payment? [closed]
  49. How can I add a so i can show sale price under normal price
  50. password_reset doesnt work
  51. WooCommerce checkout page price break down table hook
  52. How to output woocommerce products to a page, style and modify the html structure
  53. Filter default_content only for products
  54. Conditional action hook
  55. How can I override wp_price woocommerce function in my theme
  56. manage_shop_order_posts_custom_column() hook is Not working In Functions.php file in my WordPress active theme
  57. Display custom product option in admin order details
  58. Next/Previous Product with custom order by price & products inside the product category [closed]
  59. WooCommerce Meta
  60. How to alter taxes amounts when updating an order
  61. How to check if woocommerce_order_item_name is use in email?
  62. How to add woocommerce products header title in woocommerce_breadcrumb hook to function.php
  63. Use ID as slug for product category by default in WooCommerce?
  64. What is the problem with these lines of code?
  65. data-value for custom woocommerce field showing in code but not visable on page load
  66. Display custom field value in woocommerce variable product via jQurey
  67. Woocommerce : Add name filed of checkout page to buyers wordpress display name
  68. Missing variable options on add to cart form
  69. call_user_func_array() expects parameter 1 to be a valid callback, class ‘WC_Rapyd’ does not have a method ‘install’
  70. Help to locate a woocommerce hook method
  71. Add custom field address_3 before city in user profile
  72. check shipping amount in woocommerce_after_checkout_validation?
  73. Woocommerce Disabling Auto Restocking for Cancelled Orders
  74. Parameters in woocommerce_before_add_to_cart_button hook
  75. How can i add product Accessories from front-end using custom php form?
  76. woocommerce_add_to_cart custom function called several times
  77. How to track product price changes?
  78. How to save a Woocomerce metabox input as a metadata field
  79. Display product thumbnail in Woocommerce email notifications
  80. Can’t update WooCommerce payment method title
  81. Add suffix to price html does not work
  82. With what hook do I remove WooCommerce existing Product Data Tab form fields?
  83. How to hook in to WooCommerce Related Products
  84. Woocommerce: Unique price for individual basket items
  85. Hide add to cart when product variation has no price or is unavailable
  86. Update Custom Post Meta Even if Draft
  87. WooCommerce Admin Only Email Hook
  88. Check if value exists before saving
  89. change attachment custom field onChange event
  90. Add custom fields after post/page title
  91. Can a woocommerce hook alter the UI of a website?
  92. Order shipped by which driver[hook for woocoomerce order staus changed and popup in admin panel ] [closed]
  93. Updating order meta to have a meta entry from an item in the order
  94. dakon show vender address on checkout page
  95. Hide a shipping method if cart total is higher than an amount
  96. How can i hide a specific class when cart subtotal is above a certain amount
  97. Add second price option based on payment method at cart item price
  98. Use a woocommerce hook to create a table and populate it with a query
  99. Custom fields in the billing address section woocommerce
  100. Woocommerce send metadata to stripe
Categories woocommerce-offtopic Tags conditional-content, custom-field, hooks, post-meta, woocommerce-offtopic
What’s more important for supporting concurrent users on an elearning site with dynamic content, CPUs or RAM?
wp.media Uncaught TypeError: Cannot read properties of undefined (reading ‘state’)

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress