Using Post Meta Phone Numbers as WhatsApp Number #
The Chat Help plugin normally uses the global WhatsApp number set in the plugin settings.
If your website has custom post types such as Directory, Products, “Listings,” “Doctors,” etc., and each item has its own phone number stored in post meta, you can override the WhatsApp number dynamically.
This lets each post show its own WhatsApp button.
🔧 Filter: chathelp_whatsapp_number #
Use this filter to override the default WhatsApp number with a number stored in post meta.
Example: Load WhatsApp number from post meta for Directory & Product posts #
add_filter( 'chathelp_whatsapp_number', function( $number, $context ) {
if ( empty( $context['post_id'] ) ) {
return $number; // No post, use default global number
}
$post_id = (int) $context['post_id'];
$post_type = get_post_type( $post_id );
// Target only specific post types
if ( ! in_array( $post_type, array( 'directory', 'products' ), true ) ) {
return $number;
}
// Get phone number from post meta (edit key to match your needs)
$meta_number = get_post_meta( $post_id, 'phone_number', true );
if ( empty( $meta_number ) ) {
return $number; // Keep default number if no meta found
}
// Sanitize: digits only
$meta_number = preg_replace( '/\D+/', '', $meta_number );
return $meta_number;
}, 10, 2 );
✔ Works for any custom post type
✔ Supports any meta key (you can rename "phone_number" to anything)
✔ Falls back to global settings if meta is missing
🔧 Filter: chathelp_whatsapp_url (optional) #
Use this if you want to modify the final WhatsApp URL — for example, to add tracking parameters or unique behavior by post type.
Example: Add tracking for “directory” posts #
add_filter( 'chathelp_whatsapp_url', function( $url, $context ) {
if ( ! empty( $context['post_id'] ) && get_post_type( $context['post_id'] ) === 'directory' ) {
$sep = ( strpos( $url, '?' ) === false ) ? '?' : '&';
$url .= $sep . 'source=directory';
}
return $url;
}, 10, 2 );
🧠 How Chat Help Detects the Post #
Inside the plugin, we automatically pass the post ID into the filter:
$post_id = is_singular() ? get_queried_object_id() : 0;
This means:
- On single Directory/Product pages → filter works
- On archive pages → defaults still work
- On home page → global setting used
📌 Summary #
| Task | Filter | Use Case |
|---|---|---|
| Override WhatsApp number | chathelp_whatsapp_number | Use phone number from post meta |
| Modify the final WhatsApp URL | chathelp_whatsapp_url | Add tracking, branding, conditional logic |
