Overview

Classes

  • xu
  • Xu\Foundation\Foundation
  • Xu\Foundation\Jsonable
  • Xu\Model\Collection
  • Xu\Model\Model

Functions

  • xu
  • xu_add_option
  • xu_attachment_url_to_postid
  • xu_cache_delete
  • xu_cache_delete_group
  • xu_cache_get
  • xu_cache_key
  • xu_camel_case
  • xu_clean_attachment_cache
  • xu_clean_nav_menu_cache
  • xu_clean_post_cache
  • xu_contains
  • xu_convert_to_string
  • xu_current_attachment_url_to_postid
  • xu_current_url
  • xu_current_url_to_postid
  • xu_dashify
  • xu_delete_option
  • xu_delete_site_transient
  • xu_delete_transient
  • xu_doing_ajax
  • xu_ends_with
  • xu_get_class_name
  • xu_get_model
  • xu_get_option
  • xu_get_option_name
  • xu_get_option_post_id
  • xu_get_posts
  • xu_get_site_transient
  • xu_get_top_parent_post
  • xu_get_top_parent_post_type
  • xu_get_top_parent_post_type_object
  • xu_get_transient
  • xu_is_empty
  • xu_is_http_method
  • xu_is_json
  • xu_is_post_type
  • xu_is_wp
  • xu_is_xml
  • xu_post_exists
  • xu_register_large_option_post_type
  • xu_snake_case
  • xu_starts_with
  • xu_strip_spaces
  • xu_studly_case
  • xu_update_option
  • xu_url_to_postid
  • xu_valid_personnummer
  • xu_wp_get_attachment_image
  • xu_wp_get_attachment_image_sizes
  • xu_wp_get_attachment_image_src
  • xu_wp_get_attachment_image_srcset
  • xu_wp_get_attachment_url
  • xu_wp_nav_menu
  • Overview
  • Class
 1: <?php
 2: 
 3: /**
 4:  * Get current url.
 5:  *
 6:  * @param  bool $parse If url should be parsed using `parse_url`. Default false.
 7:  * @param  bool $obj   If parsed url should return as a object instead of array. Default false.
 8:  *
 9:  * @return mixed
10:  */
11: function xu_current_url( $parse = false, $obj = false ) {
12:     $url = $_SERVER['REQUEST_URI'];
13:     $url = ltrim( $url, '/' );
14:     $url = home_url() . '/' . $url;
15: 
16:     // If url shouldn't be parsed,
17:     // just return it as string.
18:     if ( ! (bool) $parse ) {
19:         return $url;
20:     }
21: 
22:     $parts = parse_url( $url );
23:     $parts = $parts === false ? [] : $parts;
24: 
25:     // Return as object or array.
26:     return $obj ? (object) $parts : $parts;
27: }
28: 
29: /**
30:  * Get post id from current url.
31:  *
32:  * @return string
33:  */
34: function xu_current_url_to_postid() {
35:     return xu_url_to_postid( xu_current_url() );
36: }
37: 
38: /**
39:  * Cached version of `url_to_post_id`.
40:  *
41:  * @see https://developer.wordpress.org/reference/functions/url_to_post_id/
42:  *
43:  * @param  string $url
44:  *
45:  * @return int
46:  */
47: function xu_url_to_postid( $url = '' ) {
48:     // `url_to_postid` don't work before `init` action.
49:     if ( ! did_action( 'init' ) ) {
50:         return 0;
51:     }
52: 
53:     // Only valid urls.
54:     if ( parse_url( $url, PHP_URL_HOST ) !== parse_url( home_url(), PHP_URL_HOST ) ) {
55:         return 0;
56:     }
57: 
58:     return (int) xu_cache_get( 'url_to_postid', [$url], __FUNCTION__ );
59: }
60: 
API documentation generated by ApiGen