WordPress.org

Ready to get started?Download WordPress

Forums

Call to undefined function wp_verify_nonce() when using do_action() on save_post (2 posts)

  1. TimG1
    Member
    Posted 1 year ago #

    Hello everyone!

    I followed this great tutorial by Justin Tadlock to add some custom metaboxes to a custom post type. It worked great! I'm trying to take things a step further and pass some additional arguments to the function that is doing the updating. But I'm getting this error when saving the post.

    Fatal error: Call to undefined function wp_verify_nonce()

    From the tutorial I'm trying to pass additional arguments to the function smashing_save_post_class_meta().

    Here is an example of what I have in my code.

    function my_save_function($post_id, $post, $my_custom_variable) {
        // verify nonce
        if ( !isset( $_POST['my_nonce'] ) || !wp_verify_nonce( $_POST['my_nonce'], basename( __FILE__ ) ) )
            return $post_id;
    ...
    }
    
    add_action('save_post', 'my_save_function', 10, 3);
    do_action('save_post', $post_id, $post, $my_custom_variable);

    The third argument $my_custom_variable gets passed okay. If I comment out the line with wp_verify_nonce(), everything works as expected and I can see my $my_custom_variable if I print_r().

    It seems that by using the do_action() it changes the order of execution somehow so that my save function is executed before the WordPress functions are fully loaded.

    I've tried changing the 10 on my add_action() to change the priority and that didn't do anything.

    How do I make sure WP is fully loaded before my save function runs? I think the answer is to use init or wp_loaded() but I'm not sure how to use these. The codex for wp_loaded is empty.

    Can anyone help point me in the right direction?

    Many thanks,
    -Tim

  2. dung.truong
    Member
    Posted 1 year ago #

Topic Closed

This topic has been closed to new replies.

About this Topic