Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8382987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:03:24+00:00 2026-06-09T17:03:24+00:00

Is there any way to validate custom meta box fields without using javascript. If

  • 0

Is there any way to validate custom meta box fields without using javascript. If it doesn’t validate I want to stop the post from being saved to the database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:03:25+00:00Added an answer on June 9, 2026 at 5:03 pm

    Since the ‘save_post’ action is run AFTER publishing and updating, there’s really no way of validating custom keys without a hackish alternative.

    However, I’m thinking you can mimic the functionality you want by employing ‘save_post’ in the way Viral suggested, but rather than interrupt or cancel the saving process upon a validation error, you can just delete the post altogether:

    add_action('save_post', 'validate_meta');
    function validate_meta($post_id)
    {
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
          return $post_id;
        /*USE THIS ONLY IF YOU ARE UTILIZING NONCE FIELDS IN A CUSTOM META BOX
        if ( !wp_verify_nonce( $_POST['metabox_nonce'], basename(__FILE__) ) )
          return $post_id;*/
        /*Use plugin_basename(__FILE__) if this is an actual plugin, rather than
        a part of your theme*/
    
        if ( 'page' == $_POST['post_type'] ) 
        {
            if ( !current_user_can( 'edit_page', $post_id ) )
                return $post_id;
        }
        else
        {
            if ( !current_user_can( 'edit_post', $post_id ) )
                return $post_id;
        }
    
        /*VALIDATE YOUR METADATA HERE HOWEVER YOU LIKE
        if(is_valid($_POST['metadata']))
            $validated = true;
        else
            $validated = false;
        */
    
        if(!$validated)
            wp_delete_post($post_id, true);
        else
            return $post_id;
    }
    

    The only thing to watch out for with this approach is that it will run upon both Publishing and Updating as well. You may want to consider adding a check to ensure that the post is deleted only for newly published posts, and updated posts are rolled back to a previous version and the invalid revision is deleted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to validate the contents of a CEdit box without subclassing?
Is there any way to validate using DataAnnotations in WPF & Entity Framework?
Is there any way to validate the Facebook Open Graph protocol meta tags in
Is there any way to show custom input validation message through javascript? I have
Is there any way to validate spring context xml files in eclipse? Features like:
Is there any way we can fetch X509 Public Cetrificates using c# from AD
Is there any way to stop animation in iOS 3 ? I know about:
Is there any way to have a custom recipe add some text and image
I'm looking for a way to use custom ASP.NET validators to validate input, without
Is there any way to test a vType on a value, without it being

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.