问题描述

我正在寻找适应现有的 forum-like 插件,它没有附加介质的功能。

该插件作为自定义帖子类型,因此将其作为将图像附加到帖子的方式为”simple” 。

我只关心附加图像而不是任何文件类型,但插件使用 wp_editor,因此解决方案应该以某种方式与之集成。只要解决方案能够将图像的缩略图插入到 tinyMCE 文本区域,我就不会过分地创建一个 tinyMCE 按钮。

请注意,我指的是我网站的 front-end,而不是管理区域。

在一个绝对理想的情况下,我想这种情况发生:

  • 用户点击 「提问」

  • 使用输入他们的帖子详细信息

  • 用户单击 tinyMCE 界面上的一个按钮,类似于 StackExchange,要求用户上传文件。

  • 系统然后将 correctly-sized 缩略图插入到 tinyMCE 文本区域,将文件缩小为缩略图大小

  • 点击此图片应提供与帖子中图像附件相同的功能

  • 用户可以再次单击以插入新图像

  • 用户还可以根据需要从 tinyMCE textarea 中删除图像

但是,我很高兴将 tinyMCE 按钮设为外设 – 如果”file upload” 盒更容易,那没关系。

我遇到了 this link,但是我总是担心在 t’interwebs 上阅读 WordPress 文章,因为我永远不会太确定他们的安全性,也没有任何想象力的 php 安全专家。

提前致谢,

最佳解决方案

我认为最简单的方法是,因为您已经在使用 wp_editor 功能,就是将媒体按钮包含在 WP_Editor 实例中 – 这样您就可以使用本机功能,包括 「插入后贴」 按钮免费。

这样做显然取决于您正在尝试使用的插件。但是,这应该让你开始。在页面模板中包含这样的代码以显示编辑器,您将在页面上获得一个编辑器。将其包含在表单中并处理结果是另一个不详细的步骤。

// Define the global variable $post_id - this is used by the media uploader
// to attach uploads to a specific post (so that the uploader can see uploads
// attached to this post and not others)
global $post_id;
$post_id = $post->ID; // should be the ID of the new post created

// Now filter the list of tabs available in the media editor.
// Remove everything but the "From Computer" option.

add_filter( 'media_upload_tabs', 'wpse42068_remove_additional_tabs' );

function wpse42068_remove_additional_tabs( $_default_tabs ) {
    return array( 'type' => __('From Computer') );
}

// Now just include the WP_Editor. See
// http://codex.wordpress.org/Function_Reference/wp_editor
// for settings available
wp_editor( '', 'posteditor', array( 'media_buttons' => true ) );

定义帖子 ID 可能是关键部分,并且您如何执行此操作将取决于您的功能的逻辑。我会建议:

  • 首先访问此页面时创建一个 auto-draft,并保存返回在全局 $ post_id 变量中的帖子 ID 。

  • 然后在提交表单时保存具有相同 ID 的创建的帖子。

次佳解决方案

也许这不是您的理想解决方案,但值得一提。得到它的谷歌搜索,但不幸的是我忘了 url 。附件部分与 @goldenapples 文章中的附件类似。

这是基本的功能。

function attach_uploads($uploads,$post_id = 0){
    $files = rearrange($uploads);
    if($files[0]['name']==''){
        return false;
    }
    foreach($files as $file){
        $upload_file = wp_handle_upload( $file, array('test_form' => false) );
        $attachment = array(
        'post_mime_type' => $upload_file['type'],
        'post_title' => preg_replace('/.[^.]+$/', '', basename($upload_file['file'])),
        'post_content' => '',
        'post_status' => 'inherit'
    );
        $attach_id = wp_insert_attachment( $attachment, $upload_file['file'], $post_id );
        $attach_array[] = $attach_id;
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata( $attach_id, $upload_file['file'] );
        wp_update_attachment_metadata( $attach_id, $attach_data );
    }
    return $attach_array;
}

ajax 功能

add_action('wp_ajax_attach_file', 'process_attach_file');
function process_attach_file() {

    // add some filter and validation on the id and the files here
    $post_id = $_POST['post_id'];
    $files = $_FILES['profile-picture'];

    // insert attachment
    $attached_files = attach_uploads($files,$post_id);

    // set the first file as post thumbnail
    if($attached_files){
        set_post_thumbnail( $post_id, $attached_files[0] );
    }

    // now all you have to do is set the response data

}

标记

<form id="upload-form" action="<?php echo admin_url('admin-ajax.php'); ?>" method="post" class="form" enctype="multipart/form-data" >
    <label for="profile-picture">Foto Profil</label>
    <input type="file" id="profile-picture" name="profile-picture[]" size="40" multiple />
    <?php wp_nonce_field( // intention nonce); ?>
    <input name="action" value="attach_file" type="hidden">
    <input name="post_id" value="12" type="hidden">
</form>

希望这个帮助

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。