2

in my single.php i took a data from one $_GET[] value and i'm using this like

domain.com/example-slug/?holder=value

The thing i want is domain.com/example-slug/value or domain.com/example-slug/holder/value

so, how i can to this?

4
  • What is this holder & what relation does it have with a single post? You don't have to get the value from $_GET. If the relation is clear, then perhaps you can achieve the same with a custom post type or taxonomy.
    – Fayaz
    Commented Aug 25, 2018 at 9:00
  • I have to use $_GET, i know ctp's or taxonomies. $_GET value call a data from custom meta box array.
    – Jane Doe
    Commented Aug 25, 2018 at 9:11
  • you can use Rewrite API for your needs. You can create rewrite tags and rules for functionality. Check these page pls. codex.wordpress.org/Rewrite_API/add_rewrite_rule Commented Aug 25, 2018 at 10:09
  • I know add_rewrite_rule and i search this, just i don't know how i can do this with that. This is the reason why i ask Serkan Bey :)
    – Jane Doe
    Commented Aug 25, 2018 at 10:34

3 Answers 3

1

You don't need to do anything with rewrite rules.

Your example ("domain.com/example-slug/?holder=value") is a perfect example of "Plain" Permalinks. So your starting point is to set Permalinks (Settings > Permalinks) to something other than "Plain". This will take the "/?" component out of the URL, and generate a URL in your preferred format.

The second thing is to capture the URL, parse it and get the "value" (to use your words). You can do this with a function like the one below. Put this function in your functions.php (or a plugin if you prefer). Then call it on the page/post where you want to get the "value". The function will yield the "value" as a variable $myvalue. You can expand the function to do more stuff, or you could return the value elsewhere to do something with it.

function getslug(){

    // get the url
    $myurl = home_url( $wp->request );
    //echo "<p>The page url is ".$myurl." </p>";  //DEBUG

    // Explode the url to get access to the taxonomy and terms
    $pageterms = explode('/', $myurl);
    //echo "the exploded components of the URL <pre>";print_r($pageterms);echo "</pre>"; //DEBUG

    // the value is always the last element in the url, so we'll count the number of elements
    $mycount = count($pageterms);
    //echo "<p>The element count is ".$mycount." </p>";  //DEBUG

    // since array keys always start with zero, the key for the value will be the count minus 1 (to allow for the zero)
    $myvalue = $pageterms[$mycount-1];
    //echo "<p>My value is  ".$myvalue." </p>";  //DEBUG

}

PS: I've left my echo statements (used for Debugging) in this example; you can just delete them if you don't need/want them.

0

You can rewrite your URL using Rewrite rule like this

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ domain.com/$1 [L,R=301,NC]

RewriteRule ^example-slug/value $ example-slug/?holder=$1 [L]
1
  • Right now htaccess is my last option, i'm trying to do this with Wordpress codes.
    – Jane Doe
    Commented Aug 25, 2018 at 10:34
0

You can add a rewrite function filter to your "site specific functions plugin" or to theme's functions.php

After editing functions.php or activating your plugin you will need to flush WP's rewrite rules. Go to Admin Dashboard >Settings > Permalinks and click save (your new rules will then be applied)

Examples (not tested):

For posts:

<?php
/*
Plugin Name: my post rewrite plugin
*/
function my_post_rules($rules)
{
    global $wp_rewrite;
    $my_rule = array(
      '(.+?)/(.+?)$' => 'index.php?category_name=$matches[1]&holder=$matches[2]'
// or
      'example-slug/(.+?)$' => 'index.php?category_name=example-slug&holder=$matches[1]'
    );
    return array_merge($my_rule, $rules);
}
add_filter('post_rewrite_rules', 'my_post_rules');

For pages:

function my_page_rules($rules)
{
    global $wp_rewrite;
    $my_rule = array(
      '(.+?)/(.+?)$' => 'index.php?page_name=$matches[1]&holder=$matches[2]'
// or
      'example-slug/(.+?)$' => 'index.php?page_name=' . 'example-slug&holder=$matches[1]'
    );
    return array_merge($my_rule, $rules);
}
add_filter('page_rewrite_rules', 'my_page_rules');

Use of WP's get_query_var function in your template is probably unnecessary in your case, but it might be needed for "GET values" used in other circumstances. So it is a good habit to use get_query_var in your template and "declare" the query var via query_var filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.