Fetch image from specific directory in drupal - drupal

I'm beginner in drupal. I would like to fetch images from specific folder in drupal. Is there any way to fetch drupal images directly from any specific in custom theme.

You should use only images from your theme. And you can get path to your theme like this:
$theme = drupal_get_path('theme',$GLOBALS['theme']);
Put that i.e. at top of template file that uses theme images. And then inside your theme templates you can have something like:
<img src="/<?php echo $theme; ?>/images/my_image.png">
If you stored you theme images inside images dir...

If an array of image paths on your local filesystem is what you want - use this code, it should do what you need. gets all the png jpg gif paths within the sites/default/files/vegas directory.
//We will use the stream wrapper to access your files directory
if ($wrapper = file_stream_wrapper_get_instance_by_uri('public://')) {
//Now we can easily get an actual path to that folder
$directory = $wrapper->realpath();
//Don't forget to append your "vegas" subfolder here
$directory .= DIRECTORY_SEPARATOR . 'vegas' . DIRECTORY_SEPARATOR;
$images = glob($directory . "*.{jpg,png,gif}", GLOB_BRACE);
foreach($images as $imagePath)
{
//Do your thing!
echo $imagePath;
}
}

Related

Modify uploads directory for gutenberg block

I actually create an extranet space on my website, with pages where you must to be logged-in to see the content.
My problem is about media uploads.
I want to make them private for a specific custom post type, and upload them in a sub directory (uploads/private).
I use the upload_dir filter (add_filter('upload_dir', 'extranet_upload_directory');) with success for thumbnail for exemple, but when I upload an image in the content with a gutenberg block, this one is upload in the initial foldier (uploads), not in uploads/private.
Does anyone have an idea where to look at ?
My code below :
add_filter('upload_dir', 'extranet_upload_directory');
function extranet_upload_directory( $param ){
$id = $_REQUEST['post_id'];
if ( ( get_post_type( $id ) == 'test-extranet' ) ) {
$param['subdir'] = '/private' . $param['subdir'];
$param['path'] = $param['basedir'] . $param['subdir'];
$param['url'] = $param['baseurl'] . $param['subdir'];
}
return $param;
}
This may be a bug... I have a plugin that offers the same functionality as yours and noticed the following:
While creating a post of that new custom post type (e.g. PATHTOsite.fr/wp-admin/post-new.php?post_type=post_type_name):
When I immediately press upload after opening an new image block the uploaded image goes to default directory, uploads.
but, if I first press 'media library' after opening an new image block and then go to the upload button there and upload the image there, the image will be uploaded in the modified uploads directory.

How to change the WordPress Upload directory, if file is uploaded from specific front end page

I am using the Profile Builder Pro Plugin to let users upload files to Wordpress. So the files get uploaded to the standard Wordpress upload folder.
However I need to change the upload directory to a subfolder of the uploads folder, but only if the files are uploaded from a certain front end page.
Is there a way I can do this?
Here is the example I use when uploading my custom avatars. Basically you need to add a filter when wp_handle_upload is executed and remove it afterwards, you will also need to add your conditions and generally complete the code to handle posted files, attach them to user or post etc.
//UPLOAD FOLDER FILTER FOR AVATAR
function avatar_upload_dir( $dirs ) {
$dirs['subdir'] = '/avatars';
$dirs['path'] = $dirs['basedir'] . '/avatars';
$dirs['url'] = $dirs['baseurl'] . '/avatars';
return $dirs;
}
//SOME CUSTOM UPLOAD FUNCTION
function somefunction() {
//some code here
add_filter( 'upload_dir', 'avatar_upload_dir' );
$uploaded_file = wp_handle_upload( $data['file'], array( 'test_form' => false ) );
remove_filter( 'upload_dir', 'avatar_upload_dir' );
//some code here
}
You can create new folder media in wordpress directory, then edit the wp-config.php file and this line before
define('UPLOADS', 'media');
/*Thats all, stop editing!, Happy blogging*/
For more detail check this link

Reset company logo for all job listings in wp job manager

I did import all of my job listings from one site to another, and I have problem with company logo image url, what I'm trying to do is to reset company logo for all job listings, so set it using a filter, or find a way to import the images correctly.
/* This code will set the default company logo, but if there is already existing logo it will not work*/
add_filter( 'job_manager_default_company_logo', 'smyles_custom_job_manager_logo' );
function smyles_custom_job_manager_logo( $logo_url ){
// Change the value below to match the filename of the custom logo you want to use
// Place the file in a /images/ directory in your child theme's root directory.
// The example provided assumes "/images/custom_logo.png" exists in your child theme
$filename = 'logo_clear.png';
$logo_url = get_stylesheet_directory_uri() . '/img/' . $filename;
return $logo_url;
}

wordpress mu sub-site image url

I have a wordpress multisite + domain mapping with one main and two child website. I have setup threewp broadcast to share products to childsites.
Everything works so far but when displaying products on the childsite the image path is the mainsite with a 403 (Forbidden) I have used the below code in my themes function.php to make the main and the child sites share uploads paths. Is that the best way to do it? I'm trying to get all the child site product image paths to be the same as childsite domain. Does anyone know how to fix this?
// Define a single common image directory for your Multisite
add_filter('upload_dir', 'mu_media_upload_dir');
function mu_media_upload_dir($upload) {
global $user_ID;
if ((int)$user_ID > 0) {
$upload['path'] = ABSPATH . 'uploads/images';
switch_to_blog(1);
$upload['url'] = site_url() . '/' . 'uploads/images';
restore_current_blog();
$upload['subdir'] = "";
$upload['basedir'] = $upload['path'];
$upload['baseurl'] = $upload['url'];
}
return $upload;
}
https://wordpress.org/plugins/threewp-broadcast/

drupal image path

i created a page content type, and put this code in it. and set the input style to php.why the image can't show. i test it on my localhost.
<img src="<?php echo $bath_path; ?>/images/1.jpg">
Drupal way:
$path2file = base_path().'/images/1.jpg'; // If image located in {drupal root}/images. If image located in files directory, use file_directory_path() to get this path
print theme('image', $path_to_file, t('Image altername name'), t('Image title'));

Resources