像(/upload/%alphanumeric_id%/%file_type%/%blog_ID%)这样的多站点调整上传

时间:2017-10-22 作者:Game Unity

Background: 我正在尝试使用WordPress多站点功能作为多语言站点,因为每个博客都是不同的语言。我有代码可以自动为我在其他博客上的内容生成翻译。文章通过字母数字ID进行关联,以识别数据库中的翻译。

This code generates the alphanumeric ID:

function bZive_generate_AlphanumericID( $post_id ) {

    $postTypes = array(\'profile\', \'article\', \'attachment\');
    $postType = get_post_type( $post_id );

    if (in_array( $postType, $postTypes ) and empty( get_post_meta( $post_id, \'alphanumeric_id\', true ) ) ) {


        $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ\'$§%&!";

        $charactersLength = strlen($characters);
        $randomString = \'\';
        for ($i = 0; $i < 13; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }


         /**
         * Now check here if the string is in the database
         */
        $args = array(
        \'post_type\'     =>  array(
                $postTypes 
            ),
        \'meta_query\'    =>  array(
            array(
                \'meta_key\'  =>  \'alphanumeric_id\'
            )
        )
        );
        $posts = new WP_Query( $args );


        $meta_values = \'\';
        if( $posts->have_posts() ) {
          while( $posts->have_posts() ) {
            $posts->the_post();

            $meta_values[] = get_post_meta( get_the_ID(), \'alphanumeric_id\', true );
          }
        } 
        wp_reset_postdata();


        if (in_array( $randomString, $meta_values )) {
            // "Match found"
            return generate_AlphanumericID;

        }  else {
            // "Match not found"
            add_post_meta($post_id, \'alphanumeric_id\', $randomString);
            return $randomString;

        }

    }

}
add_action(\'wp_insert_post\', \'bZive_generate_AlphanumericID\', 10, 3);
add_action(\'add_attachment\', \'bZive_generate_AlphanumericID\', 10, 1);
现在,我正在尝试对上载执行相同的操作-您可能会看到,无论是否添加附件,我都会生成此ID。但是,如果上载的文件上载到具有此ID的文件夹中,那就太好了。要有这样的上载结构:

概述/public_html/upload/%alphanumeric_id%/%file_type%/%blog_ID% -filename.ext/public_html/upload/gpB12n!nkV358/image/6/Gibraltar-map.jpg

About the blog_ID:

/**
* by default each file has this meta value
*/
add_post_meta($post_id, \'global_supported_language\', \'true\');
/**
* And the blog_id were the file was uploaded to
*/
add_post_meta($post_id, \'default_uploaded_language\', \'6\');

About the file_type:

/**
* Just the file type like...
*/
if(in_array( $upload_ext , array(\'jpg\', \'png\', \'gif\', \'svg\'); )){
     $file_type = \'image\';
}
注:found this on StackExchange 但还没弄明白。

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

好的,这是我现在的解决方案-但是如何将生成的字母数字\\u id添加为add_post_meta($post_id, \'alphanumeric_id\', $randomString); 到相关附件?

/*===================================================================================*/
/*  function change the upload for multi-lang
/*==================================================================================*/


function bZive_handle_upload_prefilter( $file ) {

    add_filter(\'upload_dir\', \'bZive_custom_upload_dir\');
    return $file;
}


function bZive_handle_upload( $fileinfo ){

    remove_filter(\'upload_dir\', \'bZive_custom_upload_dir\');
    return $fileinfo;
}



/**
 * Force all network uploads to reside in "/upload", and by-pass
 * "files" URL rewrite for site-specific directories.
 * 
 * @link    http://wordpress.stackexchange.com/q/147750/1685
 * 
 * @param   array   $dirs
 * @return  array 
 */

function bZive_upload_dir( $dirs ){

    $dirs[\'baseurl\'] = network_site_url( \'/upload\' );
    $dirs[\'basedir\'] = ABSPATH . \'/upload\';
    $dirs[\'path\'] = $dirs[\'basedir\'] . $dirs[\'subdir\'];
    $dirs[\'url\'] = $dirs[\'baseurl\'] . $dirs[\'subdir\'];

    return $dirs;
}

add_filter( \'upload_dir\', \'bZive_upload_dir\' );



function bZive_custom_upload_dir($path){  

    /*
     * Save uploads in alphanumeric_id based folders 
     *
     */

    $postTypes = array(\'profile\', \'article\', \'attachment\');
    $postType = get_post_type( $post_id );


    $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ\'$§%&!";

    $charactersLength = strlen($characters);
    $randomString = \'\';
    for ($i = 0; $i < 15; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }


    /*
     * Now check here if the string is in the database
     */
    $args = array(
    \'post_type\'     =>  array(
            $postTypes 
        ),
    \'meta_query\'    =>  array(
        array(
            \'meta_key\'  =>  \'alphanumeric_id\'
        )
    )
    );
    $posts = new WP_Query( $args );


    $meta_values = \'\';
    if( $posts->have_posts() ) {
      while( $posts->have_posts() ) {
        $posts->the_post();

        $meta_values[] = get_post_meta( get_the_ID(), \'alphanumeric_id\', true );
      }
    } 
    wp_reset_postdata();


    if (in_array( $randomString, $meta_values )) {
        // "Match found"
        return bZive_custom_upload_dir;

    }  else {
        // "Match not found"
        $customdir = \'/\' . $randomString;



    }

    /*
     * Save uploads in blog_id based folders 
     *
     */     
    $customdir .= \'/\' . get_current_blog_id();





    $path[\'path\']    = str_replace($path[\'subdir\'], \'\', $path[\'path\']); //remove default subdir (year/month)
    $path[\'url\']     = str_replace($path[\'subdir\'], \'\', $path[\'url\']);      
    $path[\'subdir\']  = $customdir;
    $path[\'path\']   .= $customdir; 
    $path[\'url\']    .= $customdir;  




    return $path;
}

add_filter(\'wp_handle_upload_prefilter\', \'bZive_handle_upload_prefilter\');
add_filter(\'wp_handle_upload\', \'bZive_handle_upload\');
好的,我有这样的身份证
function bZive_generate_AlphanumericID( $post_id ) {

    $postTypes  = array(\'profile\', \'article\');
    $postType       = get_post_type( $post_id );
    $randomString   = \'\';



    if ( $postType == \'attachment\' and empty( get_post_meta( $post_id, \'alphanumeric_id\', true ) ) ) {

    $urlstring = wp_get_attachment_url( $post_id ); 
    $segments = explode(\'/\', trim(parse_url($urlstring, PHP_URL_PATH), \'/\'));
    $numSegments = count($segments); 
    $randomString = $segments[1];


    add_post_meta($post_id, \'alphanumeric_id\', $randomString);
    return $randomString;


    }


}
add_action(\'wp_insert_post\', \'bZive_generate_AlphanumericID\', 10, 3);
add_action(\'add_attachment\', \'bZive_generate_AlphanumericID\', 10, 1);

结束

相关推荐

WordPress Multisite with VVV

在命令行中,我使用vv create 要创建新的WordPress多站点,请在设置期间在多站点选项上选择“y”。该过程完成后,即创建了站点,但尚未设置网络。我进入wp配置文件,并添加/* Multisite */ define( \'WP_ALLOW_MULTISITE\', true ); 获取管理区域中的网络选项。现在我被困了,因为在设置网络时,它会显示“警告:检测到现有WordPress网络。请完成配置步骤。要创建新网络,您需要清空或删除网络数据库表”在代码块上方启用网络。此外,虽然我能够将