{"id":100243,"date":"2020-03-02T12:03:44","date_gmt":"2020-03-02T18:03:44","guid":{"rendered":"https:\/\/wpengine.com\/?post_type=resource&#038;p=100243"},"modified":"2024-09-29T09:42:52","modified_gmt":"2024-09-29T14:42:52","slug":"get-url-params-wordpress","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/","title":{"rendered":"How To Extract URL Parameters in WordPress"},"content":{"rendered":"\n<p>At any given moment, new data is being created on your website. In fact, every click a user makes results in a new piece of information. The challenge lies in harnessing that data and gaining valuable input from it.&nbsp;<\/p>\n\n\n\n<p>That\u2019s where <a href=\"https:\/\/support.google.com\/google-ads\/answer\/6277564?\" target=\"_blank\" rel=\"noreferrer noopener\">URL parameters<\/a> can come in handy. We\u2019ll explain more about what these are later in this post. In a nutshell, however, multiple parameters can be used to pass key information to your website. That includes important information about its users, such as what buttons they click on and when.&nbsp;<\/p>\n\n\n\n<p>In this article, we\u2019ll provide an overview of URL parameters. This will include details about what they are, as well as how to extract them from your WordPress website both manually and using a plugin. Let\u2019s get started!&nbsp;<\/p>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">What are URL parameters?<\/h2>\n\n\n\n<p>First, let\u2019s make sure the basics are covered. A Uniform Resource Locator (URL) is what\u2019s entered into a browser address bar in order to visit a website (i.e., <em>https:\/\/mywebsite.com<\/em>). Just as its name suggests, it\u2019s a uniform way to locate the resources for any website that\u2019s hosted online.&nbsp;<\/p>\n\n\n\n<p>In order to extract URL parameters, it\u2019s important to understand that you\u2019ll be working with &#8220;<a href=\"https:\/\/www.w3schools.com\/asp\/coll_querystring.asp\" target=\"_blank\" rel=\"noreferrer noopener\">query strings<\/a>.&#8221; This is a part of a URL that is typically added after the Top-Level Domain (TLD), such as <em>.com<\/em> or <em>.org<\/em>. Its purpose is to designate the value of specific parameters.<\/p>\n\n\n\n<p>The parameter portion is usually pretty easy to spot, as it starts with a question mark:<\/p>\n\n\n\n<p><a href=\"http:\/\/www.website.com\/hats?productid=9876\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/www.website.com\/hats?productid=9876<\/a><\/p>\n\n\n\n<p>The above example shows a URL parameter made up of what\u2019s called a <a href=\"https:\/\/stackoverflow.com\/questions\/25955749\/what-is-a-key-value-pair\" target=\"_blank\" rel=\"noreferrer noopener\">key and value pair<\/a>. These are separated by an equals sign. In this case, the key is \u201cproductid,\u201d and the value is \u201c9876.\u201d<\/p>\n\n\n\n<p>As we mentioned earlier, URL parameters are also sometimes referred to as query strings. The important thing to remember is that these strings and query parameters are generated almost any time a user clicks on something. For that reason, developers tend to find fault with this function, as it is very easy to expose sensitive information through a URL parameter.&nbsp;<\/p>\n\n\n\n<p>While they do provide potentially helpful data points, URL parameters can also cause issues for Search Engine Optimization (SEO). For example, if you were to filter products in an online store by what they are, their sizes, and their prices, each unique URL would feature a different query string.&nbsp;<\/p>\n\n\n\n<p>While these strings would all be pointing to different content displays, search engines would see the resulting URLs as repeat paths to the same content. This can be problematic, unless you take some time to make an allowance for them in <a href=\"https:\/\/wpengine.com\/resources\/google-search-console-wordpress-optimization\/\" target=\"_blank\" rel=\"noreferrer noopener\">Google Search Console<\/a>.&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>With that being said, if you\u2019re looking for a way to extract URL parameters, you can use a plugin to assist in the process. There are also ways to more securely use this feature. In the rest of this post, we\u2019ll walk through how to set up a dedicated plugin, and how to manually experiment with parameters in WordPress.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getting URL parameters by using a plugin<\/h2>\n\n\n\n<p>Thanks to WordPress\u2019 open-source codebase, developers have been able to provide us with many useful tools for what are otherwise complex tasks. This can make it easy to extract the query parameter data from your website\u2019s URLs. Let\u2019s walk through how the process works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Choose a plugin<\/h3>\n\n\n\n<p>Unfortunately, there are not many URL parameter plugins that have been well-tested with the most updated version of WordPress. However, there are a few options with favorable ratings. One of the more widely installed tools is the <a href=\"https:\/\/wordpress.org\/plugins\/url-params\/\" target=\"_blank\" rel=\"noreferrer noopener\">URL Params plugin<\/a>:&nbsp;<\/p>\n\n\n\n<p>This plugin enables you to use shortcodes to display specific content, depending on the query parameters present in the URL. For example, you can display content based on what type of user has signed in, prefill form fields, or even greet visitors by name.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Insert the shortcode you want to use<\/h3>\n\n\n\n<p>The URL Params plugin provides a shortcode that you can use on your pages and posts. They can be used as-is, or tweaked to fit your needs. A standard URL Params shortcode begins with the following:<\/p>\n\n\n\n<p><code>[urlparam param=\"\" \/]<\/code><\/p>\n\n\n\n<p>For example, you can create conditional shortcodes that will return specific text depending on what query string parameters are present. Let\u2019s insert a conditional example into a WordPress page, in order to see what it looks like in action.&nbsp;<\/p>\n\n\n\n<p>We\u2019ve chosen our <em>Contact Us<\/em> page, which contains a form. Our conditional shortcode is set up to return designated text based on whether or not certain parameters are present. We\u2019ll first select the <em>Shortcode<\/em> block in the Block Editor, and paste in our shortcode:&nbsp;<\/p>\n\n\n\n<p>This shortcode will check to see if the URL parameter indicates that the user is logged in. If not, text will display inviting them to fill out the form:&nbsp;<\/p>\n\n\n\n<p>It\u2019s important to note that while this plugin has plenty of five-star reviews and many positive comments, it has not been updated in some time. Also, there are risks associated with passing information to your website in this way. It\u2019s recommended that you fully <a href=\"https:\/\/www.asandia.com\/wordpress-plugins\/urlparams\/\" target=\"_blank\" rel=\"noreferrer noopener\">review the plugin documentation<\/a> before using it, and test it out on a staging site if possible.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getting URL parameters by modifying page code<\/h2>\n\n\n\n<p>If you prefer a more hands-on approach to working with URL parameters, you can modify your page code directly. Thanks to WordPress core\u2019s flexibility, there are different ways to access URL parameters.&nbsp;<\/p>\n\n\n\n<p>If you want to understand the process of extracting multiple parameters from a URL, we recommend familiarizing yourself with the WP_Query class. This is where you\u2019ll find functions you can use to either write your own plugin, or tweak existing PHP code.&nbsp;<\/p>\n\n\n\n<p>For example, <em>get_query_var<\/em> enables you to retrieve any parameters (variables) that are present in the WP_Query class. This means you can only use it to access and retrieve variables that are publicly available in the class. These are all listed in the <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress Codex<\/a>.<\/p>\n\n\n\n<p>With that being said, if you have variables present because you\u2019ve <a href=\"https:\/\/wpengine.com\/resources\/wordpress-permalinks\/\" target=\"_blank\" rel=\"noreferrer noopener\">created custom URLs<\/a>, they will not be available. However, you can <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_query_var\/#custom-query-vars\" target=\"_blank\" rel=\"noreferrer noopener\">work around that issue<\/a> by hooking into the <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/query_vars\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>query_vars()<\/em><\/a> filter. This will add your new variables to the <em>$var<\/em> array.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Securing your query strings<\/h2>\n\n\n\n<p>Since passing data to your site through the URL is a potential security risk, you might also want to explore the use of the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/sanitize_text_field\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>sanitize_text_field()<\/em> function<\/a> in WordPress. This can help ensure that you\u2019re not receiving nefarious code strings, which could jeopardize your website.&nbsp;<\/p>\n\n\n\n<p>Additionally, depending on your specific use case, you can use the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_reset_query\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>wp_reset_query()<\/em><\/a> function. This is useful in situations with multiple loops and queries. It resets the current query back to the initial loop \u2013 a way of \u2018cleaning the slate\u2019 before initializing another separate and distinct query.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Get everything you need with WP Engine<\/h2>\n\n\n\n<p>Passing information to your website by extracting URL parameters can be useful, but you might require advanced <a href=\"https:\/\/developer.wordpress.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">developer resources<\/a> in order to pull it off. Fortunately, we have you covered. Here at WP Engine, we love trying out new techniques and helping you do the same.&nbsp;<\/p>\n\n\n\n<p>While you\u2019re here, don\u2019t forget to check out our <a href=\"https:\/\/wpengine.com\/plans\/\" target=\"_blank\" rel=\"noreferrer noopener\">hosting for WordPress sites<\/a> for your next project. Benefit from award-winning support from our experts in WordPress and a wide array of  <a href=\"https:\/\/wpengine.com\/wordpress-hosting\/\" target=\"_blank\" rel=\"noreferrer noopener\">tools and technology<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At any given moment, new data is being created on your website. In fact, every click a user makes results in a new piece of information. The challenge lies in harnessing that data and gaining valuable input from it.&nbsp; That\u2019s where URL parameters can come in handy. We\u2019ll explain more about what these are later<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":1,"featured_media":136344,"template":"","resource-topic":[913,904],"resource-role":[895,896,906,897,903],"resource-type":[916],"class_list":["post-100243","resource","type-resource","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Extract URL Parameters in WordPress<\/title>\n<meta name=\"description\" content=\"Need to learn how to GET (or extract) URL parameters for a WordPress site? The process can seem tricky, but our guide can help you get URL params easily.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Extract URL Parameters in WordPress\" \/>\n<meta property=\"og:description\" content=\"Need to learn how to GET (or extract) URL parameters for a WordPress site? The process can seem tricky, but our guide can help you get URL params easily.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Engine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpengine\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-29T14:42:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2022\/10\/Shutterstock_699634498-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"627\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"How To Extract URL Parameters in WordPress\" \/>\n<meta name=\"twitter:description\" content=\"Need to learn how to GET (or extract) URL parameters for a WordPress site? The process can seem tricky, but our guide can help you get URL params easily.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2022\/10\/Shutterstock_699634498-1.jpg\" \/>\n<meta name=\"twitter:site\" content=\"@wpengine\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/\",\"url\":\"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/\",\"name\":\"How To Extract URL Parameters in WordPress\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\"},\"datePublished\":\"2020-03-02T18:03:44+00:00\",\"dateModified\":\"2024-09-29T14:42:52+00:00\",\"description\":\"Need to learn how to GET (or extract) URL parameters for a WordPress site? The process can seem tricky, but our guide can help you get URL params easily.\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wpengine.com\/case-studies\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resources\",\"item\":\"https:\/\/wpengine.com\/case-studies\/resources\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How To Extract URL Parameters in WordPress\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\",\"url\":\"https:\/\/wpengine.com\/case-studies\/\",\"name\":\"WP Engine\",\"description\":\"Managed Hosting for WordPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085\",\"name\":\"WP Engine\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"caption\":\"WP Engine\"},\"sameAs\":[\"https:\/\/wpengine.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Extract URL Parameters in WordPress","description":"Need to learn how to GET (or extract) URL parameters for a WordPress site? The process can seem tricky, but our guide can help you get URL params easily.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"How To Extract URL Parameters in WordPress","og_description":"Need to learn how to GET (or extract) URL parameters for a WordPress site? The process can seem tricky, but our guide can help you get URL params easily.","og_url":"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2024-09-29T14:42:52+00:00","og_image":[{"width":1200,"height":627,"url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2022\/10\/Shutterstock_699634498-1.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_title":"How To Extract URL Parameters in WordPress","twitter_description":"Need to learn how to GET (or extract) URL parameters for a WordPress site? The process can seem tricky, but our guide can help you get URL params easily.","twitter_image":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2022\/10\/Shutterstock_699634498-1.jpg","twitter_site":"@wpengine","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/","url":"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/","name":"How To Extract URL Parameters in WordPress","isPartOf":{"@id":"https:\/\/wpengine.com\/case-studies\/#website"},"datePublished":"2020-03-02T18:03:44+00:00","dateModified":"2024-09-29T14:42:52+00:00","description":"Need to learn how to GET (or extract) URL parameters for a WordPress site? The process can seem tricky, but our guide can help you get URL params easily.","breadcrumb":{"@id":"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/case-studies\/resources\/get-url-params-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/case-studies\/"},{"@type":"ListItem","position":2,"name":"Resources","item":"https:\/\/wpengine.com\/case-studies\/resources\/"},{"@type":"ListItem","position":3,"name":"How To Extract URL Parameters in WordPress"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/case-studies\/#website","url":"https:\/\/wpengine.com\/case-studies\/","name":"WP Engine","description":"Managed Hosting for WordPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085","name":"WP Engine","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","caption":"WP Engine"},"sameAs":["https:\/\/wpengine.com"]}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2020\/03\/shutterstock_1259987377.jpg","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":"<strong>Roles:<\/strong> Agency, Developer, Entrepreneur, Freelancer, Marketer","topic":"<strong>Topics:<\/strong> Analytics, Marketing","_links":{"self":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource\/100243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource"}],"about":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/types\/resource"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/users\/1"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media\/136344"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media?parent=100243"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-topic?post=100243"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-role?post=100243"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-type?post=100243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}