WordPress - 将页面/帖子附件功能添加到媒体编辑屏幕

时间:2014-10-06 18:03:01

标签: wordpress-plugin 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

2 个答案:

答案 0 :(得分:1)

我在我的functions.php文件中使用这些组合功能,为表中的每个媒体项添加媒体库中的链接(管理区域中的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 );

我知道这并没有把选项放在你所描述的地方,但这是一个正确方向的开始。

<强>更新

请注意,我将保留上述代码,以防有人想要在其库表中重新附加选项。

关于你的问题......以下是代码,解释如下:

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

<强> BREAKDOWN

第一个功能与上面代码中的功能非常相似。我相信我做的唯一更改是添加检查以确保我们只在编辑附件页面上运行此代码。你可能需要调整它,因为我测试了它,但不完全。

我也改变了调用findPosts.open()的方式。我们现在传递一个名为&#39; action&#39;的变量。并将其设置为&#39; find_posts&#39;所以我们以后可以检查一下......

因此,第一个函数只显示附件已经附加到的帖子,并允许您根据需要重新分配它...或者显示一个选项来附加它。重新附加和附加只是链接,点击后,启动findPosts.open(),在页面上查找隐藏的div /输入...我们还没有创建它们。

第二个功能是关键...首先,您需要将脚本和一个样式排入队列。这里的导入代码是find_posts_div()调用。这就是魔术发生的原因,但这一切都是在弹出窗口中创建隐藏的div和表单字段,等待被调用(我们在第一个函数中的锚点)。这需要在一个单独的函数中,以便我们可以使用add_filter来调用post表单的函数OUTSIDE。

起初我试图将它们放在一个功能中。浏览器会删除我们的<form>代码,因为我们正在尝试将表单放在另一个表单(帖子表单)中,这是一个不可以。因此,通过在admin_footer中调用它,我们将代码加载到表单之外。

在表单中包装find_posts_div()允许我们将该表单的结果提交到我们想要的地方,以便随意使用它。在我们的例子中,我们创建了一个新页面(test.php)并在那里提交结果,因此我们可以做我们需要的。

到目前为止,test.php页面如下:

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

这将显示$ _POST的所有值...随意添加更多隐藏值以及不是,但是&#39; found_post_id&#39; value是弹出窗口中所选值的post id。然后你可以查看upload.php的第103-141行,找到进行实际重新连接的代码。可能有一个钩子或更好的东西,但我没有时间去看。

希望这有帮助!

答案 1 :(得分: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 );
    }
}

再次感谢Daniel,你的帮助。非常感谢!

相关问题