将图像从自定义域复制到另一个自定义域

时间:2018-09-12 作者:730wavy

在我的wordpress网站上,我有一个前端表单,允许用户创建帖子并上传自定义帖子类型“属性”的照片。

使用以下功能-

$property_sights_value = get_post_meta($Property_Id, \'imic_property_sights\', false);

if (!empty($_FILES[\'sightMulti\'][\'tmp_name\'][0])) {
                    $i = 1;
                    $files = $_FILES[\'sightMulti\'];
                    foreach ($files[\'name\'] as $key => $value) {
                        if ($files[\'name\'][$key]) {
                            $file = array(
                                \'name\' => $files[\'name\'][$key],
                                \'type\' => $files[\'type\'][$key],
                                \'tmp_name\' => $files[\'tmp_name\'][$key],
                                \'error\' => $files[\'error\'][$key],
                                \'size\' => $files[\'size\'][$key]
                            );
                            $_FILES = array("sight" . $i => $file);
                            $newuploadMulti = sight("sight" . $i, $pid);
                            if ($i == 1) {
                                update_post_meta($pid, \'_thumbnail_id\', $newuploadMulti);

                            }
                            add_post_meta($pid, \'imic_property_sights\', $newuploadMulti, false);
                        }
                        $i++;
                    }
                }
        }
然后在另一个页面上,除了一个不同的自定义帖子类型“v\\u editor”和一个不同的上传函数,它将图像转换为。png格式并去掉原始格式-

$vid_pix = get_post_meta($v_Id, \'vid_pix\', false);

    if (!empty($_FILES[\'vidPix\'][\'tmp_name\'][0])) {
                        $i = 1;
                        $files = $_FILES[\'vidPix\'];
                        foreach ($files[\'name\'] as $key => $value) {
                            if ($files[\'name\'][$key]) {
                                $file = array(
                                    \'name\' => $files[\'name\'][$key],
                                    \'type\' => $files[\'type\'][$key],
                                    \'tmp_name\' => $files[\'tmp_name\'][$key],
                                    \'error\' => $files[\'error\'][$key],
                                    \'size\' => $files[\'size\'][$key]
                                );
                                $_FILES = array("sight" . $i => $file);
    add_filter( \'upload_dir\', \'wpse_141088_upload_dir\' );
    add_filter(\'intermediate_image_sizes_advanced\', \'no_image_resizing\');

                            $mfile =  wp_handle_upload($files, $upload_overrides );             

                                $newvidPix = sight("sight" . $i, $v_Id);
    remove_filter( \'upload_dir\', \'wpse_141088_upload_dir\' );
    remove_filter(\'intermediate_image_sizes_advanced\', \'no_image_resizing\');
    // Convert the image to PNG and delete the old image.
    attachment_to_png( $newvidPix );

                                if ($i == 1) {
                                    update_post_meta($v_Id, \'_thumbnail_id\', $newvidPix);
                                }
                                    add_post_meta($v_Id, \'vid_pix\', $newvidPix, false);
                            }
                            $i++;
                        }
                    }
这两个函数都工作得很好,但是,我想做的是--

在“v\\u editor”cpt表单上,我试图创建一个选择下拉列表,让用户从“property”cpt中选择自己的帖子,并获取保存在那里的图像以复制使用--

<?php query_posts(array(\'post_type\'=>\'property\',\'order\'=>\'desc\',\'author\'=>$current_user->ID,\'posts_per_page\' => 25,\'paged\' => get_query_var(\'paged\'),\'post_status\' => array(\'publish\',\'pending\',\'draft\')));
if(have_posts()): 

echo \'<select id="property_posts" name="property_posts">\';

while(have_posts()):the_post();

echo \'<option value="\'.get_the_ID().\'">\'.get_the_title().\'</option>\';

endwhile;

echo \'</select>\';

endif; ?>

<button type="button" id="import_pics">Import</button>
上载时,两个图像都存储到元密钥中-

PROPERTY CPT - $property_sights_value = get_post_meta($Property_Id, \'imic_property_sights\', false);

V_EDITOR CPT - $vid_pix = get_post_meta($v_Id, \'vid_pix\', false);
我怎样才能做到这一点?

UPDATE

这是我目前拥有的-在我的前端自定义post editor表单上V_EDITOR CPT, 我有以下代码,可以在中创建当前用户帖子的下拉列表PROPERTY CPT, 每个id和标题-

<form id="import_photos" action="import-pics.php" method="post" target="_blank">

<?php query_posts(array(\'post_type\'=>\'property\',\'order\'=>\'desc\',\'author\'=>$current_user->ID,\'post_status\' => array(\'publish\',\'pending\',\'draft\')));
if(have_posts()):

echo \'<select id="propertyID" name="propertyID">\';
while(have_posts()):the_post();
$id = get_the_ID();
$v_title1 = get_the_title($id);

echo \'<option value="\'.$id.\'">\'.$v_title1.\'</option>\';
endwhile;
echo \'</select>\';
endif; ?>
<input type="hidden" name="videoID" id="videoID" value="<?php echo $v_Id; ?>">
<input type="submit" id="ip_submit" class="savestuff2" name="ip_submit" value="Import Photos">
</form>
<?php wp_reset_query(); ?>
当用户选择一个选项并单击提交按钮时,将打开一个新窗口,当前显示该帖子中的照片。这是导入图片。php脚本--

<?php
require_once $_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-load.php\';

$propertyID = $_POST[\'propertyID\'];
$vidID = $_POST[\'videoID\'];

$query = new WP_Query( 
  array( 
    \'post_type\' => \'property\', 
    \'posts_per_page\' => -1,
    \'fields\' => \'ids\',
    \'p\' => $propertyID
  ) 
);
$image_query = new WP_Query( 
  array( 
    \'post_type\' => \'attachment\', 
    \'post_status\' => \'inherit\', 
    \'post_mime_type\' => \'image\', 
    \'posts_per_page\' => -1, 
    \'post_parent__in\' => $query->posts, 
    \'order\' => \'DESC\' 
  ) 
);

if( $image_query->have_posts() ){
  while( $image_query->have_posts() ) {
      $image_query->the_post();
      $filename = basename ( get_attached_file( get_the_ID() ) );
      $imgurl = wp_get_attachment_url( get_the_ID() );

      echo \'<div style="display:inline-block;margin:10px;">\';
      echo \'<a href="\'.$imgurl.\'"><img style="width:200px;" src="\'.$imgurl.\'"></a>\';
      echo \'</div>\';
  }
}

?>
我需要做的是在PROPERTY post类型自定义字段$property_sights_value 进入V_EDITOR post类型自定义字段$vid_pix, 它们都是图像附件。如何使用脚本执行此操作?

UPDATE

(尝试使用ajax实现)

我的函数中的代码。php文件--

add_action( \'wp_enqueue_scripts\', \'ajax_imp_enqueue_scripts\' );
function ajax_imp_enqueue_scripts() {

wp_enqueue_script( \'imp\', get_stylesheet_directory_uri() . \'/imp.js\', array(), \'1.0.0\', true );

wp_localize_script( \'imp\', \'imppro\', array(
        \'ajax_url\' => admin_url( \'admin-ajax.php\' )
    ));

}

add_action(\'wp_ajax_imp_pros\', \'import_the_photos\');
add_action(\'wp_ajax_nopriv_imp_pros\', \'import_the_photos\');

function import_the_photos() {

Global $wp_query;
$current_page_id = $wp_query->get_queried_object_id();
global $post;
global $current_user;
$current_user = wp_get_current_user();

$propertyID = $_POST[\'propertyID\'];
$videoID = $_POST[\'videoID\'];

// Get the attachments (IDs) attached to the `property` CPT.
$prop_att_ids = (array) get_post_meta( $propertyID, \'imic_property_sights\', false );

// Copy the meta value (attachments) to the `v_editor` CPT.
$vid_pix = get_post_meta( $videoID, \'vid_pix\', false ); // test
//delete_post_meta( $videoID, \'vid_pix\' );
foreach ( $prop_att_ids as $att_id ) {
    add_post_meta( $videoID, \'vid_pix\', $att_id );
}
$vid_pix2 = get_post_meta( $videoID, \'vid_pix\', false ); // test
var_dump( $vid_pix, $vid_pix2 ); // test

// Show a notice to the user.
echo \'<p>Photos below have been imported \' .
    \'from <i>\' . get_the_title( $propertyID ) . \'</i> \' .
    \'to <i>\' .  $videoID . \'</i>:</p>\';

// And show the attachments.
foreach ( $prop_att_ids as $att_id ) {
    $imgurl = $att_id ? wp_get_attachment_url( $att_id ) : \'\';
    if ( $imgurl ) {
        echo \'<div style="display:inline-block;margin:10px;">\';
        echo \'<a href="\'.$imgurl.\'"><img style="width:200px;" src="\'.$imgurl.\'"></a>\';
        echo \'</div>\';
    }
}

die();

}
和我的JS代码--

jQuery(document).on(\'click\',\'#ip_submit\',function (e) {
  e.preventDefault();

 var propertyID = $(\'#propertyID\').val();
 var videoID = $(\'#videoID\').val();

                    $.ajax({
                      url: imppro.ajax_url,
                      data: {
                       action: \'imp_pros\',
                       propertyID: propertyID,
                       videoID: videoID
                         },
                     //processData: false,
                     //contentType: false,
                      type: \'POST\',
                      success: function(data){
                        console.log(data);
                        $("#save-order").trigger("click");
                        //$(\'#loader\').hide();
                        //$(\'#finished\').show().delay(3000).fadeOut();
                        }
                    });

        });

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

在中更新#2import_the_photos() 函数,请更改此项:

// Copy the meta value (attachments) to the `v_editor` CPT.
$vid_pix = get_post_meta( $videoID, \'vid_pix\', false ); // test
//delete_post_meta( $videoID, \'vid_pix\' );
foreach ( $prop_att_ids as $att_id ) {
    add_post_meta( $videoID, \'vid_pix\', $att_id );
}
$vid_pix2 = get_post_meta( $videoID, \'vid_pix\', false ); // test
var_dump( $vid_pix, $vid_pix2 ); // test
收件人:

// Get the attachments (IDs) attached to the `v_editor` CPT.
$vid_att_ids = (array) get_post_meta( $vidID, \'vid_pix\', false );

// Copy/add the attachments to the `v_editor` CPT.
foreach ( $prop_att_ids as $att_id ) {
    if ( $att_id && ! in_array( $att_id, $vid_att_ids ) ) {
        add_post_meta( $vidID, \'vid_pix\', $att_id );
    }
}

var_dump( $prop_att_ids, $vid_att_ids, get_post_meta( $vidID, \'vid_pix\', false ) ); // test
为此道歉delete_post_meta(), 谢天谢地,你把它评论掉了

原始答案(但删除了旧代码)

在您的import-pics.php 文件,将此添加到$query = new WP_Query 生产线:

// Get the attachments (IDs) attached to the `property` CPT.
$prop_att_ids = (array) get_post_meta( $propertyID, \'imic_property_sights\', false );

// Copy the meta value (attachments) to the `v_editor` CPT.
// OLD CODE HERE; REMOVED
但这是我使用的完整代码,我没有使用new WP_Query() 构造:

<?php
// File: import-pics.php

require_once $_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-load.php\';

$propertyID = $_POST[\'propertyID\'];
$vidID = $_POST[\'videoID\'];

// Get the attachments (IDs) attached to the `property` CPT.
$prop_att_ids = (array) get_post_meta( $propertyID, \'imic_property_sights\', false );

// Copy the meta value (attachments) to the `v_editor` CPT.
// OLD CODE HERE; REMOVED

// Show a notice to the user.
echo \'<p>Photos below have been imported \' .
    \'from <i>\' . get_the_title( $propertyID ) . \'</i> \' .
    \'to <i>\' . get_the_title( $vidID ) . \'</i>:</p>\';

// And show the attachments.
foreach ( $prop_att_ids as $att_id ) {
    $imgurl = $att_id ? wp_get_attachment_url( $att_id ) : \'\';
    if ( $imgurl ) {
        echo \'<div style="display:inline-block;margin:10px;">\';
        echo \'<a href="\'.$imgurl.\'"><img style="width:200px;" src="\'.$imgurl.\'"></a>\';
        echo \'</div>\';
    }
}

// Show a "close" JS button..
echo \'<p><a href="#" onclick="window.close(); return false;">Back to form</a></p>\';
?>
更新是因为我忘了元imic_property_sightsvid_pix 多次添加到帖子中。对此表示抱歉

结束

相关推荐

Remove P tags from images

我使用的是WordPress 4.2.2,每次我向wysiwyg添加图像时,它都会将输出的图像包装在段落标记中。我需要去掉这些标签。我在网上找到的所有东西都是从2011年开始的,而且似乎都不起作用。我试着把东西放在函数中。php类:function filter_ptags_on_images($content){ return preg_replace(\'/<p>\\s*(<a .*>)?\\s*(<img .* \\/>)\\s*(<\\/a&g