/wp-includes/link-template.php

  1. <?php 
  2. /** 
  3. * WordPress Link Template Functions 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Template 
  7. */ 
  8.  
  9. /** 
  10. * Displays the permalink for the current post. 
  11. * 
  12. * @since 1.2.0 
  13. * @since 4.4.0 Added the `$post` parameter. 
  14. * 
  15. * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 
  16. */ 
  17. function the_permalink( $post = 0 ) { 
  18. /** 
  19. * Filters the display of the permalink for the current post. 
  20. * 
  21. * @since 1.5.0 
  22. * @since 4.4.0 Added the `$post` parameter. 
  23. * 
  24. * @param string $permalink The permalink for the current post. 
  25. * @param int|WP_Post $post Post ID, WP_Post object, or 0. Default 0. 
  26. */ 
  27. echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) ); 
  28.  
  29. /** 
  30. * Retrieves a trailing-slashed string if the site is set for adding trailing slashes. 
  31. * 
  32. * Conditionally adds a trailing slash if the permalink structure has a trailing 
  33. * slash, strips the trailing slash if not. The string is passed through the 
  34. * {@see 'user_trailingslashit'} filter. Will remove trailing slash from string, if 
  35. * site is not set to have them. 
  36. * 
  37. * @since 2.2.0 
  38. * 
  39. * @global WP_Rewrite $wp_rewrite 
  40. * 
  41. * @param string $string URL with or without a trailing slash. 
  42. * @param string $type_of_url Optional. The type of URL being considered (e.g. single, category, etc) 
  43. * for use in the filter. Default empty string. 
  44. * @return string The URL with the trailing slash appended or stripped. 
  45. */ 
  46. function user_trailingslashit($string, $type_of_url = '') { 
  47. global $wp_rewrite; 
  48. if ( $wp_rewrite->use_trailing_slashes ) 
  49. $string = trailingslashit($string); 
  50. else 
  51. $string = untrailingslashit($string); 
  52.  
  53. /** 
  54. * Filters the trailing-slashed string, depending on whether the site is set to use trailing slashes. 
  55. * 
  56. * @since 2.2.0 
  57. * 
  58. * @param string $string URL with or without a trailing slash. 
  59. * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',  
  60. * 'single_feed', 'single_paged', 'commentpaged', 'paged', 'home', 'feed',  
  61. * 'category', 'page', 'year', 'month', 'day', 'post_type_archive'. 
  62. */ 
  63. return apply_filters( 'user_trailingslashit', $string, $type_of_url ); 
  64.  
  65. /** 
  66. * Displays the permalink anchor for the current post. 
  67. * 
  68. * The permalink mode title will use the post title for the 'a' element 'id' 
  69. * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute. 
  70. * 
  71. * @since 0.71 
  72. * 
  73. * @param string $mode Optional. Permalink mode. Accepts 'title' or 'id'. Default 'id'. 
  74. */ 
  75. function permalink_anchor( $mode = 'id' ) { 
  76. $post = get_post(); 
  77. switch ( strtolower( $mode ) ) { 
  78. case 'title': 
  79. $title = sanitize_title( $post->post_title ) . '-' . $post->ID; 
  80. echo '<a id="'.$title.'"></a>'; 
  81. break; 
  82. case 'id': 
  83. default: 
  84. echo '<a id="post-' . $post->ID . '"></a>'; 
  85. break; 
  86.  
  87. /** 
  88. * Retrieves the full permalink for the current post or post ID. 
  89. * 
  90. * This function is an alias for get_permalink(). 
  91. * 
  92. * @since 3.9.0 
  93. * 
  94. * @see get_permalink() 
  95. * 
  96. * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 
  97. * @param bool $leavename Optional. Whether to keep post name or page name. Default false. 
  98. * 
  99. * @return string|false The permalink URL or false if post does not exist. 
  100. */ 
  101. function get_the_permalink( $post = 0, $leavename = false ) { 
  102. return get_permalink( $post, $leavename ); 
  103.  
  104. /** 
  105. * Retrieves the full permalink for the current post or post ID. 
  106. * 
  107. * @since 1.0.0 
  108. * 
  109. * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 
  110. * @param bool $leavename Optional. Whether to keep post name or page name. Default false. 
  111. * @return string|false The permalink URL or false if post does not exist. 
  112. */ 
  113. function get_permalink( $post = 0, $leavename = false ) { 
  114. $rewritecode = array( 
  115. '%year%',  
  116. '%monthnum%',  
  117. '%day%',  
  118. '%hour%',  
  119. '%minute%',  
  120. '%second%',  
  121. $leavename? '' : '%postname%',  
  122. '%post_id%',  
  123. '%category%',  
  124. '%author%',  
  125. $leavename? '' : '%pagename%',  
  126. ); 
  127.  
  128. if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) { 
  129. $sample = true; 
  130. } else { 
  131. $post = get_post( $post ); 
  132. $sample = false; 
  133.  
  134. if ( empty($post->ID) ) 
  135. return false; 
  136.  
  137. if ( $post->post_type == 'page' ) 
  138. return get_page_link($post, $leavename, $sample); 
  139. elseif ( $post->post_type == 'attachment' ) 
  140. return get_attachment_link( $post, $leavename ); 
  141. elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) ) 
  142. return get_post_permalink($post, $leavename, $sample); 
  143.  
  144. $permalink = get_option('permalink_structure'); 
  145.  
  146. /** 
  147. * Filters the permalink structure for a post before token replacement occurs. 
  148. * 
  149. * Only applies to posts with post_type of 'post'. 
  150. * 
  151. * @since 3.0.0 
  152. * 
  153. * @param string $permalink The site's permalink structure. 
  154. * @param WP_Post $post The post in question. 
  155. * @param bool $leavename Whether to keep the post name. 
  156. */ 
  157. $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename ); 
  158.  
  159. if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) { 
  160. $unixtime = strtotime($post->post_date); 
  161.  
  162. $category = ''; 
  163. if ( strpos($permalink, '%category%') !== false ) { 
  164. $cats = get_the_category($post->ID); 
  165. if ( $cats ) { 
  166. $cats = wp_list_sort( $cats, array( 
  167. 'term_id' => 'ASC',  
  168. ) ); 
  169.  
  170. /** 
  171. * Filters the category that gets used in the %category% permalink token. 
  172. * 
  173. * @since 3.5.0 
  174. * 
  175. * @param WP_Term $cat The category to use in the permalink. 
  176. * @param array $cats Array of all categories (WP_Term objects) associated with the post. 
  177. * @param WP_Post $post The post in question. 
  178. */ 
  179. $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post ); 
  180.  
  181. $category_object = get_term( $category_object, 'category' ); 
  182. $category = $category_object->slug; 
  183. if ( $parent = $category_object->parent ) 
  184. $category = get_category_parents($parent, false, '/', true) . $category; 
  185. // show default category in permalinks, without 
  186. // having to assign it explicitly 
  187. if ( empty($category) ) { 
  188. $default_category = get_term( get_option( 'default_category' ), 'category' ); 
  189. if ( $default_category && ! is_wp_error( $default_category ) ) { 
  190. $category = $default_category->slug; 
  191.  
  192. $author = ''; 
  193. if ( strpos($permalink, '%author%') !== false ) { 
  194. $authordata = get_userdata($post->post_author); 
  195. $author = $authordata->user_nicename; 
  196.  
  197. $date = explode(" ", date('Y m d H i s', $unixtime)); 
  198. $rewritereplace = 
  199. array( 
  200. $date[0],  
  201. $date[1],  
  202. $date[2],  
  203. $date[3],  
  204. $date[4],  
  205. $date[5],  
  206. $post->post_name,  
  207. $post->ID,  
  208. $category,  
  209. $author,  
  210. $post->post_name,  
  211. ); 
  212. $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) ); 
  213. $permalink = user_trailingslashit($permalink, 'single'); 
  214. } else { // if they're not using the fancy permalink option 
  215. $permalink = home_url('?p=' . $post->ID); 
  216.  
  217. /** 
  218. * Filters the permalink for a post. 
  219. * 
  220. * Only applies to posts with post_type of 'post'. 
  221. * 
  222. * @since 1.5.0 
  223. * 
  224. * @param string $permalink The post's permalink. 
  225. * @param WP_Post $post The post in question. 
  226. * @param bool $leavename Whether to keep the post name. 
  227. */ 
  228. return apply_filters( 'post_link', $permalink, $post, $leavename ); 
  229.  
  230. /** 
  231. * Retrieves the permalink for a post of a custom post type. 
  232. * 
  233. * @since 3.0.0 
  234. * 
  235. * @global WP_Rewrite $wp_rewrite 
  236. * 
  237. * @param int $id Optional. Post ID. Default uses the global `$post`. 
  238. * @param bool $leavename Optional, defaults to false. Whether to keep post name. Default false. 
  239. * @param bool $sample Optional, defaults to false. Is it a sample permalink. Default false. 
  240. * @return string|WP_Error The post permalink. 
  241. */ 
  242. function get_post_permalink( $id = 0, $leavename = false, $sample = false ) { 
  243. global $wp_rewrite; 
  244.  
  245. $post = get_post($id); 
  246.  
  247. if ( is_wp_error( $post ) ) 
  248. return $post; 
  249.  
  250. $post_link = $wp_rewrite->get_extra_permastruct($post->post_type); 
  251.  
  252. $slug = $post->post_name; 
  253.  
  254. $draft_or_pending = get_post_status( $id ) && in_array( get_post_status( $id ), array( 'draft', 'pending', 'auto-draft', 'future' ) ); 
  255.  
  256. $post_type = get_post_type_object($post->post_type); 
  257.  
  258. if ( $post_type->hierarchical ) { 
  259. $slug = get_page_uri( $id ); 
  260.  
  261. if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) { 
  262. if ( ! $leavename ) { 
  263. $post_link = str_replace("%$post->post_type%", $slug, $post_link); 
  264. $post_link = home_url( user_trailingslashit($post_link) ); 
  265. } else { 
  266. if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) ) 
  267. $post_link = add_query_arg($post_type->query_var, $slug, ''); 
  268. else 
  269. $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), ''); 
  270. $post_link = home_url($post_link); 
  271.  
  272. /** 
  273. * Filters the permalink for a post of a custom post type. 
  274. * 
  275. * @since 3.0.0 
  276. * 
  277. * @param string $post_link The post's permalink. 
  278. * @param WP_Post $post The post in question. 
  279. * @param bool $leavename Whether to keep the post name. 
  280. * @param bool $sample Is it a sample permalink. 
  281. */ 
  282. return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample ); 
  283.  
  284. /** 
  285. * Retrieves the permalink for the current page or page ID. 
  286. * 
  287. * Respects page_on_front. Use this one. 
  288. * 
  289. * @since 1.5.0 
  290. * 
  291. * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`. 
  292. * @param bool $leavename Optional. Whether to keep the page name. Default false. 
  293. * @param bool $sample Optional. Whether it should be treated as a sample permalink. 
  294. * Default false. 
  295. * @return string The page permalink. 
  296. */ 
  297. function get_page_link( $post = false, $leavename = false, $sample = false ) { 
  298. $post = get_post( $post ); 
  299.  
  300. if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) ) 
  301. $link = home_url('/'); 
  302. else 
  303. $link = _get_page_link( $post, $leavename, $sample ); 
  304.  
  305. /** 
  306. * Filters the permalink for a page. 
  307. * 
  308. * @since 1.5.0 
  309. * 
  310. * @param string $link The page's permalink. 
  311. * @param int $post_id The ID of the page. 
  312. * @param bool $sample Is it a sample permalink. 
  313. */ 
  314. return apply_filters( 'page_link', $link, $post->ID, $sample ); 
  315.  
  316. /** 
  317. * Retrieves the page permalink. 
  318. * 
  319. * Ignores page_on_front. Internal use only. 
  320. * 
  321. * @since 2.1.0 
  322. * @access private 
  323. * 
  324. * @global WP_Rewrite $wp_rewrite 
  325. * 
  326. * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`. 
  327. * @param bool $leavename Optional. Whether to keep the page name. Default false. 
  328. * @param bool $sample Optional. Whether it should be treated as a sample permalink. 
  329. * Default false. 
  330. * @return string The page permalink. 
  331. */ 
  332. function _get_page_link( $post = false, $leavename = false, $sample = false ) { 
  333. global $wp_rewrite; 
  334.  
  335. $post = get_post( $post ); 
  336.  
  337. $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); 
  338.  
  339. $link = $wp_rewrite->get_page_permastruct(); 
  340.  
  341. if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) { 
  342. if ( ! $leavename ) { 
  343. $link = str_replace('%pagename%', get_page_uri( $post ), $link); 
  344.  
  345. $link = home_url($link); 
  346. $link = user_trailingslashit($link, 'page'); 
  347. } else { 
  348. $link = home_url( '?page_id=' . $post->ID ); 
  349.  
  350. /** 
  351. * Filters the permalink for a non-page_on_front page. 
  352. * 
  353. * @since 2.1.0 
  354. * 
  355. * @param string $link The page's permalink. 
  356. * @param int $post_id The ID of the page. 
  357. */ 
  358. return apply_filters( '_get_page_link', $link, $post->ID ); 
  359.  
  360. /** 
  361. * Retrieves the permalink for an attachment. 
  362. * 
  363. * This can be used in the WordPress Loop or outside of it. 
  364. * 
  365. * @since 2.0.0 
  366. * 
  367. * @global WP_Rewrite $wp_rewrite 
  368. * 
  369. * @param int|object $post Optional. Post ID or object. Default uses the global `$post`. 
  370. * @param bool $leavename Optional. Whether to keep the page name. Default false. 
  371. * @return string The attachment permalink. 
  372. */ 
  373. function get_attachment_link( $post = null, $leavename = false ) { 
  374. global $wp_rewrite; 
  375.  
  376. $link = false; 
  377.  
  378. $post = get_post( $post ); 
  379. $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false; 
  380. if ( $parent && ! in_array( $parent->post_type, get_post_types() ) ) { 
  381. $parent = false; 
  382.  
  383. if ( $wp_rewrite->using_permalinks() && $parent ) { 
  384. if ( 'page' == $parent->post_type ) 
  385. $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front 
  386. else 
  387. $parentlink = get_permalink( $post->post_parent ); 
  388.  
  389. if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') ) 
  390. $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker 
  391. else 
  392. $name = $post->post_name; 
  393.  
  394. if ( strpos($parentlink, '?') === false ) 
  395. $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' ); 
  396.  
  397. if ( ! $leavename ) 
  398. $link = str_replace( '%postname%', $name, $link ); 
  399. } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) { 
  400. $link = home_url( user_trailingslashit( $post->post_name ) ); 
  401.  
  402. if ( ! $link ) 
  403. $link = home_url( '/?attachment_id=' . $post->ID ); 
  404.  
  405. /** 
  406. * Filters the permalink for an attachment. 
  407. * 
  408. * @since 2.0.0 
  409. * 
  410. * @param string $link The attachment's permalink. 
  411. * @param int $post_id Attachment ID. 
  412. */ 
  413. return apply_filters( 'attachment_link', $link, $post->ID ); 
  414.  
  415. /** 
  416. * Retrieves the permalink for the year archives. 
  417. * 
  418. * @since 1.5.0 
  419. * 
  420. * @global WP_Rewrite $wp_rewrite 
  421. * 
  422. * @param int|bool $year False for current year or year for permalink. 
  423. * @return string The permalink for the specified year archive. 
  424. */ 
  425. function get_year_link( $year ) { 
  426. global $wp_rewrite; 
  427. if ( !$year ) 
  428. $year = gmdate('Y', current_time('timestamp')); 
  429. $yearlink = $wp_rewrite->get_year_permastruct(); 
  430. if ( !empty($yearlink) ) { 
  431. $yearlink = str_replace('%year%', $year, $yearlink); 
  432. $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) ); 
  433. } else { 
  434. $yearlink = home_url( '?m=' . $year ); 
  435.  
  436. /** 
  437. * Filters the year archive permalink. 
  438. * 
  439. * @since 1.5.0 
  440. * 
  441. * @param string $yearlink Permalink for the year archive. 
  442. * @param int $year Year for the archive. 
  443. */ 
  444. return apply_filters( 'year_link', $yearlink, $year ); 
  445.  
  446. /** 
  447. * Retrieves the permalink for the month archives with year. 
  448. * 
  449. * @since 1.0.0 
  450. * 
  451. * @global WP_Rewrite $wp_rewrite 
  452. * 
  453. * @param bool|int $year False for current year. Integer of year. 
  454. * @param bool|int $month False for current month. Integer of month. 
  455. * @return string The permalink for the specified month and year archive. 
  456. */ 
  457. function get_month_link($year, $month) { 
  458. global $wp_rewrite; 
  459. if ( !$year ) 
  460. $year = gmdate('Y', current_time('timestamp')); 
  461. if ( !$month ) 
  462. $month = gmdate('m', current_time('timestamp')); 
  463. $monthlink = $wp_rewrite->get_month_permastruct(); 
  464. if ( !empty($monthlink) ) { 
  465. $monthlink = str_replace('%year%', $year, $monthlink); 
  466. $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink); 
  467. $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) ); 
  468. } else { 
  469. $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) ); 
  470.  
  471. /** 
  472. * Filters the month archive permalink. 
  473. * 
  474. * @since 1.5.0 
  475. * 
  476. * @param string $monthlink Permalink for the month archive. 
  477. * @param int $year Year for the archive. 
  478. * @param int $month The month for the archive. 
  479. */ 
  480. return apply_filters( 'month_link', $monthlink, $year, $month ); 
  481.  
  482. /** 
  483. * Retrieves the permalink for the day archives with year and month. 
  484. * 
  485. * @since 1.0.0 
  486. * 
  487. * @global WP_Rewrite $wp_rewrite 
  488. * 
  489. * @param bool|int $year False for current year. Integer of year. 
  490. * @param bool|int $month False for current month. Integer of month. 
  491. * @param bool|int $day False for current day. Integer of day. 
  492. * @return string The permalink for the specified day, month, and year archive. 
  493. */ 
  494. function get_day_link($year, $month, $day) { 
  495. global $wp_rewrite; 
  496. if ( !$year ) 
  497. $year = gmdate('Y', current_time('timestamp')); 
  498. if ( !$month ) 
  499. $month = gmdate('m', current_time('timestamp')); 
  500. if ( !$day ) 
  501. $day = gmdate('j', current_time('timestamp')); 
  502.  
  503. $daylink = $wp_rewrite->get_day_permastruct(); 
  504. if ( !empty($daylink) ) { 
  505. $daylink = str_replace('%year%', $year, $daylink); 
  506. $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink); 
  507. $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink); 
  508. $daylink = home_url( user_trailingslashit( $daylink, 'day' ) ); 
  509. } else { 
  510. $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) ); 
  511.  
  512. /** 
  513. * Filters the day archive permalink. 
  514. * 
  515. * @since 1.5.0 
  516. * 
  517. * @param string $daylink Permalink for the day archive. 
  518. * @param int $year Year for the archive. 
  519. * @param int $month Month for the archive. 
  520. * @param int $day The day for the archive. 
  521. */ 
  522. return apply_filters( 'day_link', $daylink, $year, $month, $day ); 
  523.  
  524. /** 
  525. * Displays the permalink for the feed type. 
  526. * 
  527. * @since 3.0.0 
  528. * 
  529. * @param string $anchor The link's anchor text. 
  530. * @param string $feed Optional. Feed type. Default empty. 
  531. */ 
  532. function the_feed_link( $anchor, $feed = '' ) { 
  533. $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>'; 
  534.  
  535. /** 
  536. * Filters the feed link anchor tag. 
  537. * 
  538. * @since 3.0.0 
  539. * 
  540. * @param string $link The complete anchor tag for a feed link. 
  541. * @param string $feed The feed type, or an empty string for the 
  542. * default feed type. 
  543. */ 
  544. echo apply_filters( 'the_feed_link', $link, $feed ); 
  545.  
  546. /** 
  547. * Retrieves the permalink for the feed type. 
  548. * 
  549. * @since 1.5.0 
  550. * 
  551. * @global WP_Rewrite $wp_rewrite 
  552. * 
  553. * @param string $feed Optional. Feed type. Default empty. 
  554. * @return string The feed permalink. 
  555. */ 
  556. function get_feed_link( $feed = '' ) { 
  557. global $wp_rewrite; 
  558.  
  559. $permalink = $wp_rewrite->get_feed_permastruct(); 
  560. if ( '' != $permalink ) { 
  561. if ( false !== strpos($feed, 'comments_') ) { 
  562. $feed = str_replace('comments_', '', $feed); 
  563. $permalink = $wp_rewrite->get_comment_feed_permastruct(); 
  564.  
  565. if ( get_default_feed() == $feed ) 
  566. $feed = ''; 
  567.  
  568. $permalink = str_replace('%feed%', $feed, $permalink); 
  569. $permalink = preg_replace('#/+#', '/', "/$permalink"); 
  570. $output = home_url( user_trailingslashit($permalink, 'feed') ); 
  571. } else { 
  572. if ( empty($feed) ) 
  573. $feed = get_default_feed(); 
  574.  
  575. if ( false !== strpos($feed, 'comments_') ) 
  576. $feed = str_replace('comments_', 'comments-', $feed); 
  577.  
  578. $output = home_url("?feed={$feed}"); 
  579.  
  580. /** 
  581. * Filters the feed type permalink. 
  582. * 
  583. * @since 1.5.0 
  584. * 
  585. * @param string $output The feed permalink. 
  586. * @param string $feed Feed type. 
  587. */ 
  588. return apply_filters( 'feed_link', $output, $feed ); 
  589.  
  590. /** 
  591. * Retrieves the permalink for the post comments feed. 
  592. * 
  593. * @since 2.2.0 
  594. * 
  595. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. 
  596. * @param string $feed Optional. Feed type. Default empty. 
  597. * @return string The permalink for the comments feed for the given post. 
  598. */ 
  599. function get_post_comments_feed_link( $post_id = 0, $feed = '' ) { 
  600. $post_id = absint( $post_id ); 
  601.  
  602. if ( ! $post_id ) 
  603. $post_id = get_the_ID(); 
  604.  
  605. if ( empty( $feed ) ) 
  606. $feed = get_default_feed(); 
  607.  
  608. $post = get_post( $post_id ); 
  609. $unattached = 'attachment' === $post->post_type && 0 === (int) $post->post_parent; 
  610.  
  611. if ( '' != get_option('permalink_structure') ) { 
  612. if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') ) 
  613. $url = _get_page_link( $post_id ); 
  614. else 
  615. $url = get_permalink($post_id); 
  616.  
  617. if ( $unattached ) { 
  618. $url = home_url( '/feed/' ); 
  619. if ( $feed !== get_default_feed() ) { 
  620. $url .= "$feed/"; 
  621. $url = add_query_arg( 'attachment_id', $post_id, $url ); 
  622. } else { 
  623. $url = trailingslashit($url) . 'feed'; 
  624. if ( $feed != get_default_feed() ) 
  625. $url .= "/$feed"; 
  626. $url = user_trailingslashit($url, 'single_feed'); 
  627. } else { 
  628. if ( $unattached ) { 
  629. $url = add_query_arg( array( 'feed' => $feed, 'attachment_id' => $post_id ), home_url( '/' ) ); 
  630. } elseif ( 'page' == $post->post_type ) { 
  631. $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) ); 
  632. } else { 
  633. $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) ); 
  634.  
  635. /** 
  636. * Filters the post comments feed permalink. 
  637. * 
  638. * @since 1.5.1 
  639. * 
  640. * @param string $url Post comments feed permalink. 
  641. */ 
  642. return apply_filters( 'post_comments_feed_link', $url ); 
  643.  
  644. /** 
  645. * Displays the comment feed link for a post. 
  646. * 
  647. * Prints out the comment feed link for a post. Link text is placed in the 
  648. * anchor. If no link text is specified, default text is used. If no post ID is 
  649. * specified, the current post is used. 
  650. * 
  651. * @since 2.5.0 
  652. * 
  653. * @param string $link_text Optional. Descriptive link text. Default 'Comments Feed'. 
  654. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. 
  655. * @param string $feed Optional. Feed format. Default empty. 
  656. */ 
  657. function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) { 
  658. $url = get_post_comments_feed_link( $post_id, $feed ); 
  659. if ( empty( $link_text ) ) { 
  660. $link_text = __('Comments Feed'); 
  661.  
  662. $link = '<a href="' . esc_url( $url ) . '">' . $link_text . '</a>'; 
  663. /** 
  664. * Filters the post comment feed link anchor tag. 
  665. * 
  666. * @since 2.8.0 
  667. * 
  668. * @param string $link The complete anchor tag for the comment feed link. 
  669. * @param int $post_id Post ID. 
  670. * @param string $feed The feed type, or an empty string for the default feed type. 
  671. */ 
  672. echo apply_filters( 'post_comments_feed_link_html', $link, $post_id, $feed ); 
  673.  
  674. /** 
  675. * Retrieves the feed link for a given author. 
  676. * 
  677. * Returns a link to the feed for all posts by a given author. A specific feed 
  678. * can be requested or left blank to get the default feed. 
  679. * 
  680. * @since 2.5.0 
  681. * 
  682. * @param int $author_id Author ID. 
  683. * @param string $feed Optional. Feed type. Default empty. 
  684. * @return string Link to the feed for the author specified by $author_id. 
  685. */ 
  686. function get_author_feed_link( $author_id, $feed = '' ) { 
  687. $author_id = (int) $author_id; 
  688. $permalink_structure = get_option('permalink_structure'); 
  689.  
  690. if ( empty($feed) ) 
  691. $feed = get_default_feed(); 
  692.  
  693. if ( '' == $permalink_structure ) { 
  694. $link = home_url("?feed=$feed&author=" . $author_id); 
  695. } else { 
  696. $link = get_author_posts_url($author_id); 
  697. if ( $feed == get_default_feed() ) 
  698. $feed_link = 'feed'; 
  699. else 
  700. $feed_link = "feed/$feed"; 
  701.  
  702. $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed'); 
  703.  
  704. /** 
  705. * Filters the feed link for a given author. 
  706. * 
  707. * @since 1.5.1 
  708. * 
  709. * @param string $link The author feed link. 
  710. * @param string $feed Feed type. 
  711. */ 
  712. $link = apply_filters( 'author_feed_link', $link, $feed ); 
  713.  
  714. return $link; 
  715.  
  716. /** 
  717. * Retrieves the feed link for a category. 
  718. * 
  719. * Returns a link to the feed for all posts in a given category. A specific feed 
  720. * can be requested or left blank to get the default feed. 
  721. * 
  722. * @since 2.5.0 
  723. * 
  724. * @param int $cat_id Category ID. 
  725. * @param string $feed Optional. Feed type. Default empty. 
  726. * @return string Link to the feed for the category specified by $cat_id. 
  727. */ 
  728. function get_category_feed_link( $cat_id, $feed = '' ) { 
  729. return get_term_feed_link( $cat_id, 'category', $feed ); 
  730.  
  731. /** 
  732. * Retrieves the feed link for a term. 
  733. * 
  734. * Returns a link to the feed for all posts in a given term. A specific feed 
  735. * can be requested or left blank to get the default feed. 
  736. * 
  737. * @since 3.0.0 
  738. * 
  739. * @param int $term_id Term ID. 
  740. * @param string $taxonomy Optional. Taxonomy of `$term_id`. Default 'category'. 
  741. * @param string $feed Optional. Feed type. Default empty. 
  742. * @return string|false Link to the feed for the term specified by $term_id and $taxonomy. 
  743. */ 
  744. function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) { 
  745. $term_id = (int) $term_id; 
  746.  
  747. $term = get_term( $term_id, $taxonomy ); 
  748.  
  749. if ( empty( $term ) || is_wp_error( $term ) ) 
  750. return false; 
  751.  
  752. if ( empty( $feed ) ) 
  753. $feed = get_default_feed(); 
  754.  
  755. $permalink_structure = get_option( 'permalink_structure' ); 
  756.  
  757. if ( '' == $permalink_structure ) { 
  758. if ( 'category' == $taxonomy ) { 
  759. $link = home_url("?feed=$feed&cat=$term_id"); 
  760. elseif ( 'post_tag' == $taxonomy ) { 
  761. $link = home_url("?feed=$feed&tag=$term->slug"); 
  762. } else { 
  763. $t = get_taxonomy( $taxonomy ); 
  764. $link = home_url("?feed=$feed&$t->query_var=$term->slug"); 
  765. } else { 
  766. $link = get_term_link( $term_id, $term->taxonomy ); 
  767. if ( $feed == get_default_feed() ) 
  768. $feed_link = 'feed'; 
  769. else 
  770. $feed_link = "feed/$feed"; 
  771.  
  772. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' ); 
  773.  
  774. if ( 'category' == $taxonomy ) { 
  775. /** 
  776. * Filters the category feed link. 
  777. * 
  778. * @since 1.5.1 
  779. * 
  780. * @param string $link The category feed link. 
  781. * @param string $feed Feed type. 
  782. */ 
  783. $link = apply_filters( 'category_feed_link', $link, $feed ); 
  784. } elseif ( 'post_tag' == $taxonomy ) { 
  785. /** 
  786. * Filters the post tag feed link. 
  787. * 
  788. * @since 2.3.0 
  789. * 
  790. * @param string $link The tag feed link. 
  791. * @param string $feed Feed type. 
  792. */ 
  793. $link = apply_filters( 'tag_feed_link', $link, $feed ); 
  794. } else { 
  795. /** 
  796. * Filters the feed link for a taxonomy other than 'category' or 'post_tag'. 
  797. * 
  798. * @since 3.0.0 
  799. * 
  800. * @param string $link The taxonomy feed link. 
  801. * @param string $feed Feed type. 
  802. * @param string $feed The taxonomy name. 
  803. */ 
  804. $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy ); 
  805.  
  806. return $link; 
  807.  
  808. /** 
  809. * Retrieves the permalink for a tag feed. 
  810. * 
  811. * @since 2.3.0 
  812. * 
  813. * @param int $tag_id Tag ID. 
  814. * @param string $feed Optional. Feed type. Default empty. 
  815. * @return string The feed permalink for the given tag. 
  816. */ 
  817. function get_tag_feed_link( $tag_id, $feed = '' ) { 
  818. return get_term_feed_link( $tag_id, 'post_tag', $feed ); 
  819.  
  820. /** 
  821. * Retrieves the edit link for a tag. 
  822. * 
  823. * @since 2.7.0 
  824. * 
  825. * @param int $tag_id Tag ID. 
  826. * @param string $taxonomy Optional. Taxonomy slug. Default 'post_tag'. 
  827. * @return string The edit tag link URL for the given tag. 
  828. */ 
  829. function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) { 
  830. /** 
  831. * Filters the edit link for a tag (or term in another taxonomy). 
  832. * 
  833. * @since 2.7.0 
  834. * 
  835. * @param string $link The term edit link. 
  836. */ 
  837. return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) ); 
  838.  
  839. /** 
  840. * Displays or retrieves the edit link for a tag with formatting. 
  841. * 
  842. * @since 2.7.0 
  843. * 
  844. * @param string $link Optional. Anchor text. Default empty. 
  845. * @param string $before Optional. Display before edit link. Default empty. 
  846. * @param string $after Optional. Display after edit link. Default empty. 
  847. * @param WP_Term $tag Optional. Term object. If null, the queried object will be inspected. 
  848. * Default null. 
  849. */ 
  850. function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) { 
  851. $link = edit_term_link( $link, '', '', $tag, false ); 
  852.  
  853. /** 
  854. * Filters the anchor tag for the edit link for a tag (or term in another taxonomy). 
  855. * 
  856. * @since 2.7.0 
  857. * 
  858. * @param string $link The anchor tag for the edit link. 
  859. */ 
  860. echo $before . apply_filters( 'edit_tag_link', $link ) . $after; 
  861.  
  862. /** 
  863. * Retrieves the URL for editing a given term. 
  864. * 
  865. * @since 3.1.0 
  866. * @since 4.5.0 The `$taxonomy` argument was made optional. 
  867. * 
  868. * @param int $term_id Term ID. 
  869. * @param string $taxonomy Optional. Taxonomy. Defaults to the taxonomy of the term identified 
  870. * by `$term_id`. 
  871. * @param string $object_type Optional. The object type. Used to highlight the proper post type 
  872. * menu on the linked page. Defaults to the first object_type associated 
  873. * with the taxonomy. 
  874. * @return string|null The edit term link URL for the given term, or null on failure. 
  875. */ 
  876. function get_edit_term_link( $term_id, $taxonomy = '', $object_type = '' ) { 
  877. $term = get_term( $term_id, $taxonomy ); 
  878. if ( ! $term || is_wp_error( $term ) ) { 
  879. return; 
  880.  
  881. $tax = get_taxonomy( $term->taxonomy ); 
  882. if ( ! $tax || ! current_user_can( 'edit_term', $term->term_id ) ) { 
  883. return; 
  884.  
  885. $args = array( 
  886. 'taxonomy' => $taxonomy,  
  887. 'tag_ID' => $term->term_id,  
  888. ); 
  889.  
  890. if ( $object_type ) { 
  891. $args['post_type'] = $object_type; 
  892. } elseif ( ! empty( $tax->object_type ) ) { 
  893. $args['post_type'] = reset( $tax->object_type ); 
  894.  
  895. if ( $tax->show_ui ) { 
  896. $location = add_query_arg( $args, admin_url( 'term.php' ) ); 
  897. } else { 
  898. $location = ''; 
  899.  
  900. /** 
  901. * Filters the edit link for a term. 
  902. * 
  903. * @since 3.1.0 
  904. * 
  905. * @param string $location The edit link. 
  906. * @param int $term_id Term ID. 
  907. * @param string $taxonomy Taxonomy name. 
  908. * @param string $object_type The object type (eg. the post type). 
  909. */ 
  910. return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type ); 
  911.  
  912. /** 
  913. * Displays or retrieves the edit term link with formatting. 
  914. * 
  915. * @since 3.1.0 
  916. * 
  917. * @param string $link Optional. Anchor text. Default empty. 
  918. * @param string $before Optional. Display before edit link. Default empty. 
  919. * @param string $after Optional. Display after edit link. Default empty. 
  920. * @param object $term Optional. Term object. If null, the queried object will be inspected. Default null. 
  921. * @param bool $echo Optional. Whether or not to echo the return. Default true. 
  922. * @return string|void HTML content. 
  923. */ 
  924. function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) { 
  925. if ( is_null( $term ) ) 
  926. $term = get_queried_object(); 
  927.  
  928. if ( ! $term ) 
  929. return; 
  930.  
  931. $tax = get_taxonomy( $term->taxonomy ); 
  932. if ( ! current_user_can( 'edit_term', $term->term_id ) ) { 
  933. return; 
  934.  
  935. if ( empty( $link ) ) 
  936. $link = __('Edit This'); 
  937.  
  938. $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>'; 
  939.  
  940. /** 
  941. * Filters the anchor tag for the edit link of a term. 
  942. * 
  943. * @since 3.1.0 
  944. * 
  945. * @param string $link The anchor tag for the edit link. 
  946. * @param int $term_id Term ID. 
  947. */ 
  948. $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after; 
  949.  
  950. if ( $echo ) 
  951. echo $link; 
  952. else 
  953. return $link; 
  954.  
  955. /** 
  956. * Retrieves the permalink for a search. 
  957. * 
  958. * @since 3.0.0 
  959. * 
  960. * @global WP_Rewrite $wp_rewrite 
  961. * 
  962. * @param string $query Optional. The query string to use. If empty the current query is used. Default empty. 
  963. * @return string The search permalink. 
  964. */ 
  965. function get_search_link( $query = '' ) { 
  966. global $wp_rewrite; 
  967.  
  968. if ( empty($query) ) 
  969. $search = get_search_query( false ); 
  970. else 
  971. $search = stripslashes($query); 
  972.  
  973. $permastruct = $wp_rewrite->get_search_permastruct(); 
  974.  
  975. if ( empty( $permastruct ) ) { 
  976. $link = home_url('?s=' . urlencode($search) ); 
  977. } else { 
  978. $search = urlencode($search); 
  979. $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it un-encoded. 
  980. $link = str_replace( '%search%', $search, $permastruct ); 
  981. $link = home_url( user_trailingslashit( $link, 'search' ) ); 
  982.  
  983. /** 
  984. * Filters the search permalink. 
  985. * 
  986. * @since 3.0.0 
  987. * 
  988. * @param string $link Search permalink. 
  989. * @param string $search The URL-encoded search term. 
  990. */ 
  991. return apply_filters( 'search_link', $link, $search ); 
  992.  
  993. /** 
  994. * Retrieves the permalink for the search results feed. 
  995. * 
  996. * @since 2.5.0 
  997. * 
  998. * @global WP_Rewrite $wp_rewrite 
  999. * 
  1000. * @param string $search_query Optional. Search query. Default empty. 
  1001. * @param string $feed Optional. Feed type. Default empty. 
  1002. * @return string The search results feed permalink. 
  1003. */ 
  1004. function get_search_feed_link($search_query = '', $feed = '') { 
  1005. global $wp_rewrite; 
  1006. $link = get_search_link($search_query); 
  1007.  
  1008. if ( empty($feed) ) 
  1009. $feed = get_default_feed(); 
  1010.  
  1011. $permastruct = $wp_rewrite->get_search_permastruct(); 
  1012.  
  1013. if ( empty($permastruct) ) { 
  1014. $link = add_query_arg('feed', $feed, $link); 
  1015. } else { 
  1016. $link = trailingslashit($link); 
  1017. $link .= "feed/$feed/"; 
  1018.  
  1019. /** 
  1020. * Filters the search feed link. 
  1021. * 
  1022. * @since 2.5.0 
  1023. * 
  1024. * @param string $link Search feed link. 
  1025. * @param string $feed Feed type. 
  1026. * @param string $type The search type. One of 'posts' or 'comments'. 
  1027. */ 
  1028. return apply_filters( 'search_feed_link', $link, $feed, 'posts' ); 
  1029.  
  1030. /** 
  1031. * Retrieves the permalink for the search results comments feed. 
  1032. * 
  1033. * @since 2.5.0 
  1034. * 
  1035. * @global WP_Rewrite $wp_rewrite 
  1036. * 
  1037. * @param string $search_query Optional. Search query. Default empty. 
  1038. * @param string $feed Optional. Feed type. Default empty. 
  1039. * @return string The comments feed search results permalink. 
  1040. */ 
  1041. function get_search_comments_feed_link($search_query = '', $feed = '') { 
  1042. global $wp_rewrite; 
  1043.  
  1044. if ( empty($feed) ) 
  1045. $feed = get_default_feed(); 
  1046.  
  1047. $link = get_search_feed_link($search_query, $feed); 
  1048.  
  1049. $permastruct = $wp_rewrite->get_search_permastruct(); 
  1050.  
  1051. if ( empty($permastruct) ) 
  1052. $link = add_query_arg('feed', 'comments-' . $feed, $link); 
  1053. else 
  1054. $link = add_query_arg('withcomments', 1, $link); 
  1055.  
  1056. /** This filter is documented in wp-includes/link-template.php */ 
  1057. return apply_filters( 'search_feed_link', $link, $feed, 'comments' ); 
  1058.  
  1059. /** 
  1060. * Retrieves the permalink for a post type archive. 
  1061. * 
  1062. * @since 3.1.0 
  1063. * @since 4.5.0 Support for posts was added. 
  1064. * 
  1065. * @global WP_Rewrite $wp_rewrite 
  1066. * 
  1067. * @param string $post_type Post type. 
  1068. * @return string|false The post type archive permalink. 
  1069. */ 
  1070. function get_post_type_archive_link( $post_type ) { 
  1071. global $wp_rewrite; 
  1072. if ( ! $post_type_obj = get_post_type_object( $post_type ) ) 
  1073. return false; 
  1074.  
  1075. if ( 'post' === $post_type ) { 
  1076. $show_on_front = get_option( 'show_on_front' ); 
  1077. $page_for_posts = get_option( 'page_for_posts' ); 
  1078.  
  1079. if ( 'page' == $show_on_front && $page_for_posts ) { 
  1080. $link = get_permalink( $page_for_posts ); 
  1081. } else { 
  1082. $link = get_home_url(); 
  1083. /** This filter is documented in wp-includes/link-template.php */ 
  1084. return apply_filters( 'post_type_archive_link', $link, $post_type ); 
  1085.  
  1086. if ( ! $post_type_obj->has_archive ) 
  1087. return false; 
  1088.  
  1089. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) { 
  1090. $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive; 
  1091. if ( $post_type_obj->rewrite['with_front'] ) 
  1092. $struct = $wp_rewrite->front . $struct; 
  1093. else 
  1094. $struct = $wp_rewrite->root . $struct; 
  1095. $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) ); 
  1096. } else { 
  1097. $link = home_url( '?post_type=' . $post_type ); 
  1098.  
  1099. /** 
  1100. * Filters the post type archive permalink. 
  1101. * 
  1102. * @since 3.1.0 
  1103. * 
  1104. * @param string $link The post type archive permalink. 
  1105. * @param string $post_type Post type name. 
  1106. */ 
  1107. return apply_filters( 'post_type_archive_link', $link, $post_type ); 
  1108.  
  1109. /** 
  1110. * Retrieves the permalink for a post type archive feed. 
  1111. * 
  1112. * @since 3.1.0 
  1113. * 
  1114. * @param string $post_type Post type 
  1115. * @param string $feed Optional. Feed type. Default empty. 
  1116. * @return string|false The post type feed permalink. 
  1117. */ 
  1118. function get_post_type_archive_feed_link( $post_type, $feed = '' ) { 
  1119. $default_feed = get_default_feed(); 
  1120. if ( empty( $feed ) ) 
  1121. $feed = $default_feed; 
  1122.  
  1123. if ( ! $link = get_post_type_archive_link( $post_type ) ) 
  1124. return false; 
  1125.  
  1126. $post_type_obj = get_post_type_object( $post_type ); 
  1127. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) { 
  1128. $link = trailingslashit( $link ); 
  1129. $link .= 'feed/'; 
  1130. if ( $feed != $default_feed ) 
  1131. $link .= "$feed/"; 
  1132. } else { 
  1133. $link = add_query_arg( 'feed', $feed, $link ); 
  1134.  
  1135. /** 
  1136. * Filters the post type archive feed link. 
  1137. * 
  1138. * @since 3.1.0 
  1139. * 
  1140. * @param string $link The post type archive feed link. 
  1141. * @param string $feed Feed type. 
  1142. */ 
  1143. return apply_filters( 'post_type_archive_feed_link', $link, $feed ); 
  1144.  
  1145. /** 
  1146. * Retrieves the URL used for the post preview. 
  1147. * 
  1148. * Allows additional query args to be appended. 
  1149. * 
  1150. * @since 4.4.0 
  1151. * 
  1152. * @param int|WP_Post $post Optional. Post ID or `WP_Post` object. Defaults to global `$post`. 
  1153. * @param array $query_args Optional. Array of additional query args to be appended to the link. 
  1154. * Default empty array. 
  1155. * @param string $preview_link Optional. Base preview link to be used if it should differ from the 
  1156. * post permalink. Default empty. 
  1157. * @return string|null URL used for the post preview, or null if the post does not exist. 
  1158. */ 
  1159. function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) { 
  1160. $post = get_post( $post ); 
  1161. if ( ! $post ) { 
  1162. return; 
  1163.  
  1164. $post_type_object = get_post_type_object( $post->post_type ); 
  1165. if ( is_post_type_viewable( $post_type_object ) ) { 
  1166. if ( ! $preview_link ) { 
  1167. $preview_link = set_url_scheme( get_permalink( $post ) ); 
  1168.  
  1169. $query_args['preview'] = 'true'; 
  1170. $preview_link = add_query_arg( $query_args, $preview_link ); 
  1171.  
  1172. /** 
  1173. * Filters the URL used for a post preview. 
  1174. * 
  1175. * @since 2.0.5 
  1176. * @since 4.0.0 Added the `$post` parameter. 
  1177. * 
  1178. * @param string $preview_link URL used for the post preview. 
  1179. * @param WP_Post $post Post object. 
  1180. */ 
  1181. return apply_filters( 'preview_post_link', $preview_link, $post ); 
  1182.  
  1183. /** 
  1184. * Retrieves the edit post link for post. 
  1185. * 
  1186. * Can be used within the WordPress loop or outside of it. Can be used with 
  1187. * pages, posts, attachments, and revisions. 
  1188. * 
  1189. * @since 2.3.0 
  1190. * 
  1191. * @param int $id Optional. Post ID. Default is the ID of the global `$post`. 
  1192. * @param string $context Optional. How to output the '&' character. Default '&'. 
  1193. * @return string|null The edit post link for the given post. null if the post type is invalid or does 
  1194. * not allow an editing UI. 
  1195. */ 
  1196. function get_edit_post_link( $id = 0, $context = 'display' ) { 
  1197. if ( ! $post = get_post( $id ) ) 
  1198. return; 
  1199.  
  1200. if ( 'revision' === $post->post_type ) 
  1201. $action = ''; 
  1202. elseif ( 'display' == $context ) 
  1203. $action = '&action=edit'; 
  1204. else 
  1205. $action = '&action=edit'; 
  1206.  
  1207. $post_type_object = get_post_type_object( $post->post_type ); 
  1208. if ( !$post_type_object ) 
  1209. return; 
  1210.  
  1211. if ( !current_user_can( 'edit_post', $post->ID ) ) 
  1212. return; 
  1213.  
  1214. if ( $post_type_object->_edit_link ) { 
  1215. $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ); 
  1216. } else { 
  1217. $link = ''; 
  1218.  
  1219. /** 
  1220. * Filters the post edit link. 
  1221. * 
  1222. * @since 2.3.0 
  1223. * 
  1224. * @param string $link The edit link. 
  1225. * @param int $post_id Post ID. 
  1226. * @param string $context The link context. If set to 'display' then ampersands 
  1227. * are encoded. 
  1228. */ 
  1229. return apply_filters( 'get_edit_post_link', $link, $post->ID, $context ); 
  1230.  
  1231. /** 
  1232. * Displays the edit post link for post. 
  1233. * 
  1234. * @since 1.0.0 
  1235. * @since 4.4.0 The `$class` argument was added. 
  1236. * 
  1237. * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null. 
  1238. * @param string $before Optional. Display before edit link. Default empty. 
  1239. * @param string $after Optional. Display after edit link. Default empty. 
  1240. * @param int $id Optional. Post ID. Default is the ID of the global `$post`. 
  1241. * @param string $class Optional. Add custom class to link. Default 'post-edit-link'. 
  1242. */ 
  1243. function edit_post_link( $text = null, $before = '', $after = '', $id = 0, $class = 'post-edit-link' ) { 
  1244. if ( ! $post = get_post( $id ) ) { 
  1245. return; 
  1246.  
  1247. if ( ! $url = get_edit_post_link( $post->ID ) ) { 
  1248. return; 
  1249.  
  1250. if ( null === $text ) { 
  1251. $text = __( 'Edit This' ); 
  1252.  
  1253. $link = '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>'; 
  1254.  
  1255. /** 
  1256. * Filters the post edit link anchor tag. 
  1257. * 
  1258. * @since 2.3.0 
  1259. * 
  1260. * @param string $link Anchor tag for the edit link. 
  1261. * @param int $post_id Post ID. 
  1262. * @param string $text Anchor text. 
  1263. */ 
  1264. echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after; 
  1265.  
  1266. /** 
  1267. * Retrieves the delete posts link for post. 
  1268. * 
  1269. * Can be used within the WordPress loop or outside of it, with any post type. 
  1270. * 
  1271. * @since 2.9.0 
  1272. * 
  1273. * @param int $id Optional. Post ID. Default is the ID of the global `$post`. 
  1274. * @param string $deprecated Not used. 
  1275. * @param bool $force_delete Optional. Whether to bypass trash and force deletion. Default false. 
  1276. * @return string|void The delete post link URL for the given post. 
  1277. */ 
  1278. function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) { 
  1279. if ( ! empty( $deprecated ) ) 
  1280. _deprecated_argument( __FUNCTION__, '3.0.0' ); 
  1281.  
  1282. if ( !$post = get_post( $id ) ) 
  1283. return; 
  1284.  
  1285. $post_type_object = get_post_type_object( $post->post_type ); 
  1286. if ( !$post_type_object ) 
  1287. return; 
  1288.  
  1289. if ( !current_user_can( 'delete_post', $post->ID ) ) 
  1290. return; 
  1291.  
  1292. $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash'; 
  1293.  
  1294. $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) ); 
  1295.  
  1296. /** 
  1297. * Filters the post delete link. 
  1298. * 
  1299. * @since 2.9.0 
  1300. * 
  1301. * @param string $link The delete link. 
  1302. * @param int $post_id Post ID. 
  1303. * @param bool $force_delete Whether to bypass the trash and force deletion. Default false. 
  1304. */ 
  1305. return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete ); 
  1306.  
  1307. /** 
  1308. * Retrieves the edit comment link. 
  1309. * 
  1310. * @since 2.3.0 
  1311. * 
  1312. * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object. 
  1313. * @return string|void The edit comment link URL for the given comment. 
  1314. */ 
  1315. function get_edit_comment_link( $comment_id = 0 ) { 
  1316. $comment = get_comment( $comment_id ); 
  1317.  
  1318. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) ) 
  1319. return; 
  1320.  
  1321. $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID; 
  1322.  
  1323. /** 
  1324. * Filters the comment edit link. 
  1325. * 
  1326. * @since 2.3.0 
  1327. * 
  1328. * @param string $location The edit link. 
  1329. */ 
  1330. return apply_filters( 'get_edit_comment_link', $location ); 
  1331.  
  1332. /** 
  1333. * Displays the edit comment link with formatting. 
  1334. * 
  1335. * @since 1.0.0 
  1336. * 
  1337. * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null. 
  1338. * @param string $before Optional. Display before edit link. Default empty. 
  1339. * @param string $after Optional. Display after edit link. Default empty. 
  1340. */ 
  1341. function edit_comment_link( $text = null, $before = '', $after = '' ) { 
  1342. $comment = get_comment(); 
  1343.  
  1344. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) { 
  1345. return; 
  1346.  
  1347. if ( null === $text ) { 
  1348. $text = __( 'Edit This' ); 
  1349.  
  1350. $link = '<a class="comment-edit-link" href="' . esc_url( get_edit_comment_link( $comment ) ) . '">' . $text . '</a>'; 
  1351.  
  1352. /** 
  1353. * Filters the comment edit link anchor tag. 
  1354. * 
  1355. * @since 2.3.0 
  1356. * 
  1357. * @param string $link Anchor tag for the edit link. 
  1358. * @param int $comment_id Comment ID. 
  1359. * @param string $text Anchor text. 
  1360. */ 
  1361. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after; 
  1362.  
  1363. /** 
  1364. * Displays the edit bookmark link. 
  1365. * 
  1366. * @since 2.7.0 
  1367. * 
  1368. * @param int|stdClass $link Optional. Bookmark ID. Default is the id of the current bookmark. 
  1369. * @return string|void The edit bookmark link URL. 
  1370. */ 
  1371. function get_edit_bookmark_link( $link = 0 ) { 
  1372. $link = get_bookmark( $link ); 
  1373.  
  1374. if ( !current_user_can('manage_links') ) 
  1375. return; 
  1376.  
  1377. $location = admin_url('link.php?action=edit&link_id=') . $link->link_id; 
  1378.  
  1379. /** 
  1380. * Filters the bookmark edit link. 
  1381. * 
  1382. * @since 2.7.0 
  1383. * 
  1384. * @param string $location The edit link. 
  1385. * @param int $link_id Bookmark ID. 
  1386. */ 
  1387. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id ); 
  1388.  
  1389. /** 
  1390. * Displays the edit bookmark link anchor content. 
  1391. * 
  1392. * @since 2.7.0 
  1393. * 
  1394. * @param string $link Optional. Anchor text. Default empty. 
  1395. * @param string $before Optional. Display before edit link. Default empty. 
  1396. * @param string $after Optional. Display after edit link. Default empty. 
  1397. * @param int $bookmark Optional. Bookmark ID. Default is the current bookmark. 
  1398. */ 
  1399. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) { 
  1400. $bookmark = get_bookmark($bookmark); 
  1401.  
  1402. if ( !current_user_can('manage_links') ) 
  1403. return; 
  1404.  
  1405. if ( empty($link) ) 
  1406. $link = __('Edit This'); 
  1407.  
  1408. $link = '<a href="' . esc_url( get_edit_bookmark_link( $bookmark ) ) . '">' . $link . '</a>'; 
  1409.  
  1410. /** 
  1411. * Filters the bookmark edit link anchor tag. 
  1412. * 
  1413. * @since 2.7.0 
  1414. * 
  1415. * @param string $link Anchor tag for the edit link. 
  1416. * @param int $link_id Bookmark ID. 
  1417. */ 
  1418. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after; 
  1419.  
  1420. /** 
  1421. * Retrieves the edit user link. 
  1422. * 
  1423. * @since 3.5.0 
  1424. * 
  1425. * @param int $user_id Optional. User ID. Defaults to the current user. 
  1426. * @return string URL to edit user page or empty string. 
  1427. */ 
  1428. function get_edit_user_link( $user_id = null ) { 
  1429. if ( ! $user_id ) 
  1430. $user_id = get_current_user_id(); 
  1431.  
  1432. if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) ) 
  1433. return ''; 
  1434.  
  1435. $user = get_userdata( $user_id ); 
  1436.  
  1437. if ( ! $user ) 
  1438. return ''; 
  1439.  
  1440. if ( get_current_user_id() == $user->ID ) 
  1441. $link = get_edit_profile_url( $user->ID ); 
  1442. else 
  1443. $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) ); 
  1444.  
  1445. /** 
  1446. * Filters the user edit link. 
  1447. * 
  1448. * @since 3.5.0 
  1449. * 
  1450. * @param string $link The edit link. 
  1451. * @param int $user_id User ID. 
  1452. */ 
  1453. return apply_filters( 'get_edit_user_link', $link, $user->ID ); 
  1454.  
  1455. // Navigation links 
  1456.  
  1457. /** 
  1458. * Retrieves the previous post that is adjacent to the current post. 
  1459. * 
  1460. * @since 1.5.0 
  1461. * 
  1462. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false. 
  1463. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1464. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1465. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no 
  1466. * corresponding post exists. 
  1467. */ 
  1468. function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1469. return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy ); 
  1470.  
  1471. /** 
  1472. * Retrieves the next post that is adjacent to the current post. 
  1473. * 
  1474. * @since 1.5.0 
  1475. * 
  1476. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false. 
  1477. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1478. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1479. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no 
  1480. * corresponding post exists. 
  1481. */ 
  1482. function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1483. return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy ); 
  1484.  
  1485. /** 
  1486. * Retrieves the adjacent post. 
  1487. * 
  1488. * Can either be next or previous post. 
  1489. * 
  1490. * @since 2.5.0 
  1491. * 
  1492. * @global wpdb $wpdb WordPress database abstraction object. 
  1493. * 
  1494. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false. 
  1495. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1496. * @param bool $previous Optional. Whether to retrieve previous post. Default true 
  1497. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1498. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no 
  1499. * corresponding post exists. 
  1500. */ 
  1501. function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
  1502. global $wpdb; 
  1503.  
  1504. if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) ) 
  1505. return null; 
  1506.  
  1507. $current_post_date = $post->post_date; 
  1508.  
  1509. $join = ''; 
  1510. $where = ''; 
  1511. $adjacent = $previous ? 'previous' : 'next'; 
  1512.  
  1513. if ( $in_same_term || ! empty( $excluded_terms ) ) { 
  1514. if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) { 
  1515. // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and " 
  1516. if ( false !== strpos( $excluded_terms, ' and ' ) ) { 
  1517. _deprecated_argument( __FUNCTION__, '3.3.0', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) ); 
  1518. $excluded_terms = explode( ' and ', $excluded_terms ); 
  1519. } else { 
  1520. $excluded_terms = explode( ', ', $excluded_terms ); 
  1521.  
  1522. $excluded_terms = array_map( 'intval', $excluded_terms ); 
  1523.  
  1524. if ( $in_same_term ) { 
  1525. $join .= " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id"; 
  1526. $where .= $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); 
  1527.  
  1528. if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) 
  1529. return ''; 
  1530. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 
  1531.  
  1532. // Remove any exclusions from the term array to include. 
  1533. $term_array = array_diff( $term_array, (array) $excluded_terms ); 
  1534. $term_array = array_map( 'intval', $term_array ); 
  1535.  
  1536. if ( ! $term_array || is_wp_error( $term_array ) ) 
  1537. return ''; 
  1538.  
  1539. $where .= " AND tt.term_id IN (" . implode( ', ', $term_array ) . ")"; 
  1540.  
  1541. /** 
  1542. * Filters the IDs of terms excluded from adjacent post queries. 
  1543. * 
  1544. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1545. * of adjacency, 'next' or 'previous'. 
  1546. * 
  1547. * @since 4.4.0 
  1548. * 
  1549. * @param string $excluded_terms Array of excluded term IDs. 
  1550. */ 
  1551. $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms ); 
  1552.  
  1553. if ( ! empty( $excluded_terms ) ) { 
  1554. $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( ', ', array_map( 'intval', $excluded_terms ) ) . ') )'; 
  1555.  
  1556. // 'post_status' clause depends on the current user. 
  1557. if ( is_user_logged_in() ) { 
  1558. $user_id = get_current_user_id(); 
  1559.  
  1560. $post_type_object = get_post_type_object( $post->post_type ); 
  1561. if ( empty( $post_type_object ) ) { 
  1562. $post_type_cap = $post->post_type; 
  1563. $read_private_cap = 'read_private_' . $post_type_cap . 's'; 
  1564. } else { 
  1565. $read_private_cap = $post_type_object->cap->read_private_posts; 
  1566.  
  1567. /** 
  1568. * Results should include private posts belonging to the current user, or private posts where the 
  1569. * current user has the 'read_private_posts' cap. 
  1570. */ 
  1571. $private_states = get_post_stati( array( 'private' => true ) ); 
  1572. $where .= " AND ( p.post_status = 'publish'"; 
  1573. foreach ( (array) $private_states as $state ) { 
  1574. if ( current_user_can( $read_private_cap ) ) { 
  1575. $where .= $wpdb->prepare( " OR p.post_status = %s", $state ); 
  1576. } else { 
  1577. $where .= $wpdb->prepare( " OR (p.post_author = %d AND p.post_status = %s)", $user_id, $state ); 
  1578. $where .= " )"; 
  1579. } else { 
  1580. $where .= " AND p.post_status = 'publish'"; 
  1581.  
  1582. $op = $previous ? '<' : '>'; 
  1583. $order = $previous ? 'DESC' : 'ASC'; 
  1584.  
  1585. /** 
  1586. * Filters the JOIN clause in the SQL for an adjacent post query. 
  1587. * 
  1588. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1589. * of adjacency, 'next' or 'previous'. 
  1590. * 
  1591. * @since 2.5.0 
  1592. * @since 4.4.0 Added the `$taxonomy` and `$post` parameters. 
  1593. * 
  1594. * @param string $join The JOIN clause in the SQL. 
  1595. * @param bool $in_same_term Whether post should be in a same taxonomy term. 
  1596. * @param array $excluded_terms Array of excluded term IDs. 
  1597. * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. 
  1598. * @param WP_Post $post WP_Post object. 
  1599. */ 
  1600. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post ); 
  1601.  
  1602. /** 
  1603. * Filters the WHERE clause in the SQL for an adjacent post query. 
  1604. * 
  1605. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1606. * of adjacency, 'next' or 'previous'. 
  1607. * 
  1608. * @since 2.5.0 
  1609. * @since 4.4.0 Added the `$taxonomy` and `$post` parameters. 
  1610. * 
  1611. * @param string $where The `WHERE` clause in the SQL. 
  1612. * @param bool $in_same_term Whether post should be in a same taxonomy term. 
  1613. * @param array $excluded_terms Array of excluded term IDs. 
  1614. * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. 
  1615. * @param WP_Post $post WP_Post object. 
  1616. */ 
  1617. $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms, $taxonomy, $post ); 
  1618.  
  1619. /** 
  1620. * Filters the ORDER BY clause in the SQL for an adjacent post query. 
  1621. * 
  1622. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1623. * of adjacency, 'next' or 'previous'. 
  1624. * 
  1625. * @since 2.5.0 
  1626. * @since 4.4.0 Added the `$post` parameter. 
  1627. * 
  1628. * @param string $order_by The `ORDER BY` clause in the SQL. 
  1629. * @param WP_Post $post WP_Post object. 
  1630. */ 
  1631. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post ); 
  1632.  
  1633. $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort"; 
  1634. $query_key = 'adjacent_post_' . md5( $query ); 
  1635. $result = wp_cache_get( $query_key, 'counts' ); 
  1636. if ( false !== $result ) { 
  1637. if ( $result ) 
  1638. $result = get_post( $result ); 
  1639. return $result; 
  1640.  
  1641. $result = $wpdb->get_var( $query ); 
  1642. if ( null === $result ) 
  1643. $result = ''; 
  1644.  
  1645. wp_cache_set( $query_key, $result, 'counts' ); 
  1646.  
  1647. if ( $result ) 
  1648. $result = get_post( $result ); 
  1649.  
  1650. return $result; 
  1651.  
  1652. /** 
  1653. * Retrieves the adjacent post relational link. 
  1654. * 
  1655. * Can either be next or previous post relational link. 
  1656. * 
  1657. * @since 2.8.0 
  1658. * 
  1659. * @param string $title Optional. Link title format. Default '%title'. 
  1660. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1661. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1662. * @param bool $previous Optional. Whether to display link to previous or next post. Default true. 
  1663. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1664. * @return string|void The adjacent post relational link URL. 
  1665. */ 
  1666. function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
  1667. if ( $previous && is_attachment() && $post = get_post() ) 
  1668. $post = get_post( $post->post_parent ); 
  1669. else 
  1670. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
  1671.  
  1672. if ( empty( $post ) ) 
  1673. return; 
  1674.  
  1675. $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) ); 
  1676.  
  1677. if ( empty( $post_title ) ) 
  1678. $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); 
  1679.  
  1680. $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 
  1681.  
  1682. $title = str_replace( '%title', $post_title, $title ); 
  1683. $title = str_replace( '%date', $date, $title ); 
  1684.  
  1685. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; 
  1686. $link .= esc_attr( $title ); 
  1687. $link .= "' href='" . get_permalink( $post ) . "' />\n"; 
  1688.  
  1689. $adjacent = $previous ? 'previous' : 'next'; 
  1690.  
  1691. /** 
  1692. * Filters the adjacent post relational link. 
  1693. * 
  1694. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1695. * of adjacency, 'next' or 'previous'. 
  1696. * 
  1697. * @since 2.8.0 
  1698. * 
  1699. * @param string $link The relational link. 
  1700. */ 
  1701. return apply_filters( "{$adjacent}_post_rel_link", $link ); 
  1702.  
  1703. /** 
  1704. * Displays the relational links for the posts adjacent to the current post. 
  1705. * 
  1706. * @since 2.8.0 
  1707. * 
  1708. * @param string $title Optional. Link title format. Default '%title'. 
  1709. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1710. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1711. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1712. */ 
  1713. function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1714. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy ); 
  1715. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy ); 
  1716.  
  1717. /** 
  1718. * Displays relational links for the posts adjacent to the current post for single post pages. 
  1719. * 
  1720. * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins 
  1721. * or theme templates. 
  1722. * 
  1723. * @since 3.0.0 
  1724. * 
  1725. * @see adjacent_posts_rel_link() 
  1726. */ 
  1727. function adjacent_posts_rel_link_wp_head() { 
  1728. if ( ! is_single() || is_attachment() ) { 
  1729. return; 
  1730. adjacent_posts_rel_link(); 
  1731.  
  1732. /** 
  1733. * Displays the relational link for the next post adjacent to the current post. 
  1734. * 
  1735. * @since 2.8.0 
  1736. * 
  1737. * @see get_adjacent_post_rel_link() 
  1738. * 
  1739. * @param string $title Optional. Link title format. Default '%title'. 
  1740. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1741. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1742. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1743. */ 
  1744. function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1745. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy ); 
  1746.  
  1747. /** 
  1748. * Displays the relational link for the previous post adjacent to the current post. 
  1749. * 
  1750. * @since 2.8.0 
  1751. * 
  1752. * @see get_adjacent_post_rel_link() 
  1753. * 
  1754. * @param string $title Optional. Link title format. Default '%title'. 
  1755. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1756. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true. 
  1757. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1758. */ 
  1759. function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1760. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy ); 
  1761.  
  1762. /** 
  1763. * Retrieves the boundary post. 
  1764. * 
  1765. * Boundary being either the first or last post by publish date within the constraints specified 
  1766. * by $in_same_term or $excluded_terms. 
  1767. * 
  1768. * @since 2.8.0 
  1769. * 
  1770. * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term. 
  1771. * Default false. 
  1772. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
  1773. * Default empty. 
  1774. * @param bool $start Optional. Whether to retrieve first or last post. Default true 
  1775. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1776. * @return null|array Array containing the boundary post object if successful, null otherwise. 
  1777. */ 
  1778. function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) { 
  1779. $post = get_post(); 
  1780. if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) 
  1781. return null; 
  1782.  
  1783. $query_args = array( 
  1784. 'posts_per_page' => 1,  
  1785. 'order' => $start ? 'ASC' : 'DESC',  
  1786. 'update_post_term_cache' => false,  
  1787. 'update_post_meta_cache' => false 
  1788. ); 
  1789.  
  1790. $term_array = array(); 
  1791.  
  1792. if ( ! is_array( $excluded_terms ) ) { 
  1793. if ( ! empty( $excluded_terms ) ) 
  1794. $excluded_terms = explode( ', ', $excluded_terms ); 
  1795. else 
  1796. $excluded_terms = array(); 
  1797.  
  1798. if ( $in_same_term || ! empty( $excluded_terms ) ) { 
  1799. if ( $in_same_term ) 
  1800. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 
  1801.  
  1802. if ( ! empty( $excluded_terms ) ) { 
  1803. $excluded_terms = array_map( 'intval', $excluded_terms ); 
  1804. $excluded_terms = array_diff( $excluded_terms, $term_array ); 
  1805.  
  1806. $inverse_terms = array(); 
  1807. foreach ( $excluded_terms as $excluded_term ) 
  1808. $inverse_terms[] = $excluded_term * -1; 
  1809. $excluded_terms = $inverse_terms; 
  1810.  
  1811. $query_args[ 'tax_query' ] = array( array( 
  1812. 'taxonomy' => $taxonomy,  
  1813. 'terms' => array_merge( $term_array, $excluded_terms ) 
  1814. ) ); 
  1815.  
  1816. return get_posts( $query_args ); 
  1817.  
  1818. /** 
  1819. * Retrieves the previous post link that is adjacent to the current post. 
  1820. * 
  1821. * @since 3.7.0 
  1822. * 
  1823. * @param string $format Optional. Link anchor format. Default '« %link'. 
  1824. * @param string $link Optional. Link permalink format. Default '%title%'. 
  1825. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1826. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1827. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1828. * @return string The link URL of the previous post in relation to the current post. 
  1829. */ 
  1830. function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1831. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy ); 
  1832.  
  1833. /** 
  1834. * Displays the previous post link that is adjacent to the current post. 
  1835. * 
  1836. * @since 1.5.0 
  1837. * 
  1838. * @see get_previous_post_link() 
  1839. * 
  1840. * @param string $format Optional. Link anchor format. Default '« %link'. 
  1841. * @param string $link Optional. Link permalink format. Default '%title'. 
  1842. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1843. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1844. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1845. */ 
  1846. function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1847. echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy ); 
  1848.  
  1849. /** 
  1850. * Retrieves the next post link that is adjacent to the current post. 
  1851. * 
  1852. * @since 3.7.0 
  1853. * 
  1854. * @param string $format Optional. Link anchor format. Default '« %link'. 
  1855. * @param string $link Optional. Link permalink format. Default '%title'. 
  1856. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1857. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1858. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1859. * @return string The link URL of the next post in relation to the current post. 
  1860. */ 
  1861. function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1862. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy ); 
  1863.  
  1864. /** 
  1865. * Displays the next post link that is adjacent to the current post. 
  1866. * 
  1867. * @since 1.5.0 
  1868. * @see get_next_post_link() 
  1869. * 
  1870. * @param string $format Optional. Link anchor format. Default '« %link'. 
  1871. * @param string $link Optional. Link permalink format. Default '%title' 
  1872. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1873. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1874. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1875. */ 
  1876. function next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1877. echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy ); 
  1878.  
  1879. /** 
  1880. * Retrieves the adjacent post link. 
  1881. * 
  1882. * Can be either next post link or previous. 
  1883. * 
  1884. * @since 3.7.0 
  1885. * 
  1886. * @param string $format Link anchor format. 
  1887. * @param string $link Link permalink format. 
  1888. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1889. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs. Default empty. 
  1890. * @param bool $previous Optional. Whether to display link to previous or next post. Default true. 
  1891. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1892. * @return string The link URL of the previous or next post in relation to the current post. 
  1893. */ 
  1894. function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
  1895. if ( $previous && is_attachment() ) 
  1896. $post = get_post( get_post()->post_parent ); 
  1897. else 
  1898. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
  1899.  
  1900. if ( ! $post ) { 
  1901. $output = ''; 
  1902. } else { 
  1903. $title = $post->post_title; 
  1904.  
  1905. if ( empty( $post->post_title ) ) 
  1906. $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); 
  1907.  
  1908. /** This filter is documented in wp-includes/post-template.php */ 
  1909. $title = apply_filters( 'the_title', $title, $post->ID ); 
  1910.  
  1911. $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 
  1912. $rel = $previous ? 'prev' : 'next'; 
  1913.  
  1914. $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">'; 
  1915. $inlink = str_replace( '%title', $title, $link ); 
  1916. $inlink = str_replace( '%date', $date, $inlink ); 
  1917. $inlink = $string . $inlink . '</a>'; 
  1918.  
  1919. $output = str_replace( '%link', $inlink, $format ); 
  1920.  
  1921. $adjacent = $previous ? 'previous' : 'next'; 
  1922.  
  1923. /** 
  1924. * Filters the adjacent post link. 
  1925. * 
  1926. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1927. * of adjacency, 'next' or 'previous'. 
  1928. * 
  1929. * @since 2.6.0 
  1930. * @since 4.2.0 Added the `$adjacent` parameter. 
  1931. * 
  1932. * @param string $output The adjacent post link. 
  1933. * @param string $format Link anchor format. 
  1934. * @param string $link Link permalink format. 
  1935. * @param WP_Post $post The adjacent post. 
  1936. * @param string $adjacent Whether the post is previous or next. 
  1937. */ 
  1938. return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent ); 
  1939.  
  1940. /** 
  1941. * Displays the adjacent post link. 
  1942. * 
  1943. * Can be either next post link or previous. 
  1944. * 
  1945. * @since 2.5.0 
  1946. * 
  1947. * @param string $format Link anchor format. 
  1948. * @param string $link Link permalink format. 
  1949. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1950. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs. Default empty. 
  1951. * @param bool $previous Optional. Whether to display link to previous or next post. Default true. 
  1952. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1953. */ 
  1954. function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
  1955. echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy ); 
  1956.  
  1957. /** 
  1958. * Retrieves the link for a page number. 
  1959. * 
  1960. * @since 1.5.0 
  1961. * 
  1962. * @global WP_Rewrite $wp_rewrite 
  1963. * 
  1964. * @param int $pagenum Optional. Page ID. Default 1. 
  1965. * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true. 
  1966. * Otherwise, prepares the URL with esc_url_raw(). 
  1967. * @return string The link URL for the given page number. 
  1968. */ 
  1969. function get_pagenum_link($pagenum = 1, $escape = true ) { 
  1970. global $wp_rewrite; 
  1971.  
  1972. $pagenum = (int) $pagenum; 
  1973.  
  1974. $request = remove_query_arg( 'paged' ); 
  1975.  
  1976. $home_root = parse_url(home_url()); 
  1977. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : ''; 
  1978. $home_root = preg_quote( $home_root, '|' ); 
  1979.  
  1980. $request = preg_replace('|^'. $home_root . '|i', '', $request); 
  1981. $request = preg_replace('|^/+|', '', $request); 
  1982.  
  1983. if ( !$wp_rewrite->using_permalinks() || is_admin() ) { 
  1984. $base = trailingslashit( get_bloginfo( 'url' ) ); 
  1985.  
  1986. if ( $pagenum > 1 ) { 
  1987. $result = add_query_arg( 'paged', $pagenum, $base . $request ); 
  1988. } else { 
  1989. $result = $base . $request; 
  1990. } else { 
  1991. $qs_regex = '|\?.*?$|'; 
  1992. preg_match( $qs_regex, $request, $qs_match ); 
  1993.  
  1994. if ( !empty( $qs_match[0] ) ) { 
  1995. $query_string = $qs_match[0]; 
  1996. $request = preg_replace( $qs_regex, '', $request ); 
  1997. } else { 
  1998. $query_string = ''; 
  1999.  
  2000. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request); 
  2001. $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request); 
  2002. $request = ltrim($request, '/'); 
  2003.  
  2004. $base = trailingslashit( get_bloginfo( 'url' ) ); 
  2005.  
  2006. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) ) 
  2007. $base .= $wp_rewrite->index . '/'; 
  2008.  
  2009. if ( $pagenum > 1 ) { 
  2010. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' ); 
  2011.  
  2012. $result = $base . $request . $query_string; 
  2013.  
  2014. /** 
  2015. * Filters the page number link for the current request. 
  2016. * 
  2017. * @since 2.5.0 
  2018. * 
  2019. * @param string $result The page number link. 
  2020. */ 
  2021. $result = apply_filters( 'get_pagenum_link', $result ); 
  2022.  
  2023. if ( $escape ) 
  2024. return esc_url( $result ); 
  2025. else 
  2026. return esc_url_raw( $result ); 
  2027.  
  2028. /** 
  2029. * Retrieves the next posts page link. 
  2030. * 
  2031. * Backported from 2.1.3 to 2.0.10. 
  2032. * 
  2033. * @since 2.0.10 
  2034. * 
  2035. * @global int $paged 
  2036. * 
  2037. * @param int $max_page Optional. Max pages. Default 0. 
  2038. * @return string|void The link URL for next posts page. 
  2039. */ 
  2040. function get_next_posts_page_link($max_page = 0) { 
  2041. global $paged; 
  2042.  
  2043. if ( !is_single() ) { 
  2044. if ( !$paged ) 
  2045. $paged = 1; 
  2046. $nextpage = intval($paged) + 1; 
  2047. if ( !$max_page || $max_page >= $nextpage ) 
  2048. return get_pagenum_link($nextpage); 
  2049.  
  2050. /** 
  2051. * Displays or retrieves the next posts page link. 
  2052. * 
  2053. * @since 0.71 
  2054. * 
  2055. * @param int $max_page Optional. Max pages. Default 0. 
  2056. * @param bool $echo Optional. Whether to echo the link. Default true. 
  2057. * @return string|void The link URL for next posts page if `$echo = false`. 
  2058. */ 
  2059. function next_posts( $max_page = 0, $echo = true ) { 
  2060. $output = esc_url( get_next_posts_page_link( $max_page ) ); 
  2061.  
  2062. if ( $echo ) 
  2063. echo $output; 
  2064. else 
  2065. return $output; 
  2066.  
  2067. /** 
  2068. * Retrieves the next posts page link. 
  2069. * 
  2070. * @since 2.7.0 
  2071. * 
  2072. * @global int $paged 
  2073. * @global WP_Query $wp_query 
  2074. * 
  2075. * @param string $label Content for link text. 
  2076. * @param int $max_page Optional. Max pages. Default 0. 
  2077. * @return string|void HTML-formatted next posts page link. 
  2078. */ 
  2079. function get_next_posts_link( $label = null, $max_page = 0 ) { 
  2080. global $paged, $wp_query; 
  2081.  
  2082. if ( !$max_page ) 
  2083. $max_page = $wp_query->max_num_pages; 
  2084.  
  2085. if ( !$paged ) 
  2086. $paged = 1; 
  2087.  
  2088. $nextpage = intval($paged) + 1; 
  2089.  
  2090. if ( null === $label ) 
  2091. $label = __( 'Next Page »' ); 
  2092.  
  2093. if ( !is_single() && ( $nextpage <= $max_page ) ) { 
  2094. /** 
  2095. * Filters the anchor tag attributes for the next posts page link. 
  2096. * 
  2097. * @since 2.7.0 
  2098. * 
  2099. * @param string $attributes Attributes for the anchor tag. 
  2100. */ 
  2101. $attr = apply_filters( 'next_posts_link_attributes', '' ); 
  2102.  
  2103. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1, 8};)/i', '&$1', $label) . '</a>'; 
  2104.  
  2105. /** 
  2106. * Displays the next posts page link. 
  2107. * 
  2108. * @since 0.71 
  2109. * 
  2110. * @param string $label Content for link text. 
  2111. * @param int $max_page Optional. Max pages. Default 0. 
  2112. */ 
  2113. function next_posts_link( $label = null, $max_page = 0 ) { 
  2114. echo get_next_posts_link( $label, $max_page ); 
  2115.  
  2116. /** 
  2117. * Retrieves the previous posts page link. 
  2118. * 
  2119. * Will only return string, if not on a single page or post. 
  2120. * 
  2121. * Backported to 2.0.10 from 2.1.3. 
  2122. * 
  2123. * @since 2.0.10 
  2124. * 
  2125. * @global int $paged 
  2126. * 
  2127. * @return string|void The link for the previous posts page. 
  2128. */ 
  2129. function get_previous_posts_page_link() { 
  2130. global $paged; 
  2131.  
  2132. if ( !is_single() ) { 
  2133. $nextpage = intval($paged) - 1; 
  2134. if ( $nextpage < 1 ) 
  2135. $nextpage = 1; 
  2136. return get_pagenum_link($nextpage); 
  2137.  
  2138. /** 
  2139. * Displays or retrieves the previous posts page link. 
  2140. * 
  2141. * @since 0.71 
  2142. * 
  2143. * @param bool $echo Optional. Whether to echo the link. Default true. 
  2144. * @return string|void The previous posts page link if `$echo = false`. 
  2145. */ 
  2146. function previous_posts( $echo = true ) { 
  2147. $output = esc_url( get_previous_posts_page_link() ); 
  2148.  
  2149. if ( $echo ) 
  2150. echo $output; 
  2151. else 
  2152. return $output; 
  2153.  
  2154. /** 
  2155. * Retrieves the previous posts page link. 
  2156. * 
  2157. * @since 2.7.0 
  2158. * 
  2159. * @global int $paged 
  2160. * 
  2161. * @param string $label Optional. Previous page link text. 
  2162. * @return string|void HTML-formatted previous page link. 
  2163. */ 
  2164. function get_previous_posts_link( $label = null ) { 
  2165. global $paged; 
  2166.  
  2167. if ( null === $label ) 
  2168. $label = __( '« Previous Page' ); 
  2169.  
  2170. if ( !is_single() && $paged > 1 ) { 
  2171. /** 
  2172. * Filters the anchor tag attributes for the previous posts page link. 
  2173. * 
  2174. * @since 2.7.0 
  2175. * 
  2176. * @param string $attributes Attributes for the anchor tag. 
  2177. */ 
  2178. $attr = apply_filters( 'previous_posts_link_attributes', '' ); 
  2179. return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1, 8};)/i', '&$1', $label ) .'</a>'; 
  2180.  
  2181. /** 
  2182. * Displays the previous posts page link. 
  2183. * 
  2184. * @since 0.71 
  2185. * 
  2186. * @param string $label Optional. Previous page link text. 
  2187. */ 
  2188. function previous_posts_link( $label = null ) { 
  2189. echo get_previous_posts_link( $label ); 
  2190.  
  2191. /** 
  2192. * Retrieves the post pages link navigation for previous and next pages. 
  2193. * 
  2194. * @since 2.8.0 
  2195. * 
  2196. * @global WP_Query $wp_query 
  2197. * 
  2198. * @param string|array $args { 
  2199. * Optional. Arguments to build the post pages link navigation. 
  2200. * 
  2201. * @type string $sep Separator character. Default '—'. 
  2202. * @type string $prelabel Link text to display for the previous page link. 
  2203. * Default '« Previous Page'. 
  2204. * @type string $nxtlabel Link text to display for the next page link. 
  2205. * Default 'Next Page »'. 
  2206. * } 
  2207. * @return string The posts link navigation. 
  2208. */ 
  2209. function get_posts_nav_link( $args = array() ) { 
  2210. global $wp_query; 
  2211.  
  2212. $return = ''; 
  2213.  
  2214. if ( !is_singular() ) { 
  2215. $defaults = array( 
  2216. 'sep' => ' — ',  
  2217. 'prelabel' => __('« Previous Page'),  
  2218. 'nxtlabel' => __('Next Page »'),  
  2219. ); 
  2220. $args = wp_parse_args( $args, $defaults ); 
  2221.  
  2222. $max_num_pages = $wp_query->max_num_pages; 
  2223. $paged = get_query_var('paged'); 
  2224.  
  2225. //only have sep if there's both prev and next results 
  2226. if ($paged < 2 || $paged >= $max_num_pages) { 
  2227. $args['sep'] = ''; 
  2228.  
  2229. if ( $max_num_pages > 1 ) { 
  2230. $return = get_previous_posts_link($args['prelabel']); 
  2231. $return .= preg_replace('/&([^#])(?![a-z]{1, 8};)/i', '&$1', $args['sep']); 
  2232. $return .= get_next_posts_link($args['nxtlabel']); 
  2233. return $return; 
  2234.  
  2235.  
  2236. /** 
  2237. * Displays the post pages link navigation for previous and next pages. 
  2238. * 
  2239. * @since 0.71 
  2240. * 
  2241. * @param string $sep Optional. Separator for posts navigation links. Default empty. 
  2242. * @param string $prelabel Optional. Label for previous pages. Default empty. 
  2243. * @param string $nxtlabel Optional Label for next pages. Default empty. 
  2244. */ 
  2245. function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) { 
  2246. $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') ); 
  2247. echo get_posts_nav_link($args); 
  2248.  
  2249. /** 
  2250. * Retrieves the navigation to next/previous post, when applicable. 
  2251. * 
  2252. * @since 4.1.0 
  2253. * @since 4.4.0 Introduced the `in_same_term`, `excluded_terms`, and `taxonomy` arguments. 
  2254. * 
  2255. * @param array $args { 
  2256. * Optional. Default post navigation arguments. Default empty array. 
  2257. * 
  2258. * @type string $prev_text Anchor text to display in the previous post link. Default '%title'. 
  2259. * @type string $next_text Anchor text to display in the next post link. Default '%title'. 
  2260. * @type bool $in_same_term Whether link should be in a same taxonomy term. Default false. 
  2261. * @type array|string $excluded_terms Array or comma-separated list of excluded term IDs. Default empty. 
  2262. * @type string $taxonomy Taxonomy, if `$in_same_term` is true. Default 'category'. 
  2263. * @type string $screen_reader_text Screen reader text for nav element. Default 'Post navigation'. 
  2264. * } 
  2265. * @return string Markup for post links. 
  2266. */ 
  2267. function get_the_post_navigation( $args = array() ) { 
  2268. $args = wp_parse_args( $args, array( 
  2269. 'prev_text' => '%title',  
  2270. 'next_text' => '%title',  
  2271. 'in_same_term' => false,  
  2272. 'excluded_terms' => '',  
  2273. 'taxonomy' => 'category',  
  2274. 'screen_reader_text' => __( 'Post navigation' ),  
  2275. ) ); 
  2276.  
  2277. $navigation = ''; 
  2278.  
  2279. $previous = get_previous_post_link( 
  2280. '<div class="nav-previous">%link</div>',  
  2281. $args['prev_text'],  
  2282. $args['in_same_term'],  
  2283. $args['excluded_terms'],  
  2284. $args['taxonomy'] 
  2285. ); 
  2286.  
  2287. $next = get_next_post_link( 
  2288. '<div class="nav-next">%link</div>',  
  2289. $args['next_text'],  
  2290. $args['in_same_term'],  
  2291. $args['excluded_terms'],  
  2292. $args['taxonomy'] 
  2293. ); 
  2294.  
  2295. // Only add markup if there's somewhere to navigate to. 
  2296. if ( $previous || $next ) { 
  2297. $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] ); 
  2298.  
  2299. return $navigation; 
  2300.  
  2301. /** 
  2302. * Displays the navigation to next/previous post, when applicable. 
  2303. * 
  2304. * @since 4.1.0 
  2305. * 
  2306. * @param array $args Optional. See get_the_post_navigation() for available arguments. 
  2307. * Default empty array. 
  2308. */ 
  2309. function the_post_navigation( $args = array() ) { 
  2310. echo get_the_post_navigation( $args ); 
  2311.  
  2312. /** 
  2313. * Returns the navigation to next/previous set of posts, when applicable. 
  2314. * 
  2315. * @since 4.1.0 
  2316. * 
  2317. * @global WP_Query $wp_query WordPress Query object. 
  2318. * 
  2319. * @param array $args { 
  2320. * Optional. Default posts navigation arguments. Default empty array. 
  2321. * 
  2322. * @type string $prev_text Anchor text to display in the previous posts link. 
  2323. * Default 'Older posts'. 
  2324. * @type string $next_text Anchor text to display in the next posts link. 
  2325. * Default 'Newer posts'. 
  2326. * @type string $screen_reader_text Screen reader text for nav element. 
  2327. * Default 'Posts navigation'. 
  2328. * } 
  2329. * @return string Markup for posts links. 
  2330. */ 
  2331. function get_the_posts_navigation( $args = array() ) { 
  2332. $navigation = ''; 
  2333.  
  2334. // Don't print empty markup if there's only one page. 
  2335. if ( $GLOBALS['wp_query']->max_num_pages > 1 ) { 
  2336. $args = wp_parse_args( $args, array( 
  2337. 'prev_text' => __( 'Older posts' ),  
  2338. 'next_text' => __( 'Newer posts' ),  
  2339. 'screen_reader_text' => __( 'Posts navigation' ),  
  2340. ) ); 
  2341.  
  2342. $next_link = get_previous_posts_link( $args['next_text'] ); 
  2343. $prev_link = get_next_posts_link( $args['prev_text'] ); 
  2344.  
  2345. if ( $prev_link ) { 
  2346. $navigation .= '<div class="nav-previous">' . $prev_link . '</div>'; 
  2347.  
  2348. if ( $next_link ) { 
  2349. $navigation .= '<div class="nav-next">' . $next_link . '</div>'; 
  2350.  
  2351. $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] ); 
  2352.  
  2353. return $navigation; 
  2354.  
  2355. /** 
  2356. * Displays the navigation to next/previous set of posts, when applicable. 
  2357. * 
  2358. * @since 4.1.0 
  2359. * 
  2360. * @param array $args Optional. See get_the_posts_navigation() for available arguments. 
  2361. * Default empty array. 
  2362. */ 
  2363. function the_posts_navigation( $args = array() ) { 
  2364. echo get_the_posts_navigation( $args ); 
  2365.  
  2366. /** 
  2367. * Retrieves a paginated navigation to next/previous set of posts, when applicable. 
  2368. * 
  2369. * @since 4.1.0 
  2370. * 
  2371. * @param array $args { 
  2372. * Optional. Default pagination arguments, see paginate_links(). 
  2373. * 
  2374. * @type string $screen_reader_text Screen reader text for navigation element. 
  2375. * Default 'Posts navigation'. 
  2376. * } 
  2377. * @return string Markup for pagination links. 
  2378. */ 
  2379. function get_the_posts_pagination( $args = array() ) { 
  2380. $navigation = ''; 
  2381.  
  2382. // Don't print empty markup if there's only one page. 
  2383. if ( $GLOBALS['wp_query']->max_num_pages > 1 ) { 
  2384. $args = wp_parse_args( $args, array( 
  2385. 'mid_size' => 1,  
  2386. 'prev_text' => _x( 'Previous', 'previous set of posts' ),  
  2387. 'next_text' => _x( 'Next', 'next set of posts' ),  
  2388. 'screen_reader_text' => __( 'Posts navigation' ),  
  2389. ) ); 
  2390.  
  2391. // Make sure we get a string back. Plain is the next best thing. 
  2392. if ( isset( $args['type'] ) && 'array' == $args['type'] ) { 
  2393. $args['type'] = 'plain'; 
  2394.  
  2395. // Set up paginated links. 
  2396. $links = paginate_links( $args ); 
  2397.  
  2398. if ( $links ) { 
  2399. $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] ); 
  2400.  
  2401. return $navigation; 
  2402.  
  2403. /** 
  2404. * Displays a paginated navigation to next/previous set of posts, when applicable. 
  2405. * 
  2406. * @since 4.1.0 
  2407. * 
  2408. * @param array $args Optional. See get_the_posts_pagination() for available arguments. 
  2409. * Default empty array. 
  2410. */ 
  2411. function the_posts_pagination( $args = array() ) { 
  2412. echo get_the_posts_pagination( $args ); 
  2413.  
  2414. /** 
  2415. * Wraps passed links in navigational markup. 
  2416. * 
  2417. * @since 4.1.0 
  2418. * @access private 
  2419. * 
  2420. * @param string $links Navigational links. 
  2421. * @param string $class Optional. Custom class for nav element. Default: 'posts-navigation'. 
  2422. * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'. 
  2423. * @return string Navigation template tag. 
  2424. */ 
  2425. function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) { 
  2426. if ( empty( $screen_reader_text ) ) { 
  2427. $screen_reader_text = __( 'Posts navigation' ); 
  2428.  
  2429. $template = ' 
  2430. <nav class="navigation %1$s" role="navigation"> 
  2431. <h2 class="screen-reader-text">%2$s</h2> 
  2432. <div class="nav-links">%3$s</div> 
  2433. </nav>'; 
  2434.  
  2435. /** 
  2436. * Filters the navigation markup template. 
  2437. * 
  2438. * Note: The filtered template HTML must contain specifiers for the navigation 
  2439. * class (%1$s), the screen-reader-text value (%2$s), and placement of the 
  2440. * navigation links (%3$s): 
  2441. * 
  2442. * <nav class="navigation %1$s" role="navigation"> 
  2443. * <h2 class="screen-reader-text">%2$s</h2> 
  2444. * <div class="nav-links">%3$s</div> 
  2445. * </nav> 
  2446. * 
  2447. * @since 4.4.0 
  2448. * 
  2449. * @param string $template The default template. 
  2450. * @param string $class The class passed by the calling function. 
  2451. * @return string Navigation template. 
  2452. */ 
  2453. $template = apply_filters( 'navigation_markup_template', $template, $class ); 
  2454.  
  2455. return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links ); 
  2456.  
  2457. /** 
  2458. * Retrieves the comments page number link. 
  2459. * 
  2460. * @since 2.7.0 
  2461. * 
  2462. * @global WP_Rewrite $wp_rewrite 
  2463. * 
  2464. * @param int $pagenum Optional. Page number. Default 1. 
  2465. * @param int $max_page Optional. The maximum number of comment pages. Default 0. 
  2466. * @return string The comments page number link URL. 
  2467. */ 
  2468. function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) { 
  2469. global $wp_rewrite; 
  2470.  
  2471. $pagenum = (int) $pagenum; 
  2472.  
  2473. $result = get_permalink(); 
  2474.  
  2475. if ( 'newest' == get_option('default_comments_page') ) { 
  2476. if ( $pagenum != $max_page ) { 
  2477. if ( $wp_rewrite->using_permalinks() ) 
  2478. $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged'); 
  2479. else 
  2480. $result = add_query_arg( 'cpage', $pagenum, $result ); 
  2481. } elseif ( $pagenum > 1 ) { 
  2482. if ( $wp_rewrite->using_permalinks() ) 
  2483. $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged'); 
  2484. else 
  2485. $result = add_query_arg( 'cpage', $pagenum, $result ); 
  2486.  
  2487. $result .= '#comments'; 
  2488.  
  2489. /** 
  2490. * Filters the comments page number link for the current request. 
  2491. * 
  2492. * @since 2.7.0 
  2493. * 
  2494. * @param string $result The comments page number link. 
  2495. */ 
  2496. return apply_filters( 'get_comments_pagenum_link', $result ); 
  2497.  
  2498. /** 
  2499. * Retrieves the link to the next comments page. 
  2500. * 
  2501. * @since 2.7.1 
  2502. * 
  2503. * @global WP_Query $wp_query 
  2504. * 
  2505. * @param string $label Optional. Label for link text. Default empty. 
  2506. * @param int $max_page Optional. Max page. Default 0. 
  2507. * @return string|void HTML-formatted link for the next page of comments. 
  2508. */ 
  2509. function get_next_comments_link( $label = '', $max_page = 0 ) { 
  2510. global $wp_query; 
  2511.  
  2512. if ( ! is_singular() ) 
  2513. return; 
  2514.  
  2515. $page = get_query_var('cpage'); 
  2516.  
  2517. if ( ! $page ) { 
  2518. $page = 1; 
  2519.  
  2520. $nextpage = intval($page) + 1; 
  2521.  
  2522. if ( empty($max_page) ) 
  2523. $max_page = $wp_query->max_num_comment_pages; 
  2524.  
  2525. if ( empty($max_page) ) 
  2526. $max_page = get_comment_pages_count(); 
  2527.  
  2528. if ( $nextpage > $max_page ) 
  2529. return; 
  2530.  
  2531. if ( empty($label) ) 
  2532. $label = __('Newer Comments »'); 
  2533.  
  2534. /** 
  2535. * Filters the anchor tag attributes for the next comments page link. 
  2536. * 
  2537. * @since 2.7.0 
  2538. * 
  2539. * @param string $attributes Attributes for the anchor tag. 
  2540. */ 
  2541. return '<a href="' . esc_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>'. preg_replace('/&([^#])(?![a-z]{1, 8};)/i', '&$1', $label) .'</a>'; 
  2542.  
  2543. /** 
  2544. * Displays the link to the next comments page. 
  2545. * 
  2546. * @since 2.7.0 
  2547. * 
  2548. * @param string $label Optional. Label for link text. Default empty. 
  2549. * @param int $max_page Optional. Max page. Default 0. 
  2550. */ 
  2551. function next_comments_link( $label = '', $max_page = 0 ) { 
  2552. echo get_next_comments_link( $label, $max_page ); 
  2553.  
  2554. /** 
  2555. * Retrieves the link to the previous comments page. 
  2556. * 
  2557. * @since 2.7.1 
  2558. * 
  2559. * @param string $label Optional. Label for comments link text. Default empty. 
  2560. * @return string|void HTML-formatted link for the previous page of comments. 
  2561. */ 
  2562. function get_previous_comments_link( $label = '' ) { 
  2563. if ( ! is_singular() ) 
  2564. return; 
  2565.  
  2566. $page = get_query_var('cpage'); 
  2567.  
  2568. if ( intval($page) <= 1 ) 
  2569. return; 
  2570.  
  2571. $prevpage = intval($page) - 1; 
  2572.  
  2573. if ( empty($label) ) 
  2574. $label = __('« Older Comments'); 
  2575.  
  2576. /** 
  2577. * Filters the anchor tag attributes for the previous comments page link. 
  2578. * 
  2579. * @since 2.7.0 
  2580. * 
  2581. * @param string $attributes Attributes for the anchor tag. 
  2582. */ 
  2583. return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1, 8};)/i', '&$1', $label) .'</a>'; 
  2584.  
  2585. /** 
  2586. * Displays the link to the previous comments page. 
  2587. * 
  2588. * @since 2.7.0 
  2589. * 
  2590. * @param string $label Optional. Label for comments link text. Default empty. 
  2591. */ 
  2592. function previous_comments_link( $label = '' ) { 
  2593. echo get_previous_comments_link( $label ); 
  2594.  
  2595. /** 
  2596. * Displays or retrieves pagination links for the comments on the current post. 
  2597. * 
  2598. * @see paginate_links() 
  2599. * @since 2.7.0 
  2600. * 
  2601. * @global WP_Rewrite $wp_rewrite 
  2602. * 
  2603. * @param string|array $args Optional args. See paginate_links(). Default empty array. 
  2604. * @return string|void Markup for pagination links. 
  2605. */ 
  2606. function paginate_comments_links( $args = array() ) { 
  2607. global $wp_rewrite; 
  2608.  
  2609. if ( ! is_singular() ) 
  2610. return; 
  2611.  
  2612. $page = get_query_var('cpage'); 
  2613. if ( !$page ) 
  2614. $page = 1; 
  2615. $max_page = get_comment_pages_count(); 
  2616. $defaults = array( 
  2617. 'base' => add_query_arg( 'cpage', '%#%' ),  
  2618. 'format' => '',  
  2619. 'total' => $max_page,  
  2620. 'current' => $page,  
  2621. 'echo' => true,  
  2622. 'add_fragment' => '#comments' 
  2623. ); 
  2624. if ( $wp_rewrite->using_permalinks() ) 
  2625. $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged'); 
  2626.  
  2627. $args = wp_parse_args( $args, $defaults ); 
  2628. $page_links = paginate_links( $args ); 
  2629.  
  2630. if ( $args['echo'] ) 
  2631. echo $page_links; 
  2632. else 
  2633. return $page_links; 
  2634.  
  2635. /** 
  2636. * Retrieves navigation to next/previous set of comments, when applicable. 
  2637. * 
  2638. * @since 4.4.0 
  2639. * 
  2640. * @param array $args { 
  2641. * Optional. Default comments navigation arguments. 
  2642. * 
  2643. * @type string $prev_text Anchor text to display in the previous comments link. 
  2644. * Default 'Older comments'. 
  2645. * @type string $next_text Anchor text to display in the next comments link. 
  2646. * Default 'Newer comments'. 
  2647. * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'. 
  2648. * } 
  2649. * @return string Markup for comments links. 
  2650. */ 
  2651. function get_the_comments_navigation( $args = array() ) { 
  2652. $navigation = ''; 
  2653.  
  2654. // Are there comments to navigate through? 
  2655. if ( get_comment_pages_count() > 1 ) { 
  2656. $args = wp_parse_args( $args, array( 
  2657. 'prev_text' => __( 'Older comments' ),  
  2658. 'next_text' => __( 'Newer comments' ),  
  2659. 'screen_reader_text' => __( 'Comments navigation' ),  
  2660. ) ); 
  2661.  
  2662. $prev_link = get_previous_comments_link( $args['prev_text'] ); 
  2663. $next_link = get_next_comments_link( $args['next_text'] ); 
  2664.  
  2665. if ( $prev_link ) { 
  2666. $navigation .= '<div class="nav-previous">' . $prev_link . '</div>'; 
  2667.  
  2668. if ( $next_link ) { 
  2669. $navigation .= '<div class="nav-next">' . $next_link . '</div>'; 
  2670.  
  2671. $navigation = _navigation_markup( $navigation, 'comment-navigation', $args['screen_reader_text'] ); 
  2672.  
  2673. return $navigation; 
  2674.  
  2675. /** 
  2676. * Displays navigation to next/previous set of comments, when applicable. 
  2677. * 
  2678. * @since 4.4.0 
  2679. * 
  2680. * @param array $args See get_the_comments_navigation() for available arguments. Default empty array. 
  2681. */ 
  2682. function the_comments_navigation( $args = array() ) { 
  2683. echo get_the_comments_navigation( $args ); 
  2684.  
  2685. /** 
  2686. * Retrieves a paginated navigation to next/previous set of comments, when applicable. 
  2687. * 
  2688. * @since 4.4.0 
  2689. * 
  2690. * @see paginate_comments_links() 
  2691. * 
  2692. * @param array $args { 
  2693. * Optional. Default pagination arguments. 
  2694. * 
  2695. * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'. 
  2696. * } 
  2697. * @return string Markup for pagination links. 
  2698. */ 
  2699. function get_the_comments_pagination( $args = array() ) { 
  2700. $navigation = ''; 
  2701. $args = wp_parse_args( $args, array( 
  2702. 'screen_reader_text' => __( 'Comments navigation' ),  
  2703. ) ); 
  2704. $args['echo'] = false; 
  2705.  
  2706. // Make sure we get plain links, so we get a string we can work with. 
  2707. $args['type'] = 'plain'; 
  2708.  
  2709. $links = paginate_comments_links( $args ); 
  2710.  
  2711. if ( $links ) { 
  2712. $navigation = _navigation_markup( $links, 'comments-pagination', $args['screen_reader_text'] ); 
  2713.  
  2714. return $navigation; 
  2715.  
  2716. /** 
  2717. * Displays a paginated navigation to next/previous set of comments, when applicable. 
  2718. * 
  2719. * @since 4.4.0 
  2720. * 
  2721. * @param array $args See get_the_comments_pagination() for available arguments. Default empty array. 
  2722. */ 
  2723. function the_comments_pagination( $args = array() ) { 
  2724. echo get_the_comments_pagination( $args ); 
  2725.  
  2726. /** 
  2727. * Retrieves the Press This bookmarklet link. 
  2728. * 
  2729. * @since 2.6.0 
  2730. * 
  2731. * @global bool $is_IE Whether the browser matches an Internet Explorer user agent. 
  2732. */ 
  2733. function get_shortcut_link() { 
  2734. global $is_IE; 
  2735.  
  2736. include_once( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' ); 
  2737.  
  2738. $link = ''; 
  2739.  
  2740. if ( $is_IE ) { 
  2741. /** 
  2742. * Return the old/shorter bookmarklet code for MSIE 8 and lower,  
  2743. * since they only support a max length of ~2000 characters for 
  2744. * bookmark[let] URLs, which is way to small for our smarter one. 
  2745. * Do update the version number so users do not get the "upgrade your 
  2746. * bookmarklet" notice when using PT in those browsers. 
  2747. */ 
  2748. $ua = $_SERVER['HTTP_USER_AGENT']; 
  2749.  
  2750. if ( ! empty( $ua ) && preg_match( '/\bMSIE (\d)/', $ua, $matches ) && (int) $matches[1] <= 8 ) { 
  2751. $url = wp_json_encode( admin_url( 'press-this.php' ) ); 
  2752.  
  2753. $link = 'javascript:var d=document, w=window, e=w.getSelection, k=d.getSelection, x=d.selection, ' . 
  2754. 's=(e?e():(k)?k():(x?x.createRange().text:0)), f=' . $url . ', l=d.location, e=encodeURIComponent, ' . 
  2755. 'u=f+"?u="+e(l.href)+"&t="+e(d.title)+"&s="+e(s)+"&v=' . WP_Press_This::VERSION . '";' . 
  2756. 'a=function() {if(!w.open(u, "t", "toolbar=0, resizable=1, scrollbars=1, status=1, width=600, height=700"))l.href=u;};' . 
  2757. 'if(/Firefox/.test(navigator.userAgent))setTimeout(a, 0);else a();void(0)'; 
  2758.  
  2759. if ( empty( $link ) ) { 
  2760. $src = @file_get_contents( ABSPATH . 'wp-admin/js/bookmarklet.min.js' ); 
  2761.  
  2762. if ( $src ) { 
  2763. $url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . WP_Press_This::VERSION ); 
  2764. $link = 'javascript:' . str_replace( 'window.pt_url', $url, $src ); 
  2765.  
  2766. $link = str_replace( array( "\r", "\n", "\t" ), '', $link ); 
  2767.  
  2768. /** 
  2769. * Filters the Press This bookmarklet link. 
  2770. * 
  2771. * @since 2.6.0 
  2772. * 
  2773. * @param string $link The Press This bookmarklet link. 
  2774. */ 
  2775. return apply_filters( 'shortcut_link', $link ); 
  2776.  
  2777. /** 
  2778. * Retrieves the URL for the current site where the front end is accessible. 
  2779. * 
  2780. * Returns the 'home' option with the appropriate protocol. The protocol will be 'https' 
  2781. * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option. 
  2782. * If `$scheme` is 'http' or 'https', is_ssl() is overridden. 
  2783. * 
  2784. * @since 3.0.0 
  2785. * 
  2786. * @param string $path Optional. Path relative to the home URL. Default empty. 
  2787. * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts 
  2788. * 'http', 'https', 'relative', 'rest', or null. Default null. 
  2789. * @return string Home URL link with optional path appended. 
  2790. */ 
  2791. function home_url( $path = '', $scheme = null ) { 
  2792. return get_home_url( null, $path, $scheme ); 
  2793.  
  2794. /** 
  2795. * Retrieves the URL for a given site where the front end is accessible. 
  2796. * 
  2797. * Returns the 'home' option with the appropriate protocol. The protocol will be 'https' 
  2798. * if is_ssl() evaluates to true; otherwise, it will be the same as the 'home' option. 
  2799. * If `$scheme` is 'http' or 'https', is_ssl() is overridden. 
  2800. * 
  2801. * @since 3.0.0 
  2802. * 
  2803. * @global string $pagenow 
  2804. * 
  2805. * @param int $blog_id Optional. Site ID. Default null (current site). 
  2806. * @param string $path Optional. Path relative to the home URL. Default empty. 
  2807. * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts 
  2808. * 'http', 'https', 'relative', 'rest', or null. Default null. 
  2809. * @return string Home URL link with optional path appended. 
  2810. */ 
  2811. function get_home_url( $blog_id = null, $path = '', $scheme = null ) { 
  2812. global $pagenow; 
  2813.  
  2814. $orig_scheme = $scheme; 
  2815.  
  2816. if ( empty( $blog_id ) || !is_multisite() ) { 
  2817. $url = get_option( 'home' ); 
  2818. } else { 
  2819. switch_to_blog( $blog_id ); 
  2820. $url = get_option( 'home' ); 
  2821. restore_current_blog(); 
  2822.  
  2823. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) { 
  2824. if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) 
  2825. $scheme = 'https'; 
  2826. else 
  2827. $scheme = parse_url( $url, PHP_URL_SCHEME ); 
  2828.  
  2829. $url = set_url_scheme( $url, $scheme ); 
  2830.  
  2831. if ( $path && is_string( $path ) ) 
  2832. $url .= '/' . ltrim( $path, '/' ); 
  2833.  
  2834. /** 
  2835. * Filters the home URL. 
  2836. * 
  2837. * @since 3.0.0 
  2838. * 
  2839. * @param string $url The complete home URL including scheme and path. 
  2840. * @param string $path Path relative to the home URL. Blank string if no path is specified. 
  2841. * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',  
  2842. * 'relative', 'rest', or null. 
  2843. * @param int|null $blog_id Site ID, or null for the current site. 
  2844. */ 
  2845. return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id ); 
  2846.  
  2847. /** 
  2848. * Retrieves the URL for the current site where WordPress application files 
  2849. * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible. 
  2850. * 
  2851. * Returns the 'site_url' option with the appropriate protocol, 'https' if 
  2852. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is 
  2853. * overridden. 
  2854. * 
  2855. * @since 3.0.0 
  2856. * 
  2857. * @param string $path Optional. Path relative to the site URL. Default empty. 
  2858. * @param string $scheme Optional. Scheme to give the site URL context. See set_url_scheme(). 
  2859. * @return string Site URL link with optional path appended. 
  2860. */ 
  2861. function site_url( $path = '', $scheme = null ) { 
  2862. return get_site_url( null, $path, $scheme ); 
  2863.  
  2864. /** 
  2865. * Retrieves the URL for a given site where WordPress application files 
  2866. * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible. 
  2867. * 
  2868. * Returns the 'site_url' option with the appropriate protocol, 'https' if 
  2869. * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https',  
  2870. * `is_ssl()` is overridden. 
  2871. * 
  2872. * @since 3.0.0 
  2873. * 
  2874. * @param int $blog_id Optional. Site ID. Default null (current site). 
  2875. * @param string $path Optional. Path relative to the site URL. Default empty. 
  2876. * @param string $scheme Optional. Scheme to give the site URL context. Accepts 
  2877. * 'http', 'https', 'login', 'login_post', 'admin', or 
  2878. * 'relative'. Default null. 
  2879. * @return string Site URL link with optional path appended. 
  2880. */ 
  2881. function get_site_url( $blog_id = null, $path = '', $scheme = null ) { 
  2882. if ( empty( $blog_id ) || !is_multisite() ) { 
  2883. $url = get_option( 'siteurl' ); 
  2884. } else { 
  2885. switch_to_blog( $blog_id ); 
  2886. $url = get_option( 'siteurl' ); 
  2887. restore_current_blog(); 
  2888.  
  2889. $url = set_url_scheme( $url, $scheme ); 
  2890.  
  2891. if ( $path && is_string( $path ) ) 
  2892. $url .= '/' . ltrim( $path, '/' ); 
  2893.  
  2894. /** 
  2895. * Filters the site URL. 
  2896. * 
  2897. * @since 2.7.0 
  2898. * 
  2899. * @param string $url The complete site URL including scheme and path. 
  2900. * @param string $path Path relative to the site URL. Blank string if no path is specified. 
  2901. * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',  
  2902. * 'login_post', 'admin', 'relative' or null. 
  2903. * @param int|null $blog_id Site ID, or null for the current site. 
  2904. */ 
  2905. return apply_filters( 'site_url', $url, $path, $scheme, $blog_id ); 
  2906.  
  2907. /** 
  2908. * Retrieves the URL to the admin area for the current site. 
  2909. * 
  2910. * @since 2.6.0 
  2911. * 
  2912. * @param string $path Optional path relative to the admin URL. 
  2913. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 
  2914. * 'http' or 'https' can be passed to force those schemes. 
  2915. * @return string Admin URL link with optional path appended. 
  2916. */ 
  2917. function admin_url( $path = '', $scheme = 'admin' ) { 
  2918. return get_admin_url( null, $path, $scheme ); 
  2919.  
  2920. /** 
  2921. * Retrieves the URL to the admin area for a given site. 
  2922. * 
  2923. * @since 3.0.0 
  2924. * 
  2925. * @param int $blog_id Optional. Site ID. Default null (current site). 
  2926. * @param string $path Optional. Path relative to the admin URL. Default empty. 
  2927. * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https',  
  2928. * to force those schemes. Default 'admin', which obeys 
  2929. * force_ssl_admin() and is_ssl(). 
  2930. * @return string Admin URL link with optional path appended. 
  2931. */ 
  2932. function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) { 
  2933. $url = get_site_url($blog_id, 'wp-admin/', $scheme); 
  2934.  
  2935. if ( $path && is_string( $path ) ) 
  2936. $url .= ltrim( $path, '/' ); 
  2937.  
  2938. /** 
  2939. * Filters the admin area URL. 
  2940. * 
  2941. * @since 2.8.0 
  2942. * 
  2943. * @param string $url The complete admin area URL including scheme and path. 
  2944. * @param string $path Path relative to the admin area URL. Blank string if no path is specified. 
  2945. * @param int|null $blog_id Site ID, or null for the current site. 
  2946. */ 
  2947. return apply_filters( 'admin_url', $url, $path, $blog_id ); 
  2948.  
  2949. /** 
  2950. * Retrieves the URL to the includes directory. 
  2951. * 
  2952. * @since 2.6.0 
  2953. * 
  2954. * @param string $path Optional. Path relative to the includes URL. Default empty. 
  2955. * @param string $scheme Optional. Scheme to give the includes URL context. Accepts 
  2956. * 'http', 'https', or 'relative'. Default null. 
  2957. * @return string Includes URL link with optional path appended. 
  2958. */ 
  2959. function includes_url( $path = '', $scheme = null ) { 
  2960. $url = site_url( '/' . WPINC . '/', $scheme ); 
  2961.  
  2962. if ( $path && is_string( $path ) ) 
  2963. $url .= ltrim($path, '/'); 
  2964.  
  2965. /** 
  2966. * Filters the URL to the includes directory. 
  2967. * 
  2968. * @since 2.8.0 
  2969. * 
  2970. * @param string $url The complete URL to the includes directory including scheme and path. 
  2971. * @param string $path Path relative to the URL to the wp-includes directory. Blank string 
  2972. * if no path is specified. 
  2973. */ 
  2974. return apply_filters( 'includes_url', $url, $path ); 
  2975.  
  2976. /** 
  2977. * Retrieves the URL to the content directory. 
  2978. * 
  2979. * @since 2.6.0 
  2980. * 
  2981. * @param string $path Optional. Path relative to the content URL. Default empty. 
  2982. * @return string Content URL link with optional path appended. 
  2983. */ 
  2984. function content_url( $path = '' ) { 
  2985. $url = set_url_scheme( WP_CONTENT_URL ); 
  2986.  
  2987. if ( $path && is_string( $path ) ) 
  2988. $url .= '/' . ltrim($path, '/'); 
  2989.  
  2990. /** 
  2991. * Filters the URL to the content directory. 
  2992. * 
  2993. * @since 2.8.0 
  2994. * 
  2995. * @param string $url The complete URL to the content directory including scheme and path. 
  2996. * @param string $path Path relative to the URL to the content directory. Blank string 
  2997. * if no path is specified. 
  2998. */ 
  2999. return apply_filters( 'content_url', $url, $path); 
  3000.  
  3001. /** 
  3002. * Retrieves a URL within the plugins or mu-plugins directory. 
  3003. * 
  3004. * Defaults to the plugins directory URL if no arguments are supplied. 
  3005. * 
  3006. * @since 2.6.0 
  3007. * 
  3008. * @param string $path Optional. Extra path appended to the end of the URL, including 
  3009. * the relative directory if $plugin is supplied. Default empty. 
  3010. * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin. 
  3011. * The URL will be relative to its directory. Default empty. 
  3012. * Typically this is done by passing `__FILE__` as the argument. 
  3013. * @return string Plugins URL link with optional paths appended. 
  3014. */ 
  3015. function plugins_url( $path = '', $plugin = '' ) { 
  3016.  
  3017. $path = wp_normalize_path( $path ); 
  3018. $plugin = wp_normalize_path( $plugin ); 
  3019. $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR ); 
  3020.  
  3021. if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) ) 
  3022. $url = WPMU_PLUGIN_URL; 
  3023. else 
  3024. $url = WP_PLUGIN_URL; 
  3025.  
  3026.  
  3027. $url = set_url_scheme( $url ); 
  3028.  
  3029. if ( !empty($plugin) && is_string($plugin) ) { 
  3030. $folder = dirname(plugin_basename($plugin)); 
  3031. if ( '.' != $folder ) 
  3032. $url .= '/' . ltrim($folder, '/'); 
  3033.  
  3034. if ( $path && is_string( $path ) ) 
  3035. $url .= '/' . ltrim($path, '/'); 
  3036.  
  3037. /** 
  3038. * Filters the URL to the plugins directory. 
  3039. * 
  3040. * @since 2.8.0 
  3041. * 
  3042. * @param string $url The complete URL to the plugins directory including scheme and path. 
  3043. * @param string $path Path relative to the URL to the plugins directory. Blank string 
  3044. * if no path is specified. 
  3045. * @param string $plugin The plugin file path to be relative to. Blank string if no plugin 
  3046. * is specified. 
  3047. */ 
  3048. return apply_filters( 'plugins_url', $url, $path, $plugin ); 
  3049.  
  3050. /** 
  3051. * Retrieves the site URL for the current network. 
  3052. * 
  3053. * Returns the site URL with the appropriate protocol, 'https' if 
  3054. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is 
  3055. * overridden. 
  3056. * 
  3057. * @since 3.0.0 
  3058. * 
  3059. * @see set_url_scheme() 
  3060. * 
  3061. * @param string $path Optional. Path relative to the site URL. Default empty. 
  3062. * @param string $scheme Optional. Scheme to give the site URL context. Accepts 
  3063. * 'http', 'https', or 'relative'. Default null. 
  3064. * @return string Site URL link with optional path appended. 
  3065. */ 
  3066. function network_site_url( $path = '', $scheme = null ) { 
  3067. if ( ! is_multisite() ) 
  3068. return site_url($path, $scheme); 
  3069.  
  3070. $current_network = get_network(); 
  3071.  
  3072. if ( 'relative' == $scheme ) 
  3073. $url = $current_network->path; 
  3074. else 
  3075. $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme ); 
  3076.  
  3077. if ( $path && is_string( $path ) ) 
  3078. $url .= ltrim( $path, '/' ); 
  3079.  
  3080. /** 
  3081. * Filters the network site URL. 
  3082. * 
  3083. * @since 3.0.0 
  3084. * 
  3085. * @param string $url The complete network site URL including scheme and path. 
  3086. * @param string $path Path relative to the network site URL. Blank string if 
  3087. * no path is specified. 
  3088. * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',  
  3089. * 'relative' or null. 
  3090. */ 
  3091. return apply_filters( 'network_site_url', $url, $path, $scheme ); 
  3092.  
  3093. /** 
  3094. * Retrieves the home URL for the current network. 
  3095. * 
  3096. * Returns the home URL with the appropriate protocol, 'https' is_ssl() 
  3097. * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is 
  3098. * overridden. 
  3099. * 
  3100. * @since 3.0.0 
  3101. * 
  3102. * @param string $path Optional. Path relative to the home URL. Default empty. 
  3103. * @param string $scheme Optional. Scheme to give the home URL context. Accepts 
  3104. * 'http', 'https', or 'relative'. Default null. 
  3105. * @return string Home URL link with optional path appended. 
  3106. */ 
  3107. function network_home_url( $path = '', $scheme = null ) { 
  3108. if ( ! is_multisite() ) 
  3109. return home_url($path, $scheme); 
  3110.  
  3111. $current_network = get_network(); 
  3112. $orig_scheme = $scheme; 
  3113.  
  3114. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) 
  3115. $scheme = is_ssl() && ! is_admin() ? 'https' : 'http'; 
  3116.  
  3117. if ( 'relative' == $scheme ) 
  3118. $url = $current_network->path; 
  3119. else 
  3120. $url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme ); 
  3121.  
  3122. if ( $path && is_string( $path ) ) 
  3123. $url .= ltrim( $path, '/' ); 
  3124.  
  3125. /** 
  3126. * Filters the network home URL. 
  3127. * 
  3128. * @since 3.0.0 
  3129. * 
  3130. * @param string $url The complete network home URL including scheme and path. 
  3131. * @param string $path Path relative to the network home URL. Blank string 
  3132. * if no path is specified. 
  3133. * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',  
  3134. * 'relative' or null. 
  3135. */ 
  3136. return apply_filters( 'network_home_url', $url, $path, $orig_scheme); 
  3137.  
  3138. /** 
  3139. * Retrieves the URL to the admin area for the network. 
  3140. * 
  3141. * @since 3.0.0 
  3142. * 
  3143. * @param string $path Optional path relative to the admin URL. Default empty. 
  3144. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 
  3145. * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 
  3146. * @return string Admin URL link with optional path appended. 
  3147. */ 
  3148. function network_admin_url( $path = '', $scheme = 'admin' ) { 
  3149. if ( ! is_multisite() ) 
  3150. return admin_url( $path, $scheme ); 
  3151.  
  3152. $url = network_site_url('wp-admin/network/', $scheme); 
  3153.  
  3154. if ( $path && is_string( $path ) ) 
  3155. $url .= ltrim($path, '/'); 
  3156.  
  3157. /** 
  3158. * Filters the network admin URL. 
  3159. * 
  3160. * @since 3.0.0 
  3161. * 
  3162. * @param string $url The complete network admin URL including scheme and path. 
  3163. * @param string $path Path relative to the network admin URL. Blank string if 
  3164. * no path is specified. 
  3165. */ 
  3166. return apply_filters( 'network_admin_url', $url, $path ); 
  3167.  
  3168. /** 
  3169. * Retrieves the URL to the admin area for the current user. 
  3170. * 
  3171. * @since 3.0.0 
  3172. * 
  3173. * @param string $path Optional. Path relative to the admin URL. Default empty. 
  3174. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 
  3175. * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 
  3176. * @return string Admin URL link with optional path appended. 
  3177. */ 
  3178. function user_admin_url( $path = '', $scheme = 'admin' ) { 
  3179. $url = network_site_url('wp-admin/user/', $scheme); 
  3180.  
  3181. if ( $path && is_string( $path ) ) 
  3182. $url .= ltrim($path, '/'); 
  3183.  
  3184. /** 
  3185. * Filters the user admin URL for the current user. 
  3186. * 
  3187. * @since 3.1.0 
  3188. * 
  3189. * @param string $url The complete URL including scheme and path. 
  3190. * @param string $path Path relative to the URL. Blank string if 
  3191. * no path is specified. 
  3192. */ 
  3193. return apply_filters( 'user_admin_url', $url, $path ); 
  3194.  
  3195. /** 
  3196. * Retrieves the URL to the admin area for either the current site or the network depending on context. 
  3197. * 
  3198. * @since 3.1.0 
  3199. * 
  3200. * @param string $path Optional. Path relative to the admin URL. Default empty. 
  3201. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 
  3202. * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 
  3203. * @return string Admin URL link with optional path appended. 
  3204. */ 
  3205. function self_admin_url( $path = '', $scheme = 'admin' ) { 
  3206. if ( is_network_admin() ) 
  3207. return network_admin_url($path, $scheme); 
  3208. elseif ( is_user_admin() ) 
  3209. return user_admin_url($path, $scheme); 
  3210. else 
  3211. return admin_url($path, $scheme); 
  3212.  
  3213. /** 
  3214. * Sets the scheme for a URL. 
  3215. * 
  3216. * @since 3.4.0 
  3217. * @since 4.4.0 The 'rest' scheme was added. 
  3218. * 
  3219. * @param string $url Absolute URL that includes a scheme 
  3220. * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login',  
  3221. * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null. 
  3222. * @return string $url URL with chosen scheme. 
  3223. */ 
  3224. function set_url_scheme( $url, $scheme = null ) { 
  3225. $orig_scheme = $scheme; 
  3226.  
  3227. if ( ! $scheme ) { 
  3228. $scheme = is_ssl() ? 'https' : 'http'; 
  3229. } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) { 
  3230. $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http'; 
  3231. } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) { 
  3232. $scheme = is_ssl() ? 'https' : 'http'; 
  3233.  
  3234. $url = trim( $url ); 
  3235. if ( substr( $url, 0, 2 ) === '//' ) 
  3236. $url = 'http:' . $url; 
  3237.  
  3238. if ( 'relative' == $scheme ) { 
  3239. $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) ); 
  3240. if ( $url !== '' && $url[0] === '/' ) 
  3241. $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" ); 
  3242. } else { 
  3243. $url = preg_replace( '#^\w+://#', $scheme . '://', $url ); 
  3244.  
  3245. /** 
  3246. * Filters the resulting URL after setting the scheme. 
  3247. * 
  3248. * @since 3.4.0 
  3249. * 
  3250. * @param string $url The complete URL including scheme and path. 
  3251. * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'. 
  3252. * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',  
  3253. * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. 
  3254. */ 
  3255. return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme ); 
  3256.  
  3257. /** 
  3258. * Retrieves the URL to the user's dashboard. 
  3259. * 
  3260. * If a user does not belong to any site, the global user dashboard is used. If the user 
  3261. * belongs to the current site, the dashboard for the current site is returned. If the user 
  3262. * cannot edit the current site, the dashboard to the user's primary site is returned. 
  3263. * 
  3264. * @since 3.1.0 
  3265. * 
  3266. * @param int $user_id Optional. User ID. Defaults to current user. 
  3267. * @param string $path Optional path relative to the dashboard. Use only paths known to 
  3268. * both site and user admins. Default empty. 
  3269. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() 
  3270. * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 
  3271. * @return string Dashboard URL link with optional path appended. 
  3272. */ 
  3273. function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) { 
  3274. $user_id = $user_id ? (int) $user_id : get_current_user_id(); 
  3275.  
  3276. $blogs = get_blogs_of_user( $user_id ); 
  3277. if ( ! is_super_admin() && empty($blogs) ) { 
  3278. $url = user_admin_url( $path, $scheme ); 
  3279. } elseif ( ! is_multisite() ) { 
  3280. $url = admin_url( $path, $scheme ); 
  3281. } else { 
  3282. $current_blog = get_current_blog_id(); 
  3283. if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) { 
  3284. $url = admin_url( $path, $scheme ); 
  3285. } else { 
  3286. $active = get_active_blog_for_user( $user_id ); 
  3287. if ( $active ) 
  3288. $url = get_admin_url( $active->blog_id, $path, $scheme ); 
  3289. else 
  3290. $url = user_admin_url( $path, $scheme ); 
  3291.  
  3292. /** 
  3293. * Filters the dashboard URL for a user. 
  3294. * 
  3295. * @since 3.1.0 
  3296. * 
  3297. * @param string $url The complete URL including scheme and path. 
  3298. * @param int $user_id The user ID. 
  3299. * @param string $path Path relative to the URL. Blank string if no path is specified. 
  3300. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',  
  3301. * 'login_post', 'admin', 'relative' or null. 
  3302. */ 
  3303. return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme); 
  3304.  
  3305. /** 
  3306. * Retrieves the URL to the user's profile editor. 
  3307. * 
  3308. * @since 3.1.0 
  3309. * 
  3310. * @param int $user_id Optional. User ID. Defaults to current user. 
  3311. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 
  3312. * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 
  3313. * @return string Dashboard URL link with optional path appended. 
  3314. */ 
  3315. function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) { 
  3316. $user_id = $user_id ? (int) $user_id : get_current_user_id(); 
  3317.  
  3318. if ( is_user_admin() ) 
  3319. $url = user_admin_url( 'profile.php', $scheme ); 
  3320. elseif ( is_network_admin() ) 
  3321. $url = network_admin_url( 'profile.php', $scheme ); 
  3322. else 
  3323. $url = get_dashboard_url( $user_id, 'profile.php', $scheme ); 
  3324.  
  3325. /** 
  3326. * Filters the URL for a user's profile editor. 
  3327. * 
  3328. * @since 3.1.0 
  3329. * 
  3330. * @param string $url The complete URL including scheme and path. 
  3331. * @param int $user_id The user ID. 
  3332. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',  
  3333. * 'login_post', 'admin', 'relative' or null. 
  3334. */ 
  3335. return apply_filters( 'edit_profile_url', $url, $user_id, $scheme); 
  3336.  
  3337. /** 
  3338. * Returns the canonical URL for a post. 
  3339. * 
  3340. * When the post is the same as the current requested page the function will handle the 
  3341. * pagination arguments too. 
  3342. * 
  3343. * @since 4.6.0 
  3344. * 
  3345. * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`. 
  3346. * @return string|false The canonical URL, or false if the post does not exist or has not 
  3347. * been published yet. 
  3348. */ 
  3349. function wp_get_canonical_url( $post = null ) { 
  3350. $post = get_post( $post ); 
  3351.  
  3352. if ( ! $post ) { 
  3353. return false; 
  3354.  
  3355. if ( 'publish' !== $post->post_status ) { 
  3356. return false; 
  3357.  
  3358. $canonical_url = get_permalink( $post ); 
  3359.  
  3360. // If a canonical is being generated for the current page, make sure it has pagination if needed. 
  3361. if ( $post->ID === get_queried_object_id() ) { 
  3362. $page = get_query_var( 'page', 0 ); 
  3363. if ( $page >= 2 ) { 
  3364. if ( '' == get_option( 'permalink_structure' ) ) { 
  3365. $canonical_url = add_query_arg( 'page', $page, $canonical_url ); 
  3366. } else { 
  3367. $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' ); 
  3368.  
  3369. $cpage = get_query_var( 'cpage', 0 ); 
  3370. if ( $cpage ) { 
  3371. $canonical_url = get_comments_pagenum_link( $cpage ); 
  3372.  
  3373. /** 
  3374. * Filters the canonical URL for a post. 
  3375. * 
  3376. * @since 4.6.0 
  3377. * 
  3378. * @param string $string The post's canonical URL. 
  3379. * @param WP_Post $post Post object. 
  3380. */ 
  3381. return apply_filters( 'get_canonical_url', $canonical_url, $post ); 
  3382.  
  3383. /** 
  3384. * Outputs rel=canonical for singular queries. 
  3385. * 
  3386. * @since 2.9.0 
  3387. * @since 4.6.0 Adjusted to use wp_get_canonical_url(). 
  3388. */ 
  3389. function rel_canonical() { 
  3390. if ( ! is_singular() ) { 
  3391. return; 
  3392.  
  3393. $id = get_queried_object_id(); 
  3394.  
  3395. if ( 0 === $id ) { 
  3396. return; 
  3397.  
  3398. $url = wp_get_canonical_url( $id ); 
  3399.  
  3400. if ( ! empty( $url ) ) { 
  3401. echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n"; 
  3402.  
  3403. /** 
  3404. * Returns a shortlink for a post, page, attachment, or site. 
  3405. * 
  3406. * This function exists to provide a shortlink tag that all themes and plugins can target. 
  3407. * A plugin must hook in to provide the actual shortlinks. Default shortlink support is 
  3408. * limited to providing ?p= style links for posts. Plugins can short-circuit this function 
  3409. * via the {@see 'pre_get_shortlink'} filter or filter the output via the {@see 'get_shortlink'} 
  3410. * filter. 
  3411. * 
  3412. * @since 3.0.0. 
  3413. * 
  3414. * @param int $id Optional. A post or site id. Default is 0, which means the current post or site. 
  3415. * @param string $context Optional. Whether the id is a 'site' id, 'post' id, or 'media' id. If 'post',  
  3416. * the post_type of the post is consulted. If 'query', the current query is consulted 
  3417. * to determine the id and context. Default 'post'. 
  3418. * @param bool $allow_slugs Optional. Whether to allow post slugs in the shortlink. It is up to the plugin how 
  3419. * and whether to honor this. Default true. 
  3420. * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks 
  3421. * are not enabled. 
  3422. */ 
  3423. function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) { 
  3424. /** 
  3425. * Filters whether to preempt generating a shortlink for the given post. 
  3426. * 
  3427. * Passing a truthy value to the filter will effectively short-circuit the 
  3428. * shortlink-generation process, returning that value instead. 
  3429. * 
  3430. * @since 3.0.0 
  3431. * 
  3432. * @param bool|string $return Short-circuit return value. Either false or a URL string. 
  3433. * @param int $id Post ID, or 0 for the current post. 
  3434. * @param string $context The context for the link. One of 'post' or 'query',  
  3435. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. 
  3436. */ 
  3437. $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs ); 
  3438.  
  3439. if ( false !== $shortlink ) { 
  3440. return $shortlink; 
  3441.  
  3442. $post_id = 0; 
  3443. if ( 'query' == $context && is_singular() ) { 
  3444. $post_id = get_queried_object_id(); 
  3445. $post = get_post( $post_id ); 
  3446. } elseif ( 'post' == $context ) { 
  3447. $post = get_post( $id ); 
  3448. if ( ! empty( $post->ID ) ) 
  3449. $post_id = $post->ID; 
  3450.  
  3451. $shortlink = ''; 
  3452.  
  3453. // Return p= link for all public post types. 
  3454. if ( ! empty( $post_id ) ) { 
  3455. $post_type = get_post_type_object( $post->post_type ); 
  3456.  
  3457. if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) { 
  3458. $shortlink = home_url( '/' ); 
  3459. } elseif ( $post_type->public ) { 
  3460. $shortlink = home_url( '?p=' . $post_id ); 
  3461.  
  3462. /** 
  3463. * Filters the shortlink for a post. 
  3464. * 
  3465. * @since 3.0.0 
  3466. * 
  3467. * @param string $shortlink Shortlink URL. 
  3468. * @param int $id Post ID, or 0 for the current post. 
  3469. * @param string $context The context for the link. One of 'post' or 'query',  
  3470. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default. 
  3471. */ 
  3472. return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs ); 
  3473.  
  3474. /** 
  3475. * Injects rel=shortlink into the head if a shortlink is defined for the current page. 
  3476. * 
  3477. * Attached to the {@see 'wp_head'} action. 
  3478. * 
  3479. * @since 3.0.0 
  3480. */ 
  3481. function wp_shortlink_wp_head() { 
  3482. $shortlink = wp_get_shortlink( 0, 'query' ); 
  3483.  
  3484. if ( empty( $shortlink ) ) 
  3485. return; 
  3486.  
  3487. echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n"; 
  3488.  
  3489. /** 
  3490. * Sends a Link: rel=shortlink header if a shortlink is defined for the current page. 
  3491. * 
  3492. * Attached to the {@see 'wp'} action. 
  3493. * 
  3494. * @since 3.0.0 
  3495. */ 
  3496. function wp_shortlink_header() { 
  3497. if ( headers_sent() ) 
  3498. return; 
  3499.  
  3500. $shortlink = wp_get_shortlink(0, 'query'); 
  3501.  
  3502. if ( empty($shortlink) ) 
  3503. return; 
  3504.  
  3505. header('Link: <' . $shortlink . '>; rel=shortlink', false); 
  3506.  
  3507. /** 
  3508. * Displays the shortlink for a post. 
  3509. * 
  3510. * Must be called from inside "The Loop" 
  3511. * 
  3512. * Call like the_shortlink( __( 'Shortlinkage FTW' ) ) 
  3513. * 
  3514. * @since 3.0.0 
  3515. * 
  3516. * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.' 
  3517. * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title. 
  3518. * @param string $before Optional HTML to display before the link. Default empty. 
  3519. * @param string $after Optional HTML to display after the link. Default empty. 
  3520. */ 
  3521. function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) { 
  3522. $post = get_post(); 
  3523.  
  3524. if ( empty( $text ) ) 
  3525. $text = __('This is the short link.'); 
  3526.  
  3527. if ( empty( $title ) ) 
  3528. $title = the_title_attribute( array( 'echo' => false ) ); 
  3529.  
  3530. $shortlink = wp_get_shortlink( $post->ID ); 
  3531.  
  3532. if ( !empty( $shortlink ) ) { 
  3533. $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>'; 
  3534.  
  3535. /** 
  3536. * Filters the short link anchor tag for a post. 
  3537. * 
  3538. * @since 3.0.0 
  3539. * 
  3540. * @param string $link Shortlink anchor tag. 
  3541. * @param string $shortlink Shortlink URL. 
  3542. * @param string $text Shortlink's text. 
  3543. * @param string $title Shortlink's title attribute. 
  3544. */ 
  3545. $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title ); 
  3546. echo $before, $link, $after; 
  3547.  
  3548.  
  3549. /** 
  3550. * Retrieves the avatar URL. 
  3551. * 
  3552. * @since 4.2.0 
  3553. * 
  3554. * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,  
  3555. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  3556. * @param array $args { 
  3557. * Optional. Arguments to return instead of the default arguments. 
  3558. * 
  3559. * @type int $size Height and width of the avatar in pixels. Default 96. 
  3560. * @type string $default URL for the default image or a default type. Accepts '404' (return 
  3561. * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),  
  3562. * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',  
  3563. * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or 
  3564. * 'gravatar_default' (the Gravatar logo). Default is the value of the 
  3565. * 'avatar_default' option, with a fallback of 'mystery'. 
  3566. * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false. 
  3567. * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are 
  3568. * judged in that order. Default is the value of the 'avatar_rating' option. 
  3569. * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values. 
  3570. * Default null. 
  3571. * @type array $processed_args When the function returns, the value will be the processed/sanitized $args 
  3572. * plus a "found_avatar" guess. Pass as a reference. Default null. 
  3573. * } 
  3574. * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar. 
  3575. */ 
  3576. function get_avatar_url( $id_or_email, $args = null ) { 
  3577. $args = get_avatar_data( $id_or_email, $args ); 
  3578. return $args['url']; 
  3579.  
  3580. /** 
  3581. * Retrieves default data about the avatar. 
  3582. * 
  3583. * @since 4.2.0 
  3584. * 
  3585. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,  
  3586. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  3587. * @param array $args { 
  3588. * Optional. Arguments to return instead of the default arguments. 
  3589. * 
  3590. * @type int $size Height and width of the avatar image file in pixels. Default 96. 
  3591. * @type int $height Display height of the avatar in pixels. Defaults to $size. 
  3592. * @type int $width Display width of the avatar in pixels. Defaults to $size. 
  3593. * @type string $default URL for the default image or a default type. Accepts '404' (return 
  3594. * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),  
  3595. * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',  
  3596. * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or 
  3597. * 'gravatar_default' (the Gravatar logo). Default is the value of the 
  3598. * 'avatar_default' option, with a fallback of 'mystery'. 
  3599. * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false. 
  3600. * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are 
  3601. * judged in that order. Default is the value of the 'avatar_rating' option. 
  3602. * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values. 
  3603. * Default null. 
  3604. * @type array $processed_args When the function returns, the value will be the processed/sanitized $args 
  3605. * plus a "found_avatar" guess. Pass as a reference. Default null. 
  3606. * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty. 
  3607. * } 
  3608. * @return array $processed_args { 
  3609. * Along with the arguments passed in `$args`, this will contain a couple of extra arguments. 
  3610. * 
  3611. * @type bool $found_avatar True if we were able to find an avatar for this user,  
  3612. * false or not set if we couldn't. 
  3613. * @type string $url The URL of the avatar we found. 
  3614. * } 
  3615. */ 
  3616. function get_avatar_data( $id_or_email, $args = null ) { 
  3617. $args = wp_parse_args( $args, array( 
  3618. 'size' => 96,  
  3619. 'height' => null,  
  3620. 'width' => null,  
  3621. 'default' => get_option( 'avatar_default', 'mystery' ),  
  3622. 'force_default' => false,  
  3623. 'rating' => get_option( 'avatar_rating' ),  
  3624. 'scheme' => null,  
  3625. 'processed_args' => null, // if used, should be a reference 
  3626. 'extra_attr' => '',  
  3627. ) ); 
  3628.  
  3629. if ( is_numeric( $args['size'] ) ) { 
  3630. $args['size'] = absint( $args['size'] ); 
  3631. if ( ! $args['size'] ) { 
  3632. $args['size'] = 96; 
  3633. } else { 
  3634. $args['size'] = 96; 
  3635.  
  3636. if ( is_numeric( $args['height'] ) ) { 
  3637. $args['height'] = absint( $args['height'] ); 
  3638. if ( ! $args['height'] ) { 
  3639. $args['height'] = $args['size']; 
  3640. } else { 
  3641. $args['height'] = $args['size']; 
  3642.  
  3643. if ( is_numeric( $args['width'] ) ) { 
  3644. $args['width'] = absint( $args['width'] ); 
  3645. if ( ! $args['width'] ) { 
  3646. $args['width'] = $args['size']; 
  3647. } else { 
  3648. $args['width'] = $args['size']; 
  3649.  
  3650. if ( empty( $args['default'] ) ) { 
  3651. $args['default'] = get_option( 'avatar_default', 'mystery' ); 
  3652.  
  3653. switch ( $args['default'] ) { 
  3654. case 'mm' : 
  3655. case 'mystery' : 
  3656. case 'mysteryman' : 
  3657. $args['default'] = 'mm'; 
  3658. break; 
  3659. case 'gravatar_default' : 
  3660. $args['default'] = false; 
  3661. break; 
  3662.  
  3663. $args['force_default'] = (bool) $args['force_default']; 
  3664.  
  3665. $args['rating'] = strtolower( $args['rating'] ); 
  3666.  
  3667. $args['found_avatar'] = false; 
  3668.  
  3669. /** 
  3670. * Filters whether to retrieve the avatar URL early. 
  3671. * 
  3672. * Passing a non-null value in the 'url' member of the return array will 
  3673. * effectively short circuit get_avatar_data(), passing the value through 
  3674. * the {@see 'get_avatar_data'} filter and returning early. 
  3675. * 
  3676. * @since 4.2.0 
  3677. * 
  3678. * @param array $args Arguments passed to get_avatar_data(), after processing. 
  3679. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,  
  3680. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  3681. */ 
  3682. $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email ); 
  3683.  
  3684. if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) { 
  3685. /** This filter is documented in wp-includes/link-template.php */ 
  3686. return apply_filters( 'get_avatar_data', $args, $id_or_email ); 
  3687.  
  3688. $email_hash = ''; 
  3689. $user = $email = false; 
  3690.  
  3691. if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) { 
  3692. $id_or_email = get_comment( $id_or_email ); 
  3693.  
  3694. // Process the user identifier. 
  3695. if ( is_numeric( $id_or_email ) ) { 
  3696. $user = get_user_by( 'id', absint( $id_or_email ) ); 
  3697. } elseif ( is_string( $id_or_email ) ) { 
  3698. if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) { 
  3699. // md5 hash 
  3700. list( $email_hash ) = explode( '@', $id_or_email ); 
  3701. } else { 
  3702. // email address 
  3703. $email = $id_or_email; 
  3704. } elseif ( $id_or_email instanceof WP_User ) { 
  3705. // User Object 
  3706. $user = $id_or_email; 
  3707. } elseif ( $id_or_email instanceof WP_Post ) { 
  3708. // Post Object 
  3709. $user = get_user_by( 'id', (int) $id_or_email->post_author ); 
  3710. } elseif ( $id_or_email instanceof WP_Comment ) { 
  3711. /** 
  3712. * Filters the list of allowed comment types for retrieving avatars. 
  3713. * 
  3714. * @since 3.0.0 
  3715. * 
  3716. * @param array $types An array of content types. Default only contains 'comment'. 
  3717. */ 
  3718. $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 
  3719. if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { 
  3720. $args['url'] = false; 
  3721. /** This filter is documented in wp-includes/link-template.php */ 
  3722. return apply_filters( 'get_avatar_data', $args, $id_or_email ); 
  3723.  
  3724. if ( ! empty( $id_or_email->user_id ) ) { 
  3725. $user = get_user_by( 'id', (int) $id_or_email->user_id ); 
  3726. if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) { 
  3727. $email = $id_or_email->comment_author_email; 
  3728.  
  3729. if ( ! $email_hash ) { 
  3730. if ( $user ) { 
  3731. $email = $user->user_email; 
  3732.  
  3733. if ( $email ) { 
  3734. $email_hash = md5( strtolower( trim( $email ) ) ); 
  3735.  
  3736. if ( $email_hash ) { 
  3737. $args['found_avatar'] = true; 
  3738. $gravatar_server = hexdec( $email_hash[0] ) % 3; 
  3739. } else { 
  3740. $gravatar_server = rand( 0, 2 ); 
  3741.  
  3742. $url_args = array( 
  3743. 's' => $args['size'],  
  3744. 'd' => $args['default'],  
  3745. 'f' => $args['force_default'] ? 'y' : false,  
  3746. 'r' => $args['rating'],  
  3747. ); 
  3748.  
  3749. if ( is_ssl() ) { 
  3750. $url = 'https://secure.gravatar.com/avatar/' . $email_hash; 
  3751. } else { 
  3752. $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash ); 
  3753.  
  3754. $url = add_query_arg( 
  3755. rawurlencode_deep( array_filter( $url_args ) ),  
  3756. set_url_scheme( $url, $args['scheme'] ) 
  3757. ); 
  3758.  
  3759. /** 
  3760. * Filters the avatar URL. 
  3761. * 
  3762. * @since 4.2.0 
  3763. * 
  3764. * @param string $url The URL of the avatar. 
  3765. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,  
  3766. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  3767. * @param array $args Arguments passed to get_avatar_data(), after processing. 
  3768. */ 
  3769. $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args ); 
  3770.  
  3771. /** 
  3772. * Filters the avatar data. 
  3773. * 
  3774. * @since 4.2.0 
  3775. * 
  3776. * @param array $args Arguments passed to get_avatar_data(), after processing. 
  3777. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,  
  3778. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  3779. */ 
  3780. return apply_filters( 'get_avatar_data', $args, $id_or_email ); 
  3781.  
  3782. /** 
  3783. * Retrieves the URL of a file in the theme. 
  3784. * 
  3785. * Searches in the stylesheet directory before the template directory so themes 
  3786. * which inherit from a parent theme can just override one file. 
  3787. * 
  3788. * @since 4.7.0 
  3789. * 
  3790. * @param string $file Optional. File to search for in the stylesheet directory. 
  3791. * @return string The URL of the file. 
  3792. */ 
  3793. function get_theme_file_uri( $file = '' ) { 
  3794. $file = ltrim( $file, '/' ); 
  3795.  
  3796. if ( empty( $file ) ) { 
  3797. $url = get_stylesheet_directory_uri(); 
  3798. } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) { 
  3799. $url = get_stylesheet_directory_uri() . '/' . $file; 
  3800. } else { 
  3801. $url = get_template_directory_uri() . '/' . $file; 
  3802.  
  3803. /** 
  3804. * Filters the URL to a file in the theme. 
  3805. * 
  3806. * @since 4.7.0 
  3807. * 
  3808. * @param string $url The file URL. 
  3809. * @param string $file The requested file to search for. 
  3810. */ 
  3811. return apply_filters( 'theme_file_uri', $url, $file ); 
  3812.  
  3813. /** 
  3814. * Retrieves the URL of a file in the parent theme. 
  3815. * 
  3816. * @since 4.7.0 
  3817. * 
  3818. * @param string $file Optional. File to return the URL for in the template directory. 
  3819. * @return string The URL of the file. 
  3820. */ 
  3821. function get_parent_theme_file_uri( $file = '' ) { 
  3822. $file = ltrim( $file, '/' ); 
  3823.  
  3824. if ( empty( $file ) ) { 
  3825. $url = get_template_directory_uri(); 
  3826. } else { 
  3827. $url = get_template_directory_uri() . '/' . $file; 
  3828.  
  3829. /** 
  3830. * Filters the URL to a file in the parent theme. 
  3831. * 
  3832. * @since 4.7.0 
  3833. * 
  3834. * @param string $url The file URL. 
  3835. * @param string $file The requested file to search for. 
  3836. */ 
  3837. return apply_filters( 'parent_theme_file_uri', $url, $file ); 
  3838.  
  3839. /** 
  3840. * Retrieves the path of a file in the theme. 
  3841. * 
  3842. * Searches in the stylesheet directory before the template directory so themes 
  3843. * which inherit from a parent theme can just override one file. 
  3844. * 
  3845. * @since 4.7.0 
  3846. * 
  3847. * @param string $file Optional. File to search for in the stylesheet directory. 
  3848. * @return string The path of the file. 
  3849. */ 
  3850. function get_theme_file_path( $file = '' ) { 
  3851. $file = ltrim( $file, '/' ); 
  3852.  
  3853. if ( empty( $file ) ) { 
  3854. $path = get_stylesheet_directory(); 
  3855. } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) { 
  3856. $path = get_stylesheet_directory() . '/' . $file; 
  3857. } else { 
  3858. $path = get_template_directory() . '/' . $file; 
  3859.  
  3860. /** 
  3861. * Filters the path to a file in the theme. 
  3862. * 
  3863. * @since 4.7.0 
  3864. * 
  3865. * @param string $path The file path. 
  3866. * @param string $file The requested file to search for. 
  3867. */ 
  3868. return apply_filters( 'theme_file_path', $path, $file ); 
  3869.  
  3870. /** 
  3871. * Retrieves the path of a file in the parent theme. 
  3872. * 
  3873. * @since 4.7.0 
  3874. * 
  3875. * @param string $file Optional. File to return the path for in the template directory. 
  3876. * @return string The path of the file. 
  3877. */ 
  3878. function get_parent_theme_file_path( $file = '' ) { 
  3879. $file = ltrim( $file, '/' ); 
  3880.  
  3881. if ( empty( $file ) ) { 
  3882. $path = get_template_directory(); 
  3883. } else { 
  3884. $path = get_template_directory() . '/' . $file; 
  3885.  
  3886. /** 
  3887. * Filters the path to a file in the parent theme. 
  3888. * 
  3889. * @since 4.7.0 
  3890. * 
  3891. * @param string $path The file path. 
  3892. * @param string $file The requested file to search for. 
  3893. */ 
  3894. return apply_filters( 'parent_theme_file_path', $path, $file ); 
.