functions.php Add the following code
/** * WordPress background article List settings article featured image (thumbnail) Integrated version * Plugin name:easy Thumbnail switcher*/classDoocii_easy_thumbnail_switcher { Public $add _new_str; Public $change _str; Public $remove _str; Public $upload _title; Public $upload _add; Public $confirm _str; Public function__construct () {$this->ADD_NEW_STR = __ (' Add '); $this->CHANGE_STR = __ (' Change '); $this->REMOVE_STR = __ (' Remove '); $this->upload_title = __ (' Upload featured image ')); $this->upload_add = __ (' OK '); $this->CONFIRM_STR = __ (' Are you sure? ')); Add_filter (' Manage_posts_columns ',Array($this, ' Add_column ' ) ); Add_action (' Manage_posts_custom_column ',Array($this, ' Thumb_column '), 10, 2 ); Add_action (' Admin_footer ',Array($this, ' Add_nonce ' ) ); Add_action (' Admin_enqueue_scripts ',Array($this, ' Scripts ' ) ); Add_action (' Wp_ajax_ts_ets_update ',Array($this, ' Update ' ) ); Add_action (' Wp_ajax_ts_ets_remove ',Array($this, ' Remove ' ) ); Add_image_size (' Ts-ets-thumb ', 75, 75,Array(' Center ', ' center ' ) ); } /** * Security check*/ Public functionadd_nonce () {Global $pagenow; if($pagenow!== ' edit.php ' ) { return; } Wp_nonce_field (' Ts_ets_nonce ', ' ts_ets_nonce ' ); } /** * Load Script*/ Public functionScripts$pagenow ) { if($pagenow!== ' edit.php ' ) { return; } wp_enqueue_media (); Wp_enqueue_script (' Doocii-ets-js ', Get_template_directory_uri (). '/js/script.js ',Array(' jquery ', ' media-upload ', ' Thickbox '), ' 1.0 ',true ); Wp_localize_script (' Doocii-ets-js ', ' ets_strings ',Array( ' Upload_title ' =$this->upload_title, ' upload_add ' =$this->upload_add, ' confirm ' =$this->CONFIRM_STR, ) ); } /** * The action which is added to the post row actions*/ Public functionAdd_column ($columns ) { $columns[' ts-ets-option '] = __ (' thumbnail '); return $columns; } /** * Display column*/ Public functionThumb_column ($column,$id ) { Switch($column ) { Case' Ts-ets-option ':if(Has_post_thumbnail ()) {The_post_thumbnail (' Ts-ets-thumb ' ); Echo' <br> '; Echo sprintf(' <button type= "button" class= "Button-primary ts-ets-add" data-id= "%s" >%s</button> ", Esc_attr ($id),$this-change_str); Echo sprintf(' <button type= "button" class= "Button-secondary ts-ets-remove" data-id= "%s" >%s</button> ", Esc_attr ($id),$this-remove_str); } Else { Echo sprintf(' <button type= "button" class= "Button-primary ts-ets-add" data-id= "%s" >%s</button> ", Esc_attr ($id),$this-add_new_str); } Break; } } /** * Ajax save update thumbnail*/ Public functionUpdate () {//Check whether all required data is set or not if( !isset($_post[' Nonce ']) || !isset($_post[' post_id ']) || !isset($_post[' thumb_id ']) {Wp_die (); } //Validation if(!wp_verify_nonce ($_post[' nonce '], ' ts_ets_nonce ') ) {Wp_die (); } $id=$_post[' post_id ']; $thumb _id=$_post[' thumb_id ']; Set_post_thumbnail ($id,$thumb _id ); EchoWp_get_attachment_image ($thumb _id, ' Ts-ets-thumb ' ); Echo' <br> '; Echo sprintf(' <button type= "button" class= "Button-primary ts-ets-add" data-id= "%s" >%s</button> ", Esc_attr ($id),$this-change_str); Echo sprintf(' <button type= "button" class= "Button-secondary ts-ets-remove" data-id= "%s" >%s</button> ", Esc_attr ($id),$this-remove_str); Wp_die (); } /** * After Ajax callback delete thumbnail image*/ Public functionRemove () {//Check If all required data is set or not if( !isset($_post[' Nonce ']) || !isset($_post[' post_id ']) {Wp_die (); } //Verify nonce if(!wp_verify_nonce ($_post[' nonce '], ' ts_ets_nonce ') ) {Wp_die (); } $id=$_post[' post_id ']; Delete_post_thumbnail ($id ); Echo sprintf(' <button type= "button" class= "Button-primary ts-ets-add" data-id= "%s" >%s</button> ", Esc_attr ($id),$this-add_new_str); Wp_die (); } }NewDoocii_easy_thumbnail_switcher ();
The following code is saved as script.js and saved to the subject name/js Directory
Visit: HTTP//your domain/wp-admin/edit.php can
(function($) { "Use Strict"; if(typeofTs_ets = = = ' undefined ') {window.ts_ets= {}; Ts_ets.upload_frame=false; } $ (document). On (' Click ', ' Button.ts-ets-remove ',function() {ts_ets.tmp_id= $( This). Data (' ID '); Ts_ets.tmp_parent= $( This). Closest (' Td.ts-ets-option '); if( !confirm (ets_strings.confirm)) { return; } $.ajax ({Url:ajaxurl, method:"POST", data: {action:' Ts_ets_remove ', Nonce: $ (' #ts_ets_nonce '). Val (), post_id:ts_ets.tmp_id}, Success:function(data) {if(Data! = ") {ts_ets.tmp_parent.html (data); } } }); }); $ (document). Ready (function() {ts_ets.upload_frame=Wp.media ({title:ets_strings.upload_title, button: {text:ets_strings.upload_a DD,}, multiple:false }); Ts_ets.upload_frame.on (' SELECT ',function() {ts_ets.selection= Ts_ets.upload_frame.state (). Get (' selection '); Ts_ets.selection.map (function(attachment) {if(attachment.id) {$.ajax ({Url:ajaxurl, method:
"POST", data: {action:' Ts_ets_update ', Nonce: $ (' #ts_ets_nonce '). Val (), post_id:ts_ets.tmp_id, thumb_id:attachment.id }, Success:function(data) {if(Data! = ") {ts_ets.tmp_parent.html (data); } } }); } }); }); }); $ (document). On (' Click ', ' Button.ts-ets-add ',function(e) {e.preventdefault (); ts_ets.tmp_id= $( This). Data (' ID '); Ts_ets.tmp_parent= $( This). Closest (' Td.ts-ets-option '); if(ts_ets.upload_frame) {ts_ets.upload_frame.open (); } }); }) (jQuery);
WordPress background article List settings article featured image (thumbnail) integrated version