• Resolved gluecklichezwerge

    (@gluecklichezwerge)


    Hello,

    I am trying to save my previous changes from being overwritten by updates. Therefore I am using a plugin in which I can store my php, js and css snippets.
    Now I have a real basic problem- calling objects within this plugin. For example:
    Before: Requested file is located within a folder in my theme ‘woocommerce/emails/attached-files/Widerrufsrecht.pdf
    And in my functions.php:
    $widerruf_pdf_path = get_template_directory() . '/woocommerce/emails/attached-files/Widerrufsrecht.pdf';

    Now I moved the emails folder to my plugin directory and tried nearly everything to attach the file from there using plugin_dir_path (__DIR__), plugins_url and several other methods, none of them working. For sure I did change the given path before to ‘/MyPlugin/custom/emails/attached-files/Widerrufsrecht.pdf’
    Now I am hoping someone could help me with this, as studying the reference codex obviously didn’t help as well. ??

    Thank you very much, regards

    -Bj?rn

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @gluecklichezwerge,

    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.

    Thread Starter gluecklichezwerge

    (@gluecklichezwerge)

    Hi Felipe,

    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

    Hi @gluecklichezwerge,

    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!

    Thread Starter gluecklichezwerge

    (@gluecklichezwerge)

    Yeah, Felipe, you’re great!
    That did it. And now I know where my fault was…I expected the plugin to add my code from /custom/functions.php to the functions.php of my theme. Therefore I always added the full path from plugins.

    Thank you again, that will help me a lot with my further plans. ??
    Have a great day! Best regards,

    -Bj?rn

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Child theme question’ is closed to new replies.