I'm trying to write a plugin that adds a value from a meta box to a post on save_post. But I can't figure out how to get the value from the form field in the meta box This is the relevant code:

function sw_add_document_meta_boxes() {
    if (get_current_screen()->id == 'dokument') {
        add_meta_box('access_level', 'Tilgangsnivå', 'sw_ac_meta_box');

function sw_ac_meta_box() {
    $html = '<p class="description">';
    $html .= 'Velg laveste tilgangsnivå';
    $html .= '</p>';
    $html .= '<select name="access_level" id="access_level">';
    $html .= '<option value="4">Ansatt</option>';
    $html .= '<option value="3">Fagansvarlig</option>';
    $html .= '<option value="2">Daglig leder</option>';
    $html .= '<option value="1">Superbruker</option>';
    $html .= '</select>';
    echo $html;

function sw_ac_set_access_level($id) {
    $meta_value = 
    add_post_meta($id, 'access_level', $meta_value, true);

add_action('add_meta_boxes', 'sw_add_document_meta_boxes');
add_action('save_post', 'sw_ac_set_access_level');

I guess my question is, what should I write on the line '$meta_value =' in the 'sw_ac_set_access_level()' function? Take into account that I'm a total wordpress noob, so I might be on the wrong track entirely.

3 Answers 3


If form <select> is part of the form element that get submitted, then you should check the $_POST['access_level'].


It should be like this.

function sw_ac_set_access_level($id) {
    $meta_value = $_POST["access_level"]; // name of select
    update_post_meta($id, 'access_level', $meta_value );

You can get mor detail here https://codex.wordpress.org/Plugin_API/Action_Reference/save_post


You should validate $_POST['access_level'] first to save its value

You can try this

function sw_ac_set_access_level($id) {
  if(isset($_POST['access_level']) && !empty($_POST['access_level'])){
    update_post_meta($id, 'access_level', $_POST['access_level'] );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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