Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have hook:

function myHook( $post_ID, $post )
{
    $data = get_post_meta($post_ID, 'data', true);
    if ($data)
    {
        _log('data:' . $data);
        /* here some checks and validation */
        if (!valid)
            wp_die('Error', 'Error',  array( 'response' => 500, 'back_link' => true ));
    }
    else 
        wp_die('Error', 'Error',  array( 'response' => 500, 'back_link' => true ));
}

add_action( 'save_post', 'myHook', 0, 2 );

Problem is that when data is incorrect and user uses back_link to correct data and tries to publish post again, his changes are not applied. Old data is taken into analysis. How can I fix this?

Thanks in advance

share|improve this question
    
What do you mean by Old Data and Correct Data ? :::: I applied your code and don't see any anomaly when hitting the Back Link; nice trick, btw! –  brasofilo Jan 13 '13 at 12:56
    
I mean when in data i have 'a', validation fails, user clicks back_link, changes 'a' to 'b' and presses 'publish', data in myHook is still 'a'. –  user1666774 Jan 13 '13 at 13:25
    
additional issue is that title (and only title) is cleared after pressing back link, but let's say it's less important –  user1666774 Jan 13 '13 at 13:41

1 Answer 1

I don't know why this issue occurs. The back_link is created in /wp-includes/functions.php. If there were a Javascript solution, it could be applied in the message of wp_die. For example:

$msg = 'Error' . "\n<p><a href='javascript:history.go(-1)'>$back_text</a></p>";
wp_die($msg, 'Error',  array( 'response' => 500 ));

But this didn't work...

What I saw is that the Title and the default Custom Fields meta box are the only ones that don't preserve the value when the browser goes back in history.
The Content, Custom Meta Boxes with Custom Fields, the Category and others preserve the value.

A workaround is putting your Custom Fields values inside a Custom Meta Box.

Observation: when hooking in the save_post action, there are some checks that you need/may do:

add_action( 'save_post', 'myHook', 0, 2 );
function myHook( $post_ID, $post )
{
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE
            or ! isset ( $_POST['post_type'] )
            or 'post' !== $_POST['post_type']
            or wp_is_post_revision( $post_ID )
        )
        return; // <-- NOT the correct action, stop hook

    $post_meta = get_post_meta($post_ID, 'data', true);
    if ($post_meta)
    {
        // _validation_function();
    }
    else 
        wp_die('Error, please enter Data Field.', 'Error',  array( 'response' => 500, 'back_link' => true ));
}
share|improve this answer
    
Thank you for answer and tip with checks. I'll try them. –  user1666774 Jan 13 '13 at 16:19
    
It works :) Thank you :) –  user1666774 Jan 19 '13 at 21:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.