2017-09-19 1 views
0

이미지 삽입시 $ html 출력에 HTML을 추가하려면 미디어 추가 팝업 화면에 체크 박스를 만듭니다.Wordpress는 미디어 추가를 사용자 정의합니다.

괜찮습니다. 그러나 일단 체크 박스가 선택된 이미지를 추가했습니다. 다시 시도해보고 확인란을 선택 취소하면 변경 사항을 등록하거나 저장하지 않습니다.

이렇게하면 항상 WP 기본값으로 되돌리기 전에 수정 된 이미지가 출력됩니다.

선택한 이미지를 선택할 때마다 선택한 후 페이지에 삽입 된 체크 표시가 미리 선택된 상태로 유지됩니다.

예상대로 선택 취소 확인란 기능을 어떻게 만듭니 까?

많은 도움을 주셔서 감사합니다. 감사합니다

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(); 

답변

0

다른 포럼의 도움을 얻었다. 그것은 사무실에서 브레인 스토밍에 박차를 가하기 위해 우리는 해결책을 찾았습니다. 이 문제를 해결하는 가장 좋은 방법인지는 모르지만 예상대로 작동하는지 확인하십시오. 마지막 함수의 끝에서

return $html;

모든 것이 제대로 작동하고 : 바로 위의

delete_post_meta($id, 'add_class', true);

:이 코드를 추가했습니다. 더 나은 해결책이 있다면 알려주십시오.

관련 문제