Jump to content

Wordpress permalink


SkyRanger

Recommended Posts

I'm confused by your question. In a URL, '?xx' is the first variable passed via GET, '&xx' is any of any number of variables passed in the URL after the first. So your desire to change '?urlid=abc123' to '&urlid=abc123' is solved by adding a variable before the urlid.

What is the value of your permalink option in the Admin > Settings > Permalinks? If you're set to 'Post name' there are a few different things you can do. Obviously you can grab $_GET['urlid'] using an early-running hook and go if it's anywhere in the URL string. However, if you're saying that WordPress is removing the '?urlid=' part, that's a routing issue. You can set up routing in WP using the WordPress rewrite rules which will give you something along the lines of 'http://url.com/kudos/abc123' or 'http://url.com/urlid/abc123' depending on how it's all set up.

Link to comment
Share on other sites

Right now I am coding it to work with $_GET urlid so the link right now would go http://127.0.0.1/kudos/?urlid=randomcode but if I change it to http://127.0.0.1/?p=123 the ?urlid will not work after but the ?p=123&urlid=randomcode does

The issue that I am running into is right now is that permalink is set to post name.  Trying to sort out how to fix it if somebody else is running plain.  The ? will work in post name but not plain.

Link to comment
Share on other sites

Due to being VERY new to coding wordpress plugins, was wondering if somebody could point me in a direction to learn more about rewrites (actually looking for example) so I can properly learn how to do it.  I do know I need something along this line: (.?.+?)?(:/([0-9]+))?/?$  to ensure that a permalink will work properly in my plugin when a site owner chooses a permalink option in settings.  Right now the link /kudos/?kudoid=  but need to figure out how to make it change from post name to plain  and back when the permalink is changed in wordpress so the ?kudoid= will continue working.

Link to comment
Share on other sites

18 hours ago, SkyRanger said:

if I change it to http://127.0.0.1/?p=123 the ?urlid will not work

Of course it won't work - you're not passing 'urlid' in this case. You're passing 'p'.

 

6 hours ago, SkyRanger said:

was wondering if somebody could point me in a direction to learn more about rewrites

Did you follow the link I put in the first post to the codex rewrite rules page?

I think I'm still not getting what you're asking - even using post name as the permalink value in WP you can use URL parameters like normal. Just output the permalink, then append URL-encoded key/value pairs, then use them in $_GET.

Link to comment
Share on other sites

3 hours ago, maxxd said:

Did you follow the link I put in the first post to the codex rewrite rules page?

I think I'm still not getting what you're asking - even using post name as the permalink value in WP you can use URL parameters like normal. Just output the permalink, then append URL-encoded key/value pairs, then use them in $_GET. 

Hi maxxd yes I did thank you. Actually have it open and have been reading it over and starting to figure out the premises behind it.  Confused but slowly starting to understand it but.

This needs to go in to my plugin functions file and somehow this is suppose to make : href= ". get_permalink( get_page_by_path( '$kudopagename' ) )  . "?kudokey=" .$kudokey .">View Kudo work with any permalink chosen ie: plain

 

function custom_rewrite_kudos() {
  add_rewrite_rule('^kudos/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
  add_rewrite_tag('%kudokey%', '([^&]+)');
}
add_action('init', 'custom_rewrite_kudos');

 

Link to comment
Share on other sites

Look at the 'query_vars' and 'rewrite_rules_array' filter hooks as well - I've had varying degrees of success using either the add_rewrite_rules() function vs. just brute forcing the rewrite_rules_array array via those two hooks. I wish I could remember why, but it's late and it's been a long freaking week, so that information got purged. If I remember tomorrow I'll let you know, but in the meantime those two hooks are another way to go about skinning this particular cat that might be a bit easier to follow. Rewrites and routing can get confusing.

 

Link to comment
Share on other sites

Little farther ahead almost got it working but not sure how to get the var to work properly to only show the kudokey entry only and not the full list of kudos.

This is what I have so far:

add_action( 'init', 'add_kudoskey_rule' );
function add_kudoskey_rule() {
add_rewrite_rule(
    '^kudos/(.+)/?$',    
    'index.php?kudokey=$matches[1]',
    'top'
    );
}
add_filter('query_vars', 'kudos_plugin_query_vars');
function kudos_plugin_query_vars($vars) {
  $vars[] = 'kudokey';
  return $vars;
 }

Current URL:

echo '<a target=blank href=' .get_permalink( get_page_by_title( 'kudos' ) ). '?kudokey=' .$kudokey. '>View Kudo</a>';

http://127.0.0.1/?kudokey=JFMGTTQ5

That link will show the kudokey post but when I use the URL rewrite: this link http://127.0.0.1/?page_id=142?kudokey=JFMGTTQ5 it shows all entries.

Link to comment
Share on other sites

It looks like your rule is slightly malformed. As I said earlier, I've found the add_rewrite_rule() function to be a bit fiddly - I try to use the 'rewrite_rules_array' filter instead. Try this:

add_filter('add_rewrite_rules' ,'add_kudoskey_rule');
function add_kudoskey_rule(array $rules){
  $new = [
    'kudos/([^/]+)/?$' => 'index.php?pagename=kudos&kudokey=$matches[1]',
  ];
  $rules = array_merge($rules, $new);
  return $rules;
}

Now, with this in place you should be able to (after you've gone to the Permalinks page and clicked 'Save' to clear the rewrite cache) go to 'http://127.0.0.1/kudos/JFMGTTQ5' and it should show that specific kudos. If you want to get the kudos key referenced in the URL, use get_query_var('kudoskey').

If you want to show an archive listing of all kudos records, make sure that 'has_archive' is set to true in the arguments array of your custom post type registration call, then go to 'http://127.0.0.1/kudos' and you should see the listing.

The whole point of adding a rewrite rule is to enable pretty URLs and to not have to use '?variableName=variableValue' because it's easier for the user and better for SEO.

  • Thanks 1
Link to comment
Share on other sites

Thanks maxxd, except for the parse error under the $rules = array_merge thank you.  It looks like there may be an issue with my code in the end as I am unable to get it to work properly on my end.  More than likely it is something I have done so will just need to see what I have done wrong. Thank you for all your assistance thus far.

Link to comment
Share on other sites

Also, I'm assuming quite a few things about your project that may not be true. I'm assuming, for instance, that kudos is a custom post type and you're trying to show both an archive listing of kudoses and single kudos records both. I'm also assuming that you're not wanting to show a kudos specified by URL parameter on a random page (for instance, here's 1 kudos record on the 'about us' page!).

Each of these assumptions could very well change the overall approach; and even outside of all that, dealing with routing in WordPress is a convoluted and weird rabbit hole on it's own...

Link to comment
Share on other sites

Ah - OK. You can still use the WordPress WP_Query object (global $wpdb) to interact with the database, you'll just have to supply the table names yourself. Also, routing should still work as expected - if the correct rule is inserted in the rules array, you should be able to visit /kudos/JFMGTTQ5 and use get_query_var() to return 'JFMGTTQ5'. As far as an archive listing goes, I think you'll have to that yourself. Not trying to be nosy or pry, but is there a reason it's not set up as a custom post type?

Link to comment
Share on other sites

2 reasons. One is I am an idiot for not doing it that way and was too far in to the plugin to redo (possible next version if I can talk the company I work for to go that route).  They also wanted to keep the entries in a separate table for ease of transfer to another site later down the road (previous guy who wrote the kudos program back in 2014 for tham also last time WordPress was updated, provided no way to update so they wanted stand alone plugin that can also me modified down the road for use on a stand alone site if needed.

Link to comment
Share on other sites

Given the requirements from your company, I'd definitely argue that you're not an idiot. There are times when you have to just do what you've been asked to do, no questions. And yeah, if there's a chance that you'll be moving to a different CMS, extracting specific data from the posts table is difficult at best.

Link to comment
Share on other sites

All I can say they are happy with what is done so far and now I have another company thanks to an idiot friend...lol that would like a copy also which is the reason I am trying to get the permalink issue resolved.  Almost have them talked into  using the Post Name Permalink as right now they are using plain which is causing a nightmare with what I currently have coded.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.