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.
attachment
posts, or the files inwp-content/uploads
themselves? There's a lot of ambiguity here