• Resolved marliesprins

    (@marliesprins)


    Hello, I am encountering a problem I can’t seem to solve…

    I created a custom post type for an agenda, where a band can enter their shows. It is important that the date of the show is entered in the following format: dd-mm-yyyy. I check the date with preg_match, and if the date is entered in any other format, the post isn’t saved. I use this code for this in the function that is hooked to the save_post hook:

    	
    if ( !preg_match( "/\d\d-\d\d-\d\d\d\d/", $_POST[ 'topsecreet_agenda_datumoptreden' ] ) ){
    
        return $post_id;
    	
    }
    

    However, if I use this code there is still a default wordpress message that says that the post is saved (though it is not), and I’d also like to add a custom message to the formfield itself, providing additional information tp the user.

    The problem is that I fail to find a way to get the information I need for this into the function that creates the metafields. If I use ( isset ( $_POST[ ‘topsecreet_agenda_datumoptreden’ ] ) ) it keeps returning false. So I assume the $_POST data gets lost somewhere between the submit button and the reload of the page… Is there a way to pass it on so I can re-use it for display in the meta-boxes?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t see how that code would prevent a post save, but I suppose it’s beside the point if it works for you. I think WP does a redirect in order to pass a message URL parameter that tells the post edit screen what message to display. If you can identify where this redirect occurs and where the URL parameter is added, you can influence what message is displayed to the user. The messages themselves can be managed through the ‘post_updated_messages’ filter.

    When a redirect occurs the values in $_POST are discarded, so before then you need to save that value somehow for use later. There’s several ways to save data so it persists between requests and redirects. Using a cookie is one option. Perhaps the best option for you would be a session variable. Data can also be saved in the DB. Transients are a temporary form of DB storage. For all it matters you could store values semi-permanently in user or post meta, but that’s probably overkill.

    Thread Starter marliesprins

    (@marliesprins)

    Thanks for answering!

    I looked into what you said and I think I am going for a transient. (never heard of it before but it seems the sensible choice, from what I am reading about it) I was hoping to avoid using the database but there doesn’t seem to be any other option. Using a session variable would be great but I read it can potentially cause a lot of trouble in wordpress so I rather not.

    I am a bit surprised there isn’t an easier way to verify input in custom meta fields though. But at least I can finish the custom post type now. Thanks ??

    By the way, return ends the execution of all code in a function, so everything that comes after a return is never executed. That’s why it’s not saved. (the code that actually saves the metadata comes after all the verification checks) I used $postid as a parameter because all other safety measures commonly used in custom post types(autosave, error with nonsefields, wrong capabilities) seem to use it.
    BUt since you mention a redirect, I guess the postid is passed here to get back to the right post.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘$_POST data cannot be retrieved/is empty?’ is closed to new replies.