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

Add a custom class to the body tag using custom fields

You want to use body_class filter.

function prefix_add_body_class( $classes ) {
    global $post;

    // good to check
    if( ! is_single() || 'post' !== get_post_type() ) {
        return $classes;
    }

    $btag = get_post_meta( $post->ID, 'tagbody', true );

    if ( empty( $btag ) ) {
        return $classes;
    }

    $classes[] = $btag;

    return $classes;
}
add_filter( 'body_class', 'prefix_add_body_class' )

EDIT:

You wrote me in the comments that the theme is your own theme. So you don’t need to do it via a filter. Just do it right in your theme.

Edit the header.php file like this.

// some code above
$classes = array();

if( is_single() && 'post' === get_post_type() ) {
    $btag = get_post_meta( $post->ID, 'tagbody', true );

    if( ! empty( $btag ) ) {
        $classes[] = $btag;
    }
}
?>
<body <?php body_class( $classes ); ?>>
<?php
// some code below

Related Posts:

  1. How to update custom fields using the wp_insert_post() function?
  2. How to display multiple Post meta_key/meta_values by SQL query
  3. WooCommerce: How to display item meta data in email confirmation using woocommerce_order_item_meta_end
  4. How to use multiple query with same meta key
  5. How can update custom meta for all posts
  6. Use ajax to update_post_meta
  7. how to increase custom post value by one most efficiently?
  8. How can I create a list of page titles from custom meta values?
  9. Store multiple custom field as post meta per post(css, js, html, 2 link) [closed]
  10. How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
  11. Is it possible to retrieve all posts with a certain value for metadata?
  12. Non-Closing PHP Query in WordPress Loop
  13. how to get serialized post meta
  14. WordPress loop by meta key that is an array? and how loop multiple arrays
  15. How to store multiple custom meta box
  16. Order a WP_Query by meta value where the value is an array
  17. Define category ID using get_post_meta
  18. Hide a div when a custom field is empty
  19. WordPress stripping out custom field tags
  20. Order category posts by last word of custom field
  21. Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
  22. Display custom field value in woocommerce variable product via jQurey
  23. How to update custom fields when post is published?
  24. How do I get the value of a current user’s custom field?
  25. WP post meta – for loop inside for loop
  26. When working with a post, almost all wp_postmeta are deleted
  27. Updating Lat and Lng of posts automatically gives sporadic results
  28. If custom field doesn’t exist, or exists and it’s true show title?
  29. Metadata on the WP_Post object
  30. Trying to remove post thumbnail with plugin
  31. Randomly Assign an Image’s Alt Text Based on Data From Post
  32. Passing the custom field values in the wp_get_current_user array function
  33. hook that when page is starting to load
  34. Advanced Meta Query for Large Calendar Website (12k+ posts) (175k+ wp_postmeta rows)
  35. Sort posts by custom fields value using dropdown menu
  36. How to change home or site url using action hooks or filter?
  37. Custom profile field with birthday. Troubles with
  38. ajax, right way to do it and make it works?
  39. WordPress – Display array data of a child
  40. Sending data from custom inputs in WordPress comment form in the admin notification email
  41. Edit incorrect password message WordPress
  42. Assign / update custom field value for all posts (How can I assign only to posts without custom field value?)
  43. wordpress allow user to edit user profile with custom fields
  44. Why do WP_Query results change after updating unrelated Advanced Custom Fields (ACF)?
  45. Sort custom meta column by other meta value
  46. How to update custom field in WooCommerce
  47. Reload part of checkout on changed shipping method
  48. Trying to build simple deposit code that hooks into woocommerce
  49. Creating user status mode in WordPress
  50. Need Help Fixing My Iframes [closed]
  51. Contact Fom 7 – how to add custom HTML inside span.wpcf7-form-control-wrap AND IMMEDIATELY AFTER input.wpcf7-form-control?
  52. get_post_meta not working on publishing
  53. Need help with Google drive API [closed]
  54. Woocommerce: hook action/filter I could use to add variation id and price with each attribute opt on WooCommerce Rest api
  55. Remove metabox from WordPress menu editor page?
  56. Add a product to checkout via form
  57. Firing schema via code in functions.php doesn’t work
  58. Why ajax doesn’t work on certain wordpress hooks and reload the page instead?
  59. Why ajax doesn’t work on certain wordpress hooks?
  60. Remove a div from RSS feed
  61. Is it possible to intercept all ajax requests and get the parameters and the returns?
  62. user_profile_update_errors hook not executing
  63. Adding number to date not working [closed]
  64. Set meta field to publish date + 2 weeks
  65. How to add an arrow to menu items has submenus
  66. WordPress Gravatar filter is removing my custom attributes
  67. How can I add extra word in permalink when someone click download button?
  68. Error in custom php function doesn’t exist
  69. Generating an nonce for Content Security Policy and all scripts – How to make it match/persist for each page load?
  70. Saving and Restoring a Canvas on A Individual User Basis
  71. Don’t insert if permalink is the same?
  72. Return a numerical function value in Customizer controls
  73. Adding widgets to my plugin page instead of WordPress dashboard
  74. Seach custom post type posts only by meta fields?
  75. How to use thumbnail size of image if I’m only using src to get image
  76. 3 Slashes appear after Apostrophe in custom fields after updating product-site
  77. how to retrieve a value if a checkbox is checked
  78. Trouble checking if custom woocommerce checkout field is empty or not
  79. Two queries for a WP_User_Query search work perfectly apart, but not together
  80. edit_comment_link is not showing for comment author
  81. Adding function to Genesis genesis_header [closed]
  82. Archive post by meta value + 24hours
  83. Modify custom field input data before creating a new post
  84. auto-populating custom nav with all items from custom post type
  85. how to make custom ajax handler?
  86. Are all hooks/functions tied to Kses meant for sanitization?
  87. Add / Update Custom Fields After Select Pictures in Media Window
  88. WordPress update_post_meta updating with empty meta_value field [closed]
  89. conditional logic for front-end custom field edits
  90. Trying to retrieve post meta
  91. Automatic Shortcode Creation with Custom Fields [closed]
  92. Custom field values to taxonomy terms
  93. How would an if statement surrounding a custom field with two variables (holding values) look like?
  94. How to add specific meta tags to head of cart and checkout pages in woocommerce?
  95. Custom Field IF/ELSE PHP
  96. Why does wp_head hook my functions to the beginning of my source code?
  97. Pass argument to event hook
  98. How to remove woocommerce_breadcrumb() from do_action( ‘woocommerce_before_main_content’ ); [closed]
  99. Which PHP page is the Default Posts Page
  100. Should `wp_login` be used since it’s deprecated?
Categories PHP Tags custom-field, hooks, php, post-meta
What will be the best caching system for wordpress blog which has more than one million blog posts? [closed]
How to stop being directed to admin.php after sending request to admin-ajax.php

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