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

Hide individual page title using checkbox in custom meta box?

$value is always empty because post ID is missing. Pass the parameter ($post object) to the hide_title_callback() function and get the post ID right way.

Also, you should take care of nonces.

<?php
// pass the $post object
function hide_title_callback( $post ) {

    // you have to verify this nonce inside `mysite_save_postdata()`
    // https://codex.wordpress.org/Function_Reference/wp_nonce_field
    wp_nonce_field( 'mysite_action_name', 'mysite_nonce_field_name' );

    // now you have the post ID
    $value = get_post_meta( $post->ID, '_mysite_meta_hide_title', true );

    if( $value == "on" ) : ?>
        <label for="hide">Hide: </label><input type="checkbox" name="hide" checked>
    <?php else : ?>
        <label for="hide">Hide: </label><input type="checkbox" name="hide">
    <?php endif;
}

Verify the nonce

<?php
function mysite_save_postdata( $post_id ) {
    // https://codex.wordpress.org/Function_Reference/wp_verify_nonce
    if ( !wp_verify_nonce( $_POST['mysite_nonce_field_name'], 'mysite_action_name' ) ) {
        return;
    }

    if ( isset( $_POST['hide']) ) {
        update_post_meta(
            $post_id,
            '_mysite_meta_hide_title',
            sanitize_text_field( $_POST['hide'] )
        );
    } else {
        update_post_meta(
            $post_id,
            '_mysite_meta_hide_title',
            sanitize_text_field( "off" )
        );
    }
}

To keep the database a bit cleaner I personally should not save the ‘off’ meta value. I should remove the _mysite_meta_hide_title completely if checkbox is unchecked. So, it will exist if ‘on’ and do not if ‘off’. if() statement in the hide_title_callback() will remain intact.

<?php
if ( ! isset( $_POST['hide'] ) ) {
    // unchecked
    delete_post_meta(
        $post_id,
        '_mysite_meta_hide_title',
    );
} else {
    // checked
    update_post_meta(
        $post_id,
        '_mysite_meta_hide_title',
        'on' // nothing to sanitize here
    );
}

Related Posts:

  1. post formats – how to switch meta boxes when changing format?
  2. Adding an assisting editor box to Post page
  3. Metabox Data not being saved [closed]
  4. Options to get my custom post type metadata via the WordPress API
  5. Show Custom Post Type meta boxes only on Page Edit
  6. Editing does not change post_name
  7. Add box with custom per-page properties
  8. Hide Page Title with Post Meta
  9. add post meta front end edit
  10. How to save HTML data into SQL post_content column
  11. I can not display meta value in extras.php and template-tags.php
  12. Add post meta data date to event
  13. Check if any meta on the post has value then display content
  14. Get or set values in post meta
  15. I created a Custom Meta Box but it is not displaying the value on my post page
  16. update_post_meta() throws Uncaught error: Cannot create duplicate attribute
  17. post_title in save_post action
  18. Auto save title as custom meta field value
  19. Remove action of an external plugin after checking if custom post meta is set
  20. How we insert values into database using metabox WordPress?
  21. Post meta box data not saving
  22. Filtering Tabs in a Custom Post Type’s Edit Page
  23. How do I retrieve the slug of the current page?
  24. Most efficient way to get posts with postmeta
  25. Get posts by meta value
  26. Explanation of update_post_(meta/term)_cache
  27. How to extract data from a post meta serialized array?
  28. Passing error/warning messages from a meta box to “admin_notices”
  29. Add “upload media” button in meta box field
  30. How to save an array with one metakey in postmeta?
  31. WordPress is stripping escape backslashes from JSON strings in post_meta
  32. How can I get the post ID from a WP_Query loop?
  33. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  34. What is the index [0] for on post meta fields?
  35. How to update_post_meta value as array
  36. Adding meta tag without plugin
  37. What’s the point of get_post_meta’s $single param?
  38. What is the different between an attachment in wp_posts and an attachment in wp_postmeta?
  39. How to edit a post meta data in a Gutenberg Block?
  40. Sanitizing integer input for update_post_meta
  41. Execute action after post is saved with all related post_meta records (data)
  42. Lack of composite indexes for meta tables
  43. How can I retrieve multiple get_post_meta values efficiently?
  44. Get a single post by a unique meta value
  45. if get_post_meta is empty do something
  46. How we get_post_meta without post id
  47. How get post id from meta value
  48. What is the code to get the download link for a product in WooCommerce?
  49. Safe to delete blank postmeta?
  50. Custom field metabox not showing in back-end
  51. advanced custom fields update_field for field type: Taxonomy
  52. update_post_meta not saving when value is zero
  53. Content hooks vs User hooks
  54. Meta compare with date (stored as string) not working
  55. How to hide meta box values from custom fields list?
  56. Trying to get custom post meta through Jetpack JSON API [closed]
  57. How to update/insert custom field(post meta) data with wordpress REST API?
  58. get registered metaboxes by post type or post ID
  59. How do I remove all the metaboxes for a custom post type?
  60. How To Set Custom Post Type Title Without Supports
  61. Restrict post edit/delete based on user ID and custom field
  62. get_post_meta returning empty string when data shows in the database
  63. publish_post action hook doesn’t give post_meta_data
  64. Remove WordPress.org Meta link
  65. Remove post meta keys
  66. How to access the post meta of a post that has just been published?
  67. Why time functions show invalid time zone when using ‘c’ time format?
  68. How to get meta box data to display on a page
  69. Save metabox with multiple checkbox array
  70. esc before saving or before displaying does it matter?
  71. Why is get_post_meta returning an array when I specify it as single?
  72. How to batch update post content with custom post meta value
  73. How to update/delete array in post meta value?
  74. How to get all term meta for a taxonomy – getting term_meta for taxonomy
  75. Add a meta description to home page?
  76. Get all posts from custom post type and insert in select input as metabox
  77. Detect meta value changes when post is updated (post_updated)
  78. delete unused postmeta
  79. Should I sanitize custom post meta if it is going to be escaped later?
  80. Add post meta based on another post meta value before publish post
  81. Custom meta box shown when template is chosen [duplicate]
  82. How do I retrieve multi-dimensional arrays from the wp_postmeta table, & display on a website?
  83. Dynamic page.php template for custom post types
  84. Check if meta key value already exists
  85. Front-end update_post_meta snippet displays white screen?
  86. Query between two meta values?
  87. Save both current and new version of post meta
  88. Get Advanced Custom Fields values before saving [closed]
  89. Give extra post-meta to RSS feeds
  90. How to get meta value in wp_attachment_metadata
  91. Changing document title only on a custom page template
  92. WP REST API “rest_no_route” when trying to update meta
  93. Clean up output added via wp_head()
  94. List posts under meta_value heading
  95. Create meta boxes that don’t show in custom fields
  96. Choosing a default page tempate (Classic => Gutenberg)
  97. Why am I getting an infinite loop with have_posts?
  98. get_post_meta – get a single value
  99. delete value 0 in post meta [closed]
  100. Change Default Custom Fields Metabox Name
Categories post-meta Tags metabox, page-template, post-meta, title
How to add toggle-able DOM element after nav menu item?
How can I cause run wp-cron to trigger sequentially?

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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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