Friday, June 26, 2015

Wordpress : function to get different site url

Getting the Home Page URL

  1. bloginfo( 'url' ): displays the home URL as specified in our Reading settings in the admin.
  2. get_bloginfo( 'url' ): fetches the site URL without displaying it.
  3. home_url(): fetches the home page URL without displaying it: use echo esc_url( home_url( '/' ) ); to display the home URL with a trailing slash. This takes two optional parameters: $path if we want to add a path to a specific page or add a trailing slash, and $scheme to specify the scheme for the URL, such as http, https and relative.
  4. get_home_url(): retrieves the URL for the home page, and supports Multisite: we can include the blog ID as a parameter.
  5. site_url(): the URL where WordPress is stored, so if our WordPress installation is in the wordpress subdirectory, this will retrieve the URL http://mysite.com/wordpress. 
  6. get_site_url(): retrieves the site URL without outputting it. Can also be used with parameters to output the URL for a site in a Multisite network, add a path, and use a specific scheme.
  7. network_home_url(): the home URL for the main site in a Multisite network, useful if you want to include a link to the main site in the footer of each site on the network, for example.
  8. network_site_url(): the site URL for the main site in a Multisite Network.
Getting Other Front-End URLs

Other than home page URL, we want url of pages, posts, attachments, and custom post types. Below are the function, we should use:
  • post_permalink(): outputs a link to a post, with the post ID as its parameter.
  • get_page_link(): fetches (but does not output) the link to a page, with the page ID as a parameter.
  • get_permalink(): fetches (but does not output) the permalink for a post or page, with the post or page ID as its parameter.
  • get_category_link(): fetches the link to a category archive, with the category ID as its parameter.
  • get_tag_link(): fetches the link to a tag's archive page, with the tag ID as its parameter.
  • get_post_type_archive_link(): fetches the link to a post type's archive, with the post type as its parameter.
  • get_term_link(): fetches the link to a taxonomy term, with the term and the taxonomy as its parameters.
  • the_attachment_link(): outputs the link to an attachment, with the attachment ID as its first parameter. Use other parameters to define the image size and whether the link will lead to the file itself or its attachment page.
  • get_attachment_link(): fetches the link for an attachment, with the attachment ID as the parameter.
  • wp_get_attachment_link(): also fetches the link to an attachment, but additionally lets us display the image if the attachment is an image, at a size we specify.
  • get_search_link(): retrieves the link to the search page. We can define a query as its parameter, or leave it blank, in which case it will use the current query.

Getting Admin URLs

To get admin url, we should use below function:
  • admin_url(): fetches (but doesn't output) a URL in the admin. We need to include the path to the URL as a parameter and can also include the scheme if needed. So, for example, to output the URL for the screen to create a new post, we would use echo admin_url( 'post-new.php' );.
  • get_admin_url(): is similar to admin_url() but supports Multisite networks: we can include the blog ID as a parameter.
  • edit_post_link(): displays a link to the editing page for a post. It can be used in the loop or outside the loop with the post ID as a parameter.
  • get_edit_post_link(): fetches the link to a post's editing screen, with the post ID as its parameter.
Fetching Files path in our Plugin and Theme Folders
  • get_stylesheet_directory(): retrieves the full server path (not the URL) for the currently activated theme's directory. Use this to call include files rather than to output links.
  • get_stylesheet_directory_uri(): retrieves the URL for the currently activated theme, without a trailing slash. Use it in template files to fetch resources stored in our theme folder: for example, to display an image stored in our theme's images folder, use <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/logo.jpg">.
  • get_bloginfo( 'stylesheet_directory' ) : retrieves the URL for the currently activated theme: using get_stylesheet_directory_uri() is neater.
  • get_template_directory_uri(): is similar to get_stylesheet_directory_uri() but if we're using a child theme, it fetches the URL for the parent theme. Similarly, get_template_directory() works in the same way as get_stylesheet_directory() but for the parent theme.
  • plugins_url(): fetches the absolute URL to the plugins directory (without a trailing slash). It takes two optional parameters: the path to add after the URL, and the plugin which we want the URL to be relative to.
  • plugin_dir_url(): retrieves the URL for the directory a file is in (with a trailing slash), with that file as its parameter. Normally we would use __FILE__ as its parameter to denote the current file.
Below is example to use above 2 functions:

plugins_url( 'includes/myfile.php', __FILE__ );
And to retrieve the same URL using plugin_dir_url():

plugin_dir_url( __FILE__ ) . 'includes/myfile.php' ;
Note that with the second example we have to echo out the path after the function: The second returns a trailing slash and the first doesn't.

No comments:

Post a Comment