$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>metabox - save meta data of custom post type: WP_Error has no effect, even if insufficient capabilities|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

metabox - save meta data of custom post type: WP_Error has no effect, even if insufficient capabilities

matteradmin9PV0评论

The function should give the user a feedback, if the data changes of the meta box of a CPT couldn't be saved. In the following code the function returns a WP_Error if the current_user doesn't have sufficient capabilities and so the update_post_meta call won't be executed. That all is working as expected. The meta data will not be changed on saving the post. But no error or other message is shown, instead on top of the post a message appears inside a left green border box "Post updated." That may not happen. That is the wrong message. How can I change this? How should I write the code?

This is the code structure - all the vars inside the function are visible: (important is only the line with "return new \WP_Error ...")

class Foo{

function save_meta() {

         // some code
    ...

         if ( ! current_user_can( $capability, $post_id ) ) {
                    return new \WP_Error( 'capability-error', __( "You have not sufficient rights to save the data", TEXTDOMAIN ) );
            }

        //other code
    ...
        update_post_meta( $post_id, $key, sanitize_text_field( $_REQUEST[ $key ] ) );

    //other code
    ...

    }
}

add_action( 'save_post_{post_type}', [instance of class foo], 'save_meta' );
Post a comment

comment list (0)

  1. No comments so far