Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

php - How can I get the current plugin directory in WordPress?

I need to get the current plugin directory like:

[wordpress_install_dir]/wp-content/plugins/plugin_name

(If getcwd() is called from the plugin, it returns [wordpress_install_dir], the root of installation.)

question from:https://stackoverflow.com/questions/3128873/how-can-i-get-the-current-plugin-directory-in-wordpress

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the WordPress core function that's designed specifically for that purpose:

<?php plugin_dir_path( __FILE__ ); ?>

See the Codex documentation here.

You also have

<?php plugin_dir_url( __FILE__ ); ?>

if what you're looking for is a URI as opposed to a server path.

See the Codex documentation here.

IMO, it's always best to use the highest-level method that's available in core, and this is it. It makes your code more future-proof.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...