0

I'm building a small plugin into a WordPress theme like wp-content/themes/my-theme/inc/my-plugin.

I'm wondering how to include files inside this folder that are relative to the main plugin file wp-content/themes/my-theme/inc/my-plugin/my-plugin.php.

For example, in my-plugin.php, I need to include wp-content/themes/my-theme/inc/my-plugin/css/style.css.

I'm looking for something like wp_enqueue_style( 'my-plugin-style', plugins_url( '/css/style.css', __FILE__ ) ); but I want it to be only relative to the my-plugin.php directory and NOT relative to the WordPress plugin directory OR theme directory. This way I can make it a separate plugin too if I want, without changing any code.

Please do not ask why I'm building this into the theme or that it should be a separate plugin. Just looking for possible solutions.

Thanks!

1

Plugin within a theme - sounds like something new. You might not find function in this context. Let's create ours:

<?php
/**
 * Get My Plugin URL.
 * @return string.
 */
function wpse295740_get_plugin_url() {
    // http://example.com/wp-content/themes/my-theme/inc/my-plugin
    return get_template_directory_uri() .'/inc/my-plugin';
}

/**
 * Get My Plugin Path.
 * @return string.
 */
function wpse295740_get_plugin_path() {
    // eg. /home/user/var/www/wordpress/wp-content/themes/my-theme/inc/my-plugin
    return get_template_directory() .'/inc/my-plugin';
}

You can make it more complex using the __FILE__ magic constant etc. - see the original plugin_url() source. And plugin_dir_path() too. And trailingslashit() and untrailingslashit() are there too for ease.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.