2014-10-06 124 views
2

有沒有辦法將頁面附件功能添加到編輯媒體屏幕?所以,如果我添加/編輯媒體項目,我也可以附加/重新附加到一個頁面。WordPress - 爲媒體編輯屏幕添加頁面/張貼附件功能

下面的代碼我到目前爲止:

function my_post_submitbox_misc_actions($id){ 
    $post = get_post($id); 
    if($post->post_parent > 0) { 
     if(get_post($post->post_parent)) { 
      $title = _draft_or_post_title($post->post_parent); 
     } 
    ?> 
    <div class="misc-pub-section misc-pub-attachment"> 
     Attached to: <strong><a href="<?php echo get_edit_post_link($post->post_parent); ?>"><?php     echo $title ?></a></strong> 
    (<a class="hide-if-no-js" onclick="findPosts.open(''media[]'', '<?php echo $post->ID ?>' );return false;" href="#the-list"><?php _e('Re-Attach'); ?></a>) 
    </div> 
    <?php 
    } else { ?> 
     <?php _e('(Unattached)'); ?><br /> 
     <a class="hide-if-no-js" onclick="findPosts.open('media[]', '<?php echo $post->ID ?>' );return false;" href="#the-list"><?php _e('Attach'); ?></a> 
    <?php 
    } 
} 
add_action('attachment_submitbox_misc_actions', 'my_post_submitbox_misc_actions'); 

這是我的調用數據庫:

global $wpdb; 

$wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment'", $parent_id)); 

    if (isset($attached)) { 
    $location = 'upload.php'; 
    if ($referer = wp_get_referer()) { 
     if (false !== strpos($referer, 'upload.php')) 
     $location = $referer; 
    } 

    $location = add_query_arg(array('attached' => $attached) , $location); 
    wp_redirect($location); 
    } 

enter image description here

回答

0

所以在丹尼爾和一些研究的幫助下,我終於能夠使它工作了!這裏是我的代碼現在的樣子:

function spd_submitbox_misc_actions($id) { 
    global $pagenow, $typenow; 
    // We only want to run the code on a specific page 
    if($pagenow != 'post.php' || $typenow != 'attachment') { 
     return; 
    } 
    $post = get_post($id); 

    if($post->post_parent > 0) { 
     if(get_post($post->post_parent)) { 
      $title = _draft_or_post_title($post->post_parent); 
     } 

     ?> 
     <div class="misc-pub-section misc-pub-attachment"> 
      Attached to: <strong><a href="<?php echo get_edit_post_link($post->post_parent); ?>"><?php echo $title ?></a></strong> 
      <a class="hide-if-no-js" onclick="findPosts.open('action','find_posts');return false;" href="#"><?php _e(' (Re-Attach) '); ?></a> 
     </div> 
    <?php 
    } else { ?> 
     <div class="misc-pub-section misc-pub-attachment"> 
      Attached to: (Unattached) <a class="hide-if-no-js" onclick="findPosts.open('action','find_posts');return false;" href="#"><?php _e('Attach'); ?></a> 
     </div> 
    <?php 
    } 
} 
add_action('attachment_submitbox_misc_actions', 'spd_submitbox_misc_actions'); 
// Function to call the find_posts_div pop up OUTSIDE the post form 
function spd_post_submitbox_misc_form() { 
    global $pagenow, $typenow; 

    // We only want to run on edit media page 
    if($pagenow != 'post.php' || $typenow != 'attachment') 
     return; 

    // Enqueue our scripts 
    wp_enqueue_style('thickbox'); 
    wp_enqueue_script('thickbox'); // needed for find posts div 
    wp_enqueue_script('media'); 
    wp_enqueue_script('wp-ajax-response'); 
    $send_to = get_template_directory_uri() . '/inc/spdfunctions/spd_post_actions.php'; 

    ?> 
    <form name="attach-to-post" method="post" action="<?php echo $send_to; ?>"> 
     <?php find_posts_div(); ?> 
     <input type="hidden" name="attach-to-post" value="attach" /> 
     <input type="hidden" name="attachment-id" value="<?php the_ID(); ?>" /> 
    </form> 
<?php 
} 
add_filter('admin_footer','spd_post_submitbox_misc_form'); 

然後處理提交:再次

if ($_REQUEST['action'] && isset($_POST['attach-to-post'])) { 

    $location = $_SERVER['DOCUMENT_ROOT']; 
    include($location . '/wp-load.php'); 

    $parent_id = $_POST['found_post_id']; 
    $attach_id = (int) $_POST['attachment-id']; 

    $attached = $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN (%d)", $parent_id, $attach_id)); 

    //redirect back to edit media screen 
    if (isset($attached)) { 
     $referer = wp_get_referer(); 
     $location = add_query_arg(array('attached' => $attached) , $referer); 
     wp_redirect($location); 
    } 
} 

感謝,丹尼爾,你的幫助。非常感謝!

+0

沒問題...很高興它解決了!附:在您的解決方案中,您將調用spd_submitbox_misc_actions和spd_post_submitbox_misc_form兩次。更新您的代碼以刪除重複的功能,以便其他人不會感到困惑。 – 2014-10-10 15:33:17

1

我已經使用這些組合功能,在我的功能.php文件,爲表格中的每個媒體項目添加媒體庫中的鏈接(admin區域中的upload.php),以將該項目附加/重新附加到任何內容。

// Functions to allow one to re-attach an image to a post 
function upload_columns($columns) { 
    // Unset($columns['parent']); 
    $columns['better_parent'] = 'Re-Attach'; 
    return $columns; 
} 
add_filter('manage_upload_columns', 'upload_columns'); 
function media_custom_columns($column_name, $id) { 
    $post = get_post($id); 
    if($column_name != 'better_parent') 
     return; 
    if($post->post_parent > 0) { 
     if(get_post($post->post_parent)) { 
      $title = _draft_or_post_title($post->post_parent); 
     } 
     ?> 
     <strong><a href="<?php echo get_edit_post_link($post->post_parent); ?>"><?php echo $title ?></a></strong>, <?php echo get_the_time(__('Y/m/d')); ?> 
     <br /> 
     <a class="hide-if-no-js" onclick="findPosts.open('media[]', '<?php echo $post->ID ?>');return false;" href="#the-list"><?php _e('Re-Attach'); ?></a> 
     <?php 
    }else { 
     ?> 
     <?php _e('(Unattached)'); ?><br /> 
     <a class="hide-if-no-js" onclick="findPosts.open('media[]', '<?php echo $post->ID ?>');return false;" href="#the-list"><?php _e('Attach'); ?></a> 
     <?php 
    } 
} 
add_action('manage_media_custom_column', 'media_custom_columns', 10, 2); 

我知道這不會在您描述的位置放置選項,但它是一個正確方向的開始。

UPDATE:

請注意,我將離開,以防有人在上面的代碼要在他們的庫表中的重新連接選項。

至於你的問題...這裏是代碼,解釋如下:

function my_post_submitbox_misc_actions($id) { 
    global $pagenow, $typenow; 
    // We only want to run the code on a specific page 
    if($pagenow != 'post.php' || $typenow != 'attachment') { 
     return; 
    } 
    $post = get_post($id); 

    if($post->post_parent > 0) { 
     if(get_post($post->post_parent)) { 
      $title = _draft_or_post_title($post->post_parent); 
     } 
     ?> 
     <div class="misc-pub-section misc-pub-attachment"> 
      Attached to: <strong><a href="<?php echo get_edit_post_link($post->post_parent); ?>"><?php echo $title ?></a></strong> 
      (<a class="hide-if-no-js" onclick="findPosts.open('action','find_posts');return false;" href="#"><?php _e('Re-Attach'); ?></a>) 
     </div> 
     <?php 
    } else { 
     _e('(Unattached)'); ?><br /> 
     <a class="hide-if-no-js" onclick="findPosts.open('action','find_posts');return false;" href="#"><?php _e('Attach'); ?></a> 
     <?php 
    } 
} 
add_action('attachment_submitbox_misc_actions', 'my_post_submitbox_misc_actions'); 
// Function to call the find_posts_div pop up OUTSIDE the post form 
function my_post_submitbox_misc_form() { 
    global $pagenow, $typenow; 
    // We only want to run the code on a specific page 
    if($pagenow != 'post.php' || $typenow != 'attachment') { 
     return; 
    } 
    // Enqueue our scripts 
    wp_enqueue_style('thickbox'); 
    wp_enqueue_script('thickbox'); // needed for find posts div 
    wp_enqueue_script('media'); 
    wp_enqueue_script('wp-ajax-response'); 
    ?> 
    <form name="plugin_form" id="plugin_form" method="post" action="/wp-content/themes/<?php echo get_template() . '/test.php'; ?>"> 
     <?php 
      wp_nonce_field('plugin_nonce'); 
      find_posts_div(); 
     ?> 
    </form> 
    <?php 
} 
add_filter('admin_footer','my_post_submitbox_misc_form'); 

擊穿

第一個功能是非常相似,你已經在你的代碼之上。我相信我所做的唯一更改是添加檢查以確保我們只在編輯附件頁上運行此代碼。您可能需要調整它,但我並未完全測試。

我也改變了我們稱之爲findPosts.open()的方式。我們現在傳遞一個名爲'action'的變量,並將其值設置爲'find_posts',以便稍後檢查它...

因此,第一個函數只顯示附件已附加到的帖子, - 如果需要,指定它...或顯示一個選項來附加它。重新附加和附加鏈接只是點擊時,啓動findPosts.open(),它尋找頁面上隱藏的div /輸入...我們還沒有創建這些。

第二個功能是關鍵......首先,你需要排隊腳本和一個風格。這裏輸入的代碼是find_posts_div()調用。這就是讓魔法發生的原因,但所有這些都是在彈出窗口中創建隱藏的div和表單域,只是等待被調用(我們在第一個函數中的錨點)。這需要在一個單獨的函數中,以便我們可以使用add_filter來調用表單外部的函數。

起初我試圖把它全部集中在一個功能中。瀏覽器會去掉我們的<form>標籤,因爲我們試圖將表單放在另一個表單(帖子表單)中,這是一個不行。因此,通過在admin_footer中調用它,我們將代碼加載到表單之外。

在表單中包裝find_posts_div()使我們能夠提交表單,我們希望在以往任何時候的結果,做任何我們想做的事情。在我們的例子中,我們創建一個新頁面(test.php)並在那裏提交結果,這樣我們就可以做我們需要的。

到目前爲止,test.php的頁面如下:

<?php 
    echo '<pre>';print_r($_POST);echo '</pre>'; 
    die(); 
?> 

這將顯示的$ _ POST所有值...隨意添加更多的隱藏價值並沒有什麼,但' found_post_id'值是從彈出的選定值中獲得的帖子ID。然後你可以查看upload.php的第103 - 141行來找到代碼來做實際的重新連接。可能有鉤子或更好的東西,但我沒有時間看。

希望這會有所幫助!

+0

非常感謝您的回覆。到目前爲止,我已將它放置在「編輯媒體」屏幕上的正確位置,但是當我單擊「附加/重新附加」時,帖子對話框不會打開。我需要排隊腳本嗎? – Shapada 2014-10-07 19:52:27

+0

不...你不需要任何東西,但這個代碼。也許別的地方還有其他問題?衝突?檢查並看看你是否在firebug中得到任何JS錯誤?確保你同時調用了「add_filter」和「add_action」,並且函數名是正確的。 – 2014-10-08 01:34:21

+0

如果我沒有在媒體庫中創建自定義列,爲什麼還需要這些過濾器/操作掛鉤? – Shapada 2014-10-08 15:29:27

相關問題