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="https://wordpress.stackexchange.com/" . $randomString;
}
/*
* Save uploads in blog_id based folders
*
*/
$customdir .= "https://wordpress.stackexchange.com/" . 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("https://wordpress.stackexchange.com/", trim(parse_url($urlstring, PHP_URL_PATH), "https://wordpress.stackexchange.com/"));
$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);