TUTORIAL Removing querystrings from Answer links

4.96K viewsCore
1

WARNING: this will break things if your question have more than one page of answers!!! Do not use in production!

By default, you get links like http://localhost/question/example/?show_answer=201#answer_201. This is bad for many reasons:

  • caching plugins may fail
  • page getting reloaded instead of just jumping around if clicked
  • default answers order getting mixed (especially bad if you, like me, are using “oldest first” to make it look like a forum)
  • some possible SEO issues

The only benefit of such querystring, is reordering of answers (I consider it a bad thing, but some may like it)

To remove querystrings, leaving just the hashtag part, use snippet:

add_filter('ap_answer_post_type_link', 'd_ap_answer_post_type_link', 10, 2);
function d_ap_answer_post_type_link($link, $post) {
	if ($post->post_type == 'answer' && $post->post_parent != 0) {
		$link = get_permalink($post->post_parent) . "#answer_$post->ID";
	}

	return $link;
}

Now it will be http://localhost/question/example/#answer_201.

 

I also highly recommend using CSS like the one I use:

:target {
    background-color: rgba(34, 168, 216, 0.1) !important;
    border: 3px dashed #22A8D8 !important;
}
.target-id-fullwidth:target {
    padding-left: 20px;
}
#answers .type-answer:target {
    padding: 12px;
    margin-left: -15px;
    margin-right: -15px;
}

Example usage: http://dima.stefantsov.com/q/prisedaniya-dlya-mh-v-myshtsah/#answer_2117

commented

show_answer string is required when there are pagination in answers. It will show answer user wants to see in top if is pagination.

Pagination inside of a question never happened to me. Thanks for letting me know. Need to think of a better way to generate urls, getting page number into it…

I’ll go with this for now: http://4.stefantsov.com/s/?2015.12.12_17.44.23_lh0jnh56.png
No pagination – no problems.

Hey @Dima, thanks for this beautiful trick ! In which PHP file should I add this snippet though ?

Ok nevermind I found it, I put it in functions.php and it works 🙂

0

@Dima, yes, I see what you mean.

Its even worse than that on my site. For example, here’s an answer link on my site ~ http://wpexpert.support/questions/question/test-question/?show_answer=1813#answer_1813

With the snippet I get ~ http://wpexpert.support/questions/question/test-question/#answer_1813

A lot better, but not exactly ideal ~ which is not your fault, of course.

And why do you “highly recommend using CSS like the one I use”?

What’s the thinking behind it?

Answer ID in url will be highlighted as stackoverflow does.

You could change your urls like I did: exclude “questions” for question url, shorten question and questions slugs. It can be done from AnsPress Options.
Result: http://dima.stefantsov.com/q/prisedaniya-dlya-mh-v-myshtsah/#answer_2117

With highlighting like I propose (blue in my example, click the link and see yourself), it’s easy for visitors to understand what I wanted them to see. Imagine 100 answers, and a link to answer 37. Reader would not be sure he looks at what he should. Without some strong visual clue, like that blue box.

There are other ways, JS one like it’s implemented in here, flashing than fading away, but I like permanent one more.

@Dima ~ I am not a developer, so the constant hacking of code and using unreleased versions is a pain in the ass, as far as I am concerned. And the average user isn’t a developer either, so if it [AnsPress/AskBug] keeps on going in this direction and not settling down to be a more mature product, I think its going to alienate a lot of current users..

Show me a “more mature product”, I’ll switch today. As far as I know, this is the best you get.

You don’t have to tinker, this is just me, trying to make it even more perfect. Someone must do it. How do you think product becomes “mature”? By all the users waiting?

I have not changed a single template, running my production site on default anspress and pretty happy with it. It’s GREAT experience out of the box. It just works.

Terence, I agree with that, my users arn’t even getting notifications at all.

I guess everybody has a different pain threshold and personal experience tells us by empirical judgement what is good or bad, what is improving or getting worse, and the rate of change. So therefore, I guess its only logical we are all bound to disagree, at times, even though our overall aim is the same.

My irritation is primarily that you can see it working on this site but not on my own?! Yet they are ‘apparently’ using the same core code.

@Matt, ~ yes, but that’s been the case from day one. In fact it is with many, but not all themes. What is particularly irksome here is the rate of change. What’s broken, is fixed, but only on Github, not in the current release version. Its like its in a perpetual state of beta release. I think it suits the author and one or two other developers in here, but I just don’t think it is very professional way to carry on a business

Terence do you mind if I use your setup to test the email system? 🙂 Just want to register two accounts and see if it works on yours.

@Matt ~ Go for it. If it breaks you own both halves.