WordPress installation path / template theme / picture function call Daquan

Recently, during some theme changes, some images, CSS, JS and other static files are frequently called.

  • Of course, for these static files, we can call them directly using absolute paths.
  • But consider the followingBuilding websiteTesting, and a series of code problems that the theme may have, such as code that doesn't work due to random modifications.
  • Chen Weiliangstill like to useWordpressPath function, and relative path for resource loading.

Because the human brain is difficult to remember for complex WordPress function calling codes, we often forget which WordPress function codes to use when we need to use them?

So, decided to list the WordPress path function calls here and update it occasionally for reference.

WordPress logo

WordPress homepage path

<?php home_url( $path, $scheme ); ?>

PHP function call ▼

<?php echo home_url(); ?>
  • Display: http:// your domain name

WordPress installation path

<?php site_url( $path, $scheme ); ?>

PHP function call ▼

<?php echo site_url(); ?>
  • Display: http://yourdomain/wordpress

WordPress backendmanagement path

<?php admin_url( $path, $scheme ); ?>

PHP function call ▼

<?php echo admin_url(); ?>
  • Display: http://yourdomain/wordpress/wp-admin/

wp-includes path

<?php includes_url( $path ); ?>

PHP function call ▼

<?php echo includes_url(); ?>
  • Display: http://yourdomain/wordpress/wp-includes/

wp-content path

<?php content_url( $path ); ?>

PHP function call ▼

<?php echo content_url(); ?>
  • Display: http://yourdomain/wordpress/wp-content

WordPress upload path

<?php wp_upload_dir( string $time = null, bool $create_dir = true,bool $refresh_cache = false ) ?>

PHP function call ▼

<?php $upload_dir = wp_upload_dir(); echo $upload_dir['baseurl']; ?>
  • Display: http://yourdomain/wordpress/wp-content/uploads

PHP function call ▼

<?php $upload_dir = wp_upload_dir(); echo $upload_dir['url']; ?>
  • Display: http://yourdomain/wordpress/wp-content/uploads/2018/01

PHP function call server path ▼

<?php $upload_dir = wp_upload_dir(); echo $upload_dir['basedir']; ?>
  • Display: D:\WorkingSoftWare\phpStudy\WWW\wordpress/wp-content/uploads

PHP function call server path ▼

<?php $upload_dir = wp_upload_dir(); echo $upload_dir['path']; ?>
  • Display: D:\WorkingSoftWare\phpStudy\WWW\wordpress/wp-content/uploads/2018/01

WordPress pluginpath

<?php plugins_url( $path, $plugin ); ?>

PHP function call ▼

<?php echo plugins_url(); ?>
  • Display: http://yourdomain/wordpress/wp-content/plugins

PHP function call ▼

<?php plugin_dir_url($file) ?>
  • Commonly used:      //$file (required) returns the absolute path of the current plugin
  • Display: http://yourdomain/wordpress/wp-content/plugins/yourplugin/

PHP function call ▼

<?php plugin_dir_path($file); ?>
  • Commonly used:      //$file (required) returns the absolute path of the current plugin server.
  • Putting it under the theme file will also return the absolute path of the theme server, but it is not recommended to use it, it is easy to mess up.
  • Display: D:\WorkingSoftWare\phpStudy\WWW\wordpress\wp-content\plugins\yourplugin/

WordPress theme path

<?php get_theme_roots(); ?>

Commonly used:

show: /themes

<?php get_theme_root( '$stylesheet_or_template' ); ?>

Commonly used:

Display: D:\WorkingSoftWare\phpStudy\WWW\wordpress/wp-content/themes

<?php get_theme_root_uri(); ?>

Commonly used:

Show: http://yourdomain.com/wordpress/wp-content/themes

<?php get_theme_file_uri( '$file' ) ?>

Commonly used:

Display: http://yourdomain.com/wordpress/wp-content/themes/cwlcms

<?php get_theme_file_path( '$file' ) ?>

Commonly used:

Display: D:\WorkingSoftWare\phpStudy\WWW\wordpress/wp-content/themes/cwlcms

<?php get_template(); ?>

Commonly used: //return theme name

Display: cwlcms

<?php get_template_directory(); ?>

Commonly used:

Display: D:\WorkingSoftWare\phpStudy\WWW\wordpress/wp-content/themes/cwlcms

<?php get_template_directory_uri(); ?>

Commonly used:

Display: http://yourdomain.com/wordpress/wp-content/themes/cwlcms

Note: get_template queries the style.css file of the theme. If there is no such file in the theme directory, an error will occur.

<?php get_stylesheet(); ?>

Commonly used: //If using a sub-theme, return the directory name of the sub-theme

Display: cwlcms

<?php get_stylesheet_uri(); ?>

Commonly used:

Display: http://yourdomain.com/wordpress/wp-content/themes/cwlcms/style.css

<?php get_stylesheet_directory() ?>

Commonly used:

  • //If using a sub-theme, return the sub-theme server path

Display: D:\WorkingSoftWare\phpStudy\WWW\wordpress/wp-content/themes/cwlcms

  • //But it is more used in include other files
<?php get_stylesheet_directory_uri(); ?>

Commonly used:

Display: http://yourdomain.com/wordpress/wp-content/themes/cwlcms

Note: get_stylesheet queries the style.css file of the theme. If there is no such file in the theme directory, an error will occur.

Get multiple pieces of information from a blog

Finally, share more powerful functions that basically get all the above paths and other information.

<?php get_bloginfo( '$show', '$filter' ) ?>
  • PHP function call: //get_bloginfo can get a variety of information about the blog,Get blog address when $show is set to url
  • Display: http:// your domain name

Other information that can be obtained by get_bloginfo:

  • name
  • Description
  • wpurl
  • siteurl/url
  • admin_email
  • charset
  • version
  • html_type
  • text_direction
  • language
  • stylesheet_url
  • stylesheet_directory
  • template_url
  • template_directory
  • pingback_url
  • atom_url
  • rdf_url
  • rss_url
  • rss2_url
  • comments_atom_url
  • comments_rss2_url

Comment

Your email address will not be published. Required fields * Callout

Scroll to Top