Are you trying to get the path, i.e., the location of the file in the server (/home/user/public_html/wp-content/plugins…) or the url, i.e., the “web” path (https://example.com/wp-content/plugins…)?
For paths you should use plugin_dir_path, but that function doesn’t accept a dir as parameter, it accepts a file. So, depending on where are you calling it, you can try plugin_dir_path ( __FILE__ ) . 'custom/emails/attached-files/Widerrufsrecht.pdf'
;
If you want a URL, then you can use plugins_url, like plugins_url( 'custom/emails/attached-files/Widerrufsrecht.pdf', __FILE__ )
.
If you find yourself into a directory mess, where you are too many levels deep but want to call one of those relatively to the plugins root, a good ideia is to define a constant in the main plugin file, like define( 'MY_PLUGIN_FILE', __FILE__ );
and then call plugin_dir_path ( MY_PLUGIN_FILE ) . 'custom/emails/attached-files/Widerrufsrecht.pdf'
from whatever you want.
thanks for your reply. I gave it a try, but none of them was working. Because I assume the plugin folder needs to be included in the path as well I put it in front of the path, trying with slash and without slash in front.
Maybe I should explain my function a little bit more so that you are able to see what I want to achieve. I want to add an attachment to an email:
function attach_agb_pdf_to_email ( $attachments_agb , $id, $object ) {
$agb_pdf_path = plugin_dir_path(__FILE__) . '/theme-customisation-master/custom/emails/attached-files/AGB.pdf';
$attachments_agb[] = $agb_pdf_path;
return $attachments_agb;
}
When storing the emails folder within my theme folder everything is working. Moving it to wp-content/plugins/theme-customisation-master/custom is the desired goal.
Thanks again for your help,
-Bj?rn
]]>Are you using this plugin? If so, are you adding your code into custom/functions.php
?
The plugin_dir_path
function is just a wrapper for trailingslashit( dirname( $file ) )
, so if you pass __FILE__
as paramater the function will return the directory of the current file with a trailing slash. That said, if you are calling it from theme-customisation-master/custom/functions.php
, this piece of code plugin_dir_path(__FILE__)
will probably return something like /home/your-folders/wordpress/wp-content/plugins/theme-customisation-master/custom/
and you just need to complete the rest. So, you can try this:
$agb_pdf_path = plugin_dir_path(__FILE__) . 'emails/attached-files/AGB.pdf';
If it doesn’t work, the best way to move foward is debugging. An echo
, or a die()
or some better way like xdebug will tell you what is the path returned by the function, right?
If you have any doubts just come back here!
]]>Thank you again, that will help me a lot with my further plans. ??
Have a great day! Best regards,
-Bj?rn
]]>