BoBo_ Posted September 19, 2017 Share Posted September 19, 2017 I am creating a checkbox in the Add Media popup screen in Page Editor of WordPress to add HTML to the $html output when inserting an image. This is working fine. However, once I have added an image with the checkbox selected. If I try and add it again, and deselect the checkbox, it won't register or save the change. Thus always outputting the modified image instead of reverting back to WP default once unchecked. Plus the check mark after I have selected it and inserted to page will stay pre-selected every time I select that image. How do I make the unselect checkbox function as would be expected? Much appreciate the help. Thanks class media_uploader_cb{ function __construct(){ // attach our function to the correct hook add_filter('attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2); //save attachment field add_filter( 'attachment_fields_to_save', array( $this, 'attachment_fields_to_save' ), 10, 2); //add custom css class add_filter( 'media_send_to_editor', array( $this, 'media_send_to_editor' ), 10, 2 ); } /** * Adding our custom checkbox field to the $form_fields array * * @param array $form_fields * @param object $post * @return array */ function attachment_fields_to_edit($form_fields, $post) { $form_fields['add_class']['label'] = __("Add SEO Data"); $form_fields['add_class']['input'] = 'html'; $form_fields['add_class']['html'] = '<input type="checkbox" value="1" name="attachments['.$post->ID.'][add_class]" id="attachments['.$post->ID.'][add_class]" '.checked( 1, get_post_meta($post->ID, 'add_class', true), false ).'/>'; return $form_fields; } /** * Saving our custom checkbox field * @param array $post * @param array $attachment * @return array */ function attachment_fields_to_save($post, $attachment) { if( isset($attachment['add_class']) ){ update_post_meta($post['ID'], 'add_class', $attachment['add_class']); } return $post; } /** * Adding our custom css class based on checkbox field * * @param string $html * @param int $id * @return string */ function media_send_to_editor( $html, $id ) { //only add class if the checkbox was checked if ( 1 == (int)get_post_meta($id, 'add_class', true) ){ //change this to whatever you want $seo_data_to_add = 'custom="HTML Output Here""'; // THIS SHOULD BE THE CHECKBOX get_post_meta($id, 'add_class', true); $attachment_title = get_the_title($id); $title = 'title="'.$attachment_title .' by '. get_option('ews_opt_branding') .'"'; $html = str_replace('<img', '<img '.$title.' '.$seo_data_to_add.'', $html); } return $html; } } new media_uploader_cb(); Quote Link to comment Share on other sites More sharing options...
Solution maxxd Posted September 20, 2017 Solution Share Posted September 20, 2017 (edited) Check boxes aren't be set in $_POST (so it won't be in $attachment, I believe) if they're not checked on the form. So, the isset() won't update the record if the user deselects the check box. What I'd recommend is get the existing post_meta for the record before you try to update. If the value exists, your system will know that the user has deselected the previously selected checkbox and you can delete the post_meta record. If the post_meta data didn't previously exist, then the user never has checked the box and you can skip the update. Edited September 20, 2017 by maxxd 1 Quote Link to comment Share on other sites More sharing options...
BoBo_ Posted September 20, 2017 Author Share Posted September 20, 2017 @maxxd , yous spurred some brainstorming at the office and we came up with a solution. Not sure if this is the best way to resolve it but it is working as expected. I added this code: delete_post_meta($id, 'add_class', true); just above the: return $html; at the end of the last function and everything is behaving properly. If there is a better solution, please let me know. Otherwise, thank you very much!! Quote Link to comment Share on other sites More sharing options...
maxxd Posted September 20, 2017 Share Posted September 20, 2017 I wouldn't recommend deleting the value after you populate the edit form - what if the user changes his/her mind and navigates away? You've just deleted the value from the database. I'd do what I recommended in my earlier post - get the value of 'add_class' post_meta before you try to update the record. If it's not empty, you know there was a previously existing record that needs to be updated whether the value exists in $attributes or not. If is is empty and the value in attributes is not, insert that value. Then return $post. Quote Link to comment Share on other sites More sharing options...
BoBo_ Posted September 20, 2017 Author Share Posted September 20, 2017 @maxxd , I'm not sure how I would code that. However for our purpose, deleting the value, as far as I can tell, has no consequence. We do not want that output there except for a one off situation. So it's usually only needed in a one time instance and every other time an image is inserted it should be inserted as WP would insert it by default. So it appears that unless you check and import, it always defaults back to it's original functionality, which is great. If there is any serious problem with doing it this way, I am willing to update it. But I would need clear code revisions rather than ideas because I am not talented enough to execute what you are saying. Thanks again. Your response and help has been great! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.