We have finished migration to the new data center successfully and are working out some minor issues within the site's code. If you experience any difficulties or have any questions please email us at [email protected].
Ask your WordPress questions! Pay money and get answers fast! (more info)

This member is one of the top experts of all time, the true elite. John Cotton



Web developer based near Oxford, UK, specialising in Wordpress, jQuery, Bing/Google maps and external API integrations.

Answers Given: 400 (see them all)
Contests Won: 182
Prize Money Earned: $2987
Questions asked: 3 (see all of them)
Questions refunded: 0 (see all?)
Is a top asker (has the power to vote on anything):
Subscribes to all the discourse posted to the site?: No
Country: UK
City: Oxford
Netvotes this month (upvotes minus downvotes): 1
Netvotes of all time (upvotes minus downvotes): 25
Joined the site: October 4, 2010

John Cotton's upvoted answers:

John Cotton's downvoted answers:

Answers given to these questions:

See more?

Questions voted:

The community is grateful to those experts who take the time to vote on questions.

See more?

Questions asked:

See more?

This member has not made any recommendations. If you are one of the top experts, please offer one now!

John Cotton's discourse:

Written in response to Need Multipe Select2 Drop Downs:

$(document).ready(function() {
var options = {
placeholder: 'Search',
matcher: function(term, text) {
var has = true;
var words = term.toUpperCase().split(" ");

for (var i =0; i < words.length; i++){
var word = words[i];
has = has && (text.toUpperCase().indexOf(word) >= 0);

return has;

// Send the user to the category URL
$('#goto1, #goto2').select2( options ).on('change', function() {
var url = $(this).val();
window.location.href = url;

// Focus our search input
$('#goto1, #goto2').select2('focus');

would have been neater....


Written in response to Hello, Are you just need to add a custom class to tag like ...:

I downvoted as I think suggest to just edit the HTML body tag is bad advice.

Your follow up suggestion of flitering the body_class function (as I suggested )is the way to go. That way any other plugins or theme needing to vary the body class can do so too.

As it happens, the questioner is actually asking how to store a body class value for each page. ie They want a whole plugin!


Written in response to Custom Meta Box for Plugin:

It's really disturbing when people tell you "it's a really quick fix".

If it's so quick why doesn't the guy do it himself? Presumably because he doesn't know how to. And if he doesn't, how can he know it's quick?

More to the point, someone who's able to do these things quickly has spent a lot of time learning and developing their skills to do so. And they deserve more than a dollar for that!

I hope anyone who's able to do this, holds out for more than $1.


siouxfan45, siouxfan45 had responses to this.

Written in response to How to display custom fields for my custom taxonomy:

Good that a solution has been found.

However, get_metadata supports custom taxonomies just like it supports posts and users and comments.

In this case, a call like this would have return meta in the normal way:

$meta = get_metadata( 'authors', $term_id, 'my_meta_field' );

In fact, all meta functions are support for custom taxonomies. The only thing that needs to be done is the creation of a meta table in the database with the name wp_authorsmeta (copying the structure from any of the other meta tables). Something along these lines:

global $wpdb;

$taxonomy_name = 'authors';

$sql = "CREATE TABLE {$wpdb->prefix}{$taxonomy_name}meta (
meta_id bigint(20) NOT NULL AUTO_INCREMENT,
{$taxonomy_name}_id bigint(20) NOT NULL,
meta_key varchar(255) NULL,
meta_value longtext NULL,
PRIMARY KEY id (meta_id)) $collate;";


It is odd - and a bit annoying - that WP doesn't do this or, at least, support creation somewhere in the code.


Fahad Murtaza, Fahad Murtaza had responses to this.

Written in response to How to redirect to custom page after subscription ?:

Do you know, I voted on this (to match the asker's vote - why do we always do that??) and then I regretted it.

The accepted answers both recommend changes to the plugin code - and that's bad.

What happens when the plugin updates? Your site breaks. That's what happens.

So the principle of the answer might have been correct (set window.location on ajax success) but editing the plugin files to do that is WRONG, WRONG, WRONG.

What should they have done?

I'd have added a callback to success using the global ajax event, checked the data property to see if it was the ajax sent from the subscribe submit and then fired my window location code there. 4 or 5 lines, in my own js file, doing exactly the same thing and not going anywhere near someone's plugin code.

So can I take my votes back ?



kegiaumat055 had responses to this.

Written in response to Help w/ jQuery scripts & wp_enqueue_script:

A couple of points here:

1/ When deregistering/registering jquery to google CDN (a good thing!) it's good to do it with a protocol-agnostic URL since that gives you greater flexibility for a future switch to https.

2/ If you intend to enqueue immediately (not always a good idea - do you need the script on every page??) then you can skip the wp_register call and just enqueue - we all like to save a line of code!!



Just looking at the new "My Account" page...hmmm, think I preferred the old version Lawrence :)


Written in response to Correct way for shortcode to show up after:


I don't understand how people can want to create websites and yet not want to understand how the code works. Just cutting and pasting someone else's code will never advance your knowledge.

The chosen answer does almost what I described (I disagree that it is the "correct" way - the code uses output buffering which will nearly always perform less efficiently than string concatenation and so probably is best left to where code readability is of a much higher priority) and so we can be happy that the question has a solution.

But is he any the wiser?


Dbranes, Dbranes had responses to this.

Written in response to Access to CPT for Subscribers:

The addition (or removal) of roles and capabilities updates the wp_options table and so doesn't need to be run on each page access.

So admin_init is not the best hook to use here (worse since until someone accessed the admin area the code would not take effect). The best hook to use is "switch_theme" as it only gets run once when the theme is first activated. Of course, if the code is an amendment to an existing theme, you'd need to switch away and then back but I think that is a small price to pay instead of updating the database EVERY time someone makes a call to an admin page.


Written in response to Sent you request on skype. ...:

I can understand that some problems are best solved between the questioner and the answer, but it would be good if one or other came back to wpquestions and gave a summary of the problem and the solution. That way others will learn and problems could be solved more quickly in future.


Written in response to Display the_content for latest post and the_excerpt for rest:

Jurre has a good point.

There are solutions that give you the result you want, and there are good solutions that give you the result you want.

With people using plugins all over the place (and poorly written ones at that), WordPress can perform sluggishly due to multiple database queries.

The best answer isn't always the one that queries the database the fewest times, but it's often a good starting place!


Written in response to Theme localization in a query...:

Although Francisco's solution fixed Josh's problem, his answer is technically incorrect.

There is no need to assign the return value of __() to a variable prior to use - the return value can be used like any return value from any other function.

The problem with Josh's code was the incorrect use of concatenation within his parameters.

The solution I offered:

'category_name'    	=>  __('Our Friends', 'mytheme'),

would have fixed that without the (albeit minor) overhead of creating a new variable that only gets used once.


Written in response to How to get a similar structure?:

For those interested, here is what BadNews got in the end:

A custom post type of game_post with custom taxonomies of game_ratings, game_genres, game_publishers and game_developers. The custom post type wasn't completely necessary, but made things clearer in the queries, kept the custom taxonomies out of other post types and enabled the use of single-game_post.php theme file, making cleaner code.

(Standard) Post categories of cheats, hints, unlockables and Easter Eggs were created.

The "glue" was another custom taxonomy called game_name in which each game_post had a matching entry. This taxonomy was applied to both the game_post and each related standard post (ie cheats, hints etc).

Then in single-game_post.php this function:

function get_related_posts( $category, $slug ) {

$args = array( 'tax_query' => array(
'relation' => 'AND',
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( $category )
'taxonomy' => 'game_name',
'field' => 'slug',
'terms' => array( $slug )

$the_query = new WP_Query( $args );

if( $the_query->post_count > 0 ) {
// loop through and output as desired


could be called by this code:

// Get the 'glue' game_name taxonomy
$game = wp_get_object_terms( $post_id, 'game_name');

// Use the glue to retrieve related posts
get_related_posts( 'unlockable', $game[0]->slug );
get_related_posts( 'cheat', $game[0]->slug );
get_related_posts( 'hint', $game[0]->slug );


Lawrence Krubner had responses to this.

Written in response to Custom Wordpress Search Results Page (Only comments, not posts):

Edit the search.php file in your theme to replace the existing posts loop with something like this:

$comments = $wpdb->get_results("SELECT * FROM {$wpdb->comments} WHERE comment_content LIKE '%{$s}%'");

if($comments) {
foreach($comments as $comment) {
echo '<h1>'.$comment->comment_content.'</h1>';

The SELECT query could be made much more detailed of course and the output would need to be more sophisticated (you'll get the ID for the post back for example so you could easily link to the original post), but hopefully you get the idea..



See more?

John Cotton has not had any questions refunded

John Cotton suggested these prizes as appropriate for these questions:

See more?