wp_upload_dir()
via ['basedir']
(you already are listing in your CONSTANTS list) to generate your path constants and the ['baseurl']
for your url constants?
]]>I want to change the upload folder dynamically but I just can’t find the solution.
How can i change dynamic the folder /CUSTOM
Code example in function
function changeUploadFolder( $upload ) {
$upload['subdir'] = '/CUSTOM' . $upload['subdir'];
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];
return $upload;
}
add_filter('upload_dir', 'changeUploadFolder');
...
//here the file is uploaded with the new dynamic path
...
remove_filter('upload_dir', 'changeUploadFolder');
Thx for help
Alex
<?php
if(isset($_POST['message'])){
$post = array(
'post_title' => strip_tags($_POST['title']),
'post_content' => strip_tags($_POST['message']),
'post_status' => 'publish',
'post_type' => 'testimonial'
);
$post_id = wp_insert_post( $post );
if($_FILES["image"]["type"] =="image/gif" or $_FILES["image"]["type"] =="image/png" or $_FILES["image"]["type"] =="image/jpeg" ) {
if(!empty($_FILES["image"])) {
$uploads = wp_upload_dir();
$fileName = $_FILES["image"]["name"];
$fileTmpLoc = $_FILES["image"]["tmp_name"];
$uzanti = pathinfo($fileName, PATHINFO_EXTENSION);
$remove_these = array(' ','','"','\'','\\','/');
$fileName = str_replace($remove_these,'', $_FILES['image']['name']);
$newname = time().'-hepyorum.'.$uzanti;
$pathAndName = $uploads['path'].'/'.$newname;
$moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
if ($moveResult == true) {
// echo "File has been moved from " . $fileTmpLoc . " to " . $pathAndName;
$resimadresi = 'https://www.hepyorum.com/'.substr($pathAndName, 27);
// add_post_meta($post_id, 'resim' , $resimadresi, true );
$filename = $pathAndName;
$parent_post_id = $post_id;
$filetype = wp_check_filetype( basename( $filename ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
$resimadresi = wp_get_attachment_url( $attach_id );
set_post_thumbnail( $post_id, $attach_id );
} else {
// echo "ERROR";
}
}
}
}
?>
<form action="<?php the_permalink(); ?>" method="post" enctype="multipart/form-data">
<textarea class="i2" name="message" placeholder="comment"></textarea>
<input type="text" name="title" class="i1" placeholder="Title">
<label class="i4">
Foto: <input type="file" name="image" placeholder="Foto">
</label>
<input type="submit" value="Enviar" class="i3">
</form>
]]>https://www.ads-software.com/plugins/custom-upload-dir/
]]>So I am having a big of a problem, currently one of our php files that we bought points to the default uploads directory for wordpress and we would like to change this to our own uploads directory.
We are still using the default uploads directory for wordpress but we want to change the directory where this custom php file uploads to.
Currently the code looks like this:
$upload_dir = wp_upload_dir();
$upload_dir['basedir'].'/downloads/encrypted/'.$item.'/*'));
It currently looks like this “www.example.com/wp-content/uploads/downloads/encrypted/”
We want it to point to “www.example.com/downloads/encrypted/”
Have looked into the arrays for wp_upload_dir();
but cannot seem to go a folder back. Any tips or help?
Thanks
]]>function kdr_awesome_fonts_font_family_choice(){
$font_family_input = get_option( 'font_family_choice', $default = false );
?>
<select name="font_family_choice">
<?php
$fonts = kdr_awesome_fonts_fontfamily_jsoncall();
$i=0;
//if the json request works render a select box for the list of the family names
foreach($fonts as $key => $value){
echo "<option value='".$i."' ". selected($i, $font_family_input).">".$fonts[$key]->family_name."</option>";
$i++;
}
?>
</select>
<?php
$font_position = kdr_postion($font_family_input);
$font_position_placement = wp_remote_request('https://www.fontsquirrel.com/fontfacekit/'.$font_position -> family_urlname.'');
<p>}
<?php
$upload_dir = wp_upload_dir('web-fonts/'.$font_position_placement);
}
]]>Is there anyway I can pass in a site ID and get the upload dir for *that* site using another function or combination? (in my case I need the main site)
I can do get_site_url(1).'/uploads/sites/1'
, but wondering if there’s a safer way.
I have seen many articles and questions on StackExchange, stackoverflow, here on the forum, but nothing worked for me.
My current code creates a folder based on custom post type approach, however uploading the pictures and the url of them is on uploads folder.
function post_type_dir($param){
$screen = get_current_screen();
$mydir = (string)$screen->post_type;
echo $mydir. '<br />';
$param['path'] = $param['path'] .'/'. $mydir;
$param['url'] = $param['url'] .'/'. $mydir;
$param['subdir'] = $mydir;
error_log("path={$param['path']}");
error_log("url={$param['url']}");
error_log("subdir={$param['subdir']}");
error_log("basedir={$param['basedir']}");
error_log("baseurl={$param['baseurl']}");
error_log("error={$param['error']}");
return $param;
}
I’m going crazy with this, please, someone has been there and managed to solve this problem?
]]>upload_dir
filter hook), SIW assumes they will be in the default location then fails when they aren’t.
It also conflicts with wp e-Commerce, regenerating the watermarked image on every page load, continually degrading the image as it’s a copy of a copy of a copy, ad infinitum. This is not SIW’s fault, but it’s a very common plugin so I made SIW work around e-Commerce’s flaw.
function siw_add_watermark($meta){
$referer = strpos( wp_get_referer(), 'siw_options_page' );
if ( $referer != '' ) {
return $meta;
}
//var_dump($meta);
$options = get_option('siw_plugin_options');
if(!isset($options['sizes']) || !isset($options['image']) || $options['image'] == '') return $meta;
foreach($options['sizes'] as $size){ // Cycle trough checked image sizes to crop
if(isset($meta['sizes'][$size]) || $size == 'full size'){ // Check if given size exist
$pos = strpos($options['image'], 'wp-content/');
$watermark_path = ABSPATH.substr($options['image'], $pos);
$test = getimagesize($watermark_path);
//var_dump($test);
if($test['mime'] == 'image/png'){
$stamp = imagecreatefrompng($watermark_path);
}elseif($test['mime'] == 'image/jpeg'){
$stamp = imagecreatefromjpeg($watermark_path);
}
// Url of processed file
$file = wp_upload_dir();
$file = trailingslashit($file['basedir']);
if ($size == 'full size')
$file .= $meta['file'];
else
$file .= substr($meta['file'], 0, strrpos($meta['file'], '/') + 1) . $meta['sizes'][$size]['file'];
$watermarked = $file . '.siw';//use filesystem to store watermarked status because wp e-Commerce doesn't completely save metadata
if (file_exists($watermarked) && filemtime($watermarked) >= filemtime($file))
continue;
$file_params = getimagesize($file);
if($file_params['mime'] == 'image/png'){
$im = imagecreatefrompng($file);
}elseif($file_params['mime'] == 'image/jpeg'){
$im = imagecreatefromjpeg($file);
}
else
continue;
// Set the margins for the stamp and get the height/width of the stamp image
$marginlr = (int)$options['margin_leftright'];
$margintd = (int)$options['margin_topdown'];
$stampw = imagesx($stamp);
$stamph = imagesy($stamp);
$mainw = imagesx($im);
$mainh = imagesy($im);
switch($options['watermark_position']){
case '1':
$posx = $marginlr;
$posy = $margintd;
break;
case '2':
$posx = ($mainw - $stampw) / 2;
$posy = $margintd;
break;
case '3':
$posx = $mainw - $stampw - $marginlr;
$posy = $margintd;
break;
case '4':
$posx = $marginlr;
$posy = ($mainh - $stamph) / 2;
break;
case '5':
$posx = ($mainw - $stampw) / 2;
$posy = ($mainh - $stamph) / 2;
break;
case '6':
$posx = $mainw - $stampw - $marginlr;
$posy = ($mainh - $stamph) / 2;
break;
case '7':
$posx = $marginlr;
$posy = $mainh - $stamph - $margintd;
break;
case '8':
$posx = ($mainw - $stampw) / 2;
$posy = $mainh - $stamph - $margintd;
break;
case '9':
$posx = $mainw - $stampw - $marginlr;
$posy = $mainh - $stamph - $margintd;
break;
default:
}
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, $posx, $posy, 0, 0, imagesx($stamp), imagesy($stamp));
imagejpeg($im, $file, 90);
imagedestroy($im);
touch($watermarked);
}
}
return $meta;
}
https://www.ads-software.com/extend/plugins/simple-image-watermark/
]]>However if I’m on a subsite (not the main site) and call switch_to_blog() to some other subsite the basedir does properly update.
In WP 3.4.2 the basedir item would change to the proper blog media upload path.
]]>