3

I am trying to use SMOF Options Framework in a plugin and i have this problem. SMOOF use this constants to get theme directory url:

if( !defined('ADMIN_PATH') )
    define( 'ADMIN_PATH', get_template_directory() . '/admin/' );
if( !defined('ADMIN_DIR') )
    define( 'ADMIN_DIR', get_template_directory_uri() . '/admin/' );

But i want to use SMOF in a plugin, not in theme, how can i get the plugin directory url? What to put in order to get_template_directory() and get_template_directory_uri() to use it in a plugin?

2 Answers 2

11

Use plugin_dir_url( __FILE__ ); for the URL and plugin_dir_path( __FILE__ ); for the path.

Pass the plugin’s main file to both functions to get similar results.

Besides that, ADMIN_PATH and ADMIN_DIR are really poor names for custom code. They might result in collisions with other code in the future. Try to use better names, something with a unique prefix.

1
  • working like a charm to make local resource calls, thanks!
    – d1jhoni1b
    Commented May 15, 2018 at 19:05
3

I have created a wordpress plugin that uses custom css/js code, in order to include these resources locally add a line like this one:

wp_enqueue_style( 'wp-aa-style', plugin_dir_url( __FILE__ ) . 'css/wp-aa-style.css',false,'1.1','all');

wp-aa-style is the element id make sure to define a css folder and a wp-aa-style.css file inside your WordPress plugin's main folder

Adding this answer hoping it will help someone willing to use plugin_dir_url( __FILE__ ) to make local resource calls (instead of static public URLs) for custom WordPress plugins.

1
  • I guess that your "demo" plugin is gone from GitHub... oh well. Thanks for the tip, anyway. Commented May 27, 2023 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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