0

Background: I'm trying to use the WordPress multisite feature as a multi-lang site were each blog is a different language. I have code which automatically generates a translation for my content at the other blogs. The articles are related by an alphanumeric ID to identify the translation in the db.

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

Now I'm trying to do the same for the uploads - as you might see I'm having this ID generated whether an attachment gets added. But it would be good if the uploaded file is uploaded in a folder with this ID. To have a upload structure like this:

  • General /public_html/upload/%alphanumeric_id%/%file_type%/%blog_ID% -filename.ext
  • Example/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';
}

Note: found this on StackExchange but couldn't figure it out yet.

2
  • I don't understand, are you trying to multiplex so that different versions of an attachment are shown depending on the language? Are you trying to rewrite the URLs of attachment posts, or the files in wp-content/uploads themselves? There's a lot of ambiguity here
    – Tom J Nowell
    Oct 22, 2017 at 18:33
  • No not exactly at first I only try to organize them differently.
    – Game Unity
    Oct 26, 2017 at 13:14

1 Answer 1

1

Ok this is my solution for now - but how do I add the generated alphanumeric_id as add_post_meta($post_id, 'alphanumeric_id', $randomString); to the related attachment?

/*===================================================================================*/
/*  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');

Ok got the id like this

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);
1
  • Ok maybe I have an idea...
    – Game Unity
    Oct 26, 2017 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.