3

I want to add an option to the featured image metabox.

I have found a way to do this here

The problem I'm having is saving the value tot the database.

Here is what I have tried:

add_filter( 'admin_post_thumbnail_html', 'featured_image_opacity');
function featured_image_opacity( $myhtml ) {
    //$selected_option = GET META OPACITY HERE
    return $myhtml .= 'Opacity: 
    <form>
        <select>
            <option'. ($selected_option == "0.1" ? "selected" : "" ).' value="0.1">0.1</option>
        </select>
    </form>';
}
function meta_save( $post_id ) {
        if( isset( $_POST[ 'opacity' ] ) ) {update_post_meta( $post_id, 'opacity', sanitize_text_field( $_POST[ 'opacity' ] ) );}   
    }   
add_action( 'save_post', 'meta_save' );

I see the option-form and can select an option but it won't save the data.
(I have also tried to put the function within the function but that also doesn't work)

Hope somebody sees what I'm doing wrong.

1 Answer 1

3

You got it quite right, but with a few minor problems.

First, please use a uniqe prefix for your functions and values - opacity or meta_save are quite generic and could be used by other authors.

Second, the part where you get the meta opacity was missing, I added it for you - just retrieve the meta value from the current post.

Third, I then created a function to loop throug the different opacities. I too used the selected() function, but with the third parameter (echo) set to false - so selected returns my value instead of echoing it. This is a handy WordPress function to create such options and stuff like that.

That's it.

Just be sure to adjust your code when you use the post thumbnail, and be sure to get the f711_opacity meta value from the $post object, not from the thumbnail itself, as it is saved to the post.

add_filter( 'admin_post_thumbnail_html', 'f711_add_something_to_feature_thumb_box', 10, 2 ); //same as before
function f711_add_something_to_feature_thumb_box( $myhtml, $post_id ) {

    $selected_option = get_post_meta( $post_id, 'f711_opacity', true ); // get the current value
    for ( $i = 0; $i <= 1; $i = $i + 0.1 ) { //loop from 0 to 1 in 0.1 increments
        $selects .= '<option value="' . $i . '" ' . selected( $selected_option, $i, false ) . '>' . $i . '</option>'; //add a option field, and select it if it is the one saved before
    }
    //create the return html, with the selects created before
    return $myhtml .= 'Opacity: 
        <form>
            <select name="f711_opacity">
                ' . $selects . '
            </select>
        </form>';
}

// function and action to save the new value to the post
function f711_meta_save( $post_id ) {
    if( isset( $_POST[ 'f711_opacity' ] ) ) {
        update_post_meta( $post_id, 'f711_opacity', sanitize_text_field( $_POST[ 'f711_opacity' ] ) );
    }   
}
add_action( 'save_post', 'f711_meta_save' );    
4
  • Amazing. While your answer got submitted I found that I just didn't add a name attribute to the select-tag.... I'm using your solution because it's better :-) Thank you Feb 2, 2016 at 11:53
  • 1
    We might just use the post ID that's passed on to the filter callback (see update). Then we could consider adding a nonce and add further checks in the save_post callback.
    – birgire
    Feb 2, 2016 at 11:54
  • You're absolutely right @birgire - this was to provide a quick solution, and not very efficient in the topic of security and usage of arguments.
    – fischi
    Feb 2, 2016 at 12:08
  • I like that you used the built-in selected() function here and mentioned the name collision danger ;-)
    – birgire
    Feb 2, 2016 at 12:10

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.