19 Dec, 2011
If you have a relatively new installation of wordpress you will notice there are a bunch of meta tags in the source that are not that common to see. “Which ones do I need?” I thought. Well since I was using a wordpress page to run code that would generate dynamic content for multiple urls I knew I needed to get rid of the ones showing the actual page URL.
EditURI and Windows Live Writer
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://yoursite.com/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://yoursite.com/wp-includes/wlwmanifest.xml" />
EditURI will help client software discover your site. “wlwmanifest” sets up support for Windows Live Writer tagging. This maybe valuable to some of us and it didn’t really hurt my case so I left it. If you’d like to remove them, add these lines of code to your functions.php file in your theme:
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'rsd_link');
index, prev, next
<link rel='index' title='indextitle' href='http://yoursite.com/' />
<link rel='prev' title='prev_page_title' href='http://yoursite.com/prev/' />
<link rel='next' title='next_page_title' href='http://yoursite.com/next/' />
These are here to help explain to search engines how your page hierarchy is displayed. If you are running a straight up blog like ServerSideGuy.com, then this will probably work. But it seems like it trips up on pages. You should have a good sitemap.xml file with good prioritization so I really didn’t see the value here. I left the index as that will always be the same.
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
Note: Older wordpress versions use ‘adjacent_posts_rel_link’ as the action
Generator
This one I would recommend every wordpress user in removing. It’s there to tell search engines that this is a wordpress site. But this is only used in data gathering so WordPress can say they have X percent marketshare. Since it plainly displays the version of wordpress, I say it needs to go. You never know when the next exploit will be found and if its in the version you are using do you really want hackers to see that?
<meta name="generator" content="WordPress 3.2.1" />
Remove this with:
remove_action( 'wp_head', 'wp_generator');
15 Dec, 2011
I mentioned yesterday, I’ve been doing the first few problems of Project Euler and figured I’d share my solution to problem 7 here.
The Problem
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10,001st prime number?
The Solution
Let’s define a function isPrime(num) (granted, could be more efficient) and loop through the natural numbers, counting those that are prime.
/**
* Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
**/
#include
using namespace std;
bool isPrime ( unsigned long num ) {
for(int i = num - 1; i > 1; i--) {
if(num % i == 0) {
return false;
}
}
return true;
}
int main () {
unsigned long num = 2, primeCount = 0;
while ( primeCount < 10001 ) {
if ( isPrime (num) ) {
primeCount++;
cout << "Prime " << primeCount << " " << num << " is prime" << endl;
}
num++;
}
return(0);
}
14 Dec, 2011
I mentioned yesterday, I’ve been doing the first few problems of Project Euler and figured I’d share my solution to problem 6 here.
The Problem
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
The Solution
This one is pretty straight forward. Find a data structure capable of holding the square of the sum of numbers 1 to 100 and subtract the squares of the first 100 natural numbers.
#include
using namespace std;
int main () {
unsigned long sumsquares = 0, sums = 0;
int n;
cout <> n;
while (n < 0) {
cout <> n;
}
for(int i = 1; i <= n; i++) {
cout << "i: " << i << endl;
sumsquares += i * i;
cout << "sumsquares: " << sumsquares << endl;
sums += i;
cout << "sums: " << sums << endl;
}
cout << "The sum of squares from 1 to " << n << ": " << sumsquares << endl;
cout << "The square of sums from 1 to " << n << ": " << sums * sums << endl << endl;
cout << "The difference: " << (sums * sums - sumsquares) << endl;
return(0);
}
As you can see, there is a loop creating the sum and the sum of squares. From there, it subtracts the square of the sum and the sum of squares to get the answer.
13 Dec, 2011
I’ve been having some fun doing the first few problems of Project Euler and figured I’d share my solution to problem 5 here.
The Problem
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
The Solution
This problem, worded another way, asks what the least common multiple of all the integers between 1 and 20 is. Since multiplication is commutative, the least common multiple of a series is the same as computing the least common multiple of all the numbers before it and the current. So, LCM(1…6) is the same as LCM(6, LCM(5, LCM(4, LCM(3, LCM(2, 1))))).
#include
using namespace std;
unsigned long gcd(unsigned long a, unsigned long b) {
if(b == 0)
return a;
return gcd(b, a%b);
}
unsigned long lcm(unsigned long a, unsigned long b) {
return a * b / gcd(a,b);
}
int main ()
{
unsigned long answer = 1;
for(long i = 1; i <= 20; i++) {
cout << "i: " << i;
answer = lcm(answer, i);
cout << " lcm: " << answer << endl;
}
return 0;
}
My solution uses an algorithm to compute the least common multiple that involves finding the greatest common divisor as follows: lcm(a, b) = a*b / gcd(a, b). I used the Euclid method of finding the greatest common divisor: gcd(a,0) = a; gcd(a, b) = gcd(b, a mod b).
9 Dec, 2011
I ran into a problem setting up a php framework on a Mac Mini. Once I thought I had everything configured I found this error:
“Warning:mysql_connect(): [2002] No such file or directory”
I went and rechecked everything but without any results plus the error is not very descriptive of the problem.
After some search engine tech support, I realized this error is related to the mysql socket. It seems the OS can put the needed socket in different directories. In my case, it was in /tmp/mysql.sock while php was looking at /var/mysql/mysql.sock. I copied it to the path php was looking for and the error disappeared. This may happen the other way around where you’d need to copy the file to /tmp/mysql.sock but I am not sure what criteria gives what result.
8 Dec, 2011
I have a client who has a WordPress installation with thousands of pages. There are a couple things wrong with this, mainly relating to speed and editing practicality. It’s also an issue to find pages because they are ordered hierarchically. There are a few pages I modify fairly regularly and wanted a convenient way to find them. In order to do so, I needed to iterate through recently modified pages. Here’s what I came up with:
function recently_modified_dashboard_widget_function() {
global $wpdb;
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts
WHERE $wpdb->posts.post_type = 'page'
ORDER BY $wpdb->posts.post_modified DESC
LIMIT 0,25
";
echo "<ul>\n";
foreach($wpdb->get_results($querystr, OBJECT) as $post) {
echo '<li>'.edit_post_link( str_replace( site_url(), '', get_permalink( $post->ID ) ), '', '', $post->ID )."</a></li>\n";
}
echo "</ul>\n";
}
The query gets the last 25 posts of type page ordered by post_modified. From there, a foreach loop through the result yields individual posts in object form. The I used the post’s permalink, stripping off the site_url() as the text for the edit_post_link() text. All is well. I’ll soon publish the plugin which adds the list to the admin dashboard.
6 Dec, 2011
Having an anchor within a div that has the overflow attribute set to ‘hidden’ via applying the equal height column method creates a unique problem. When selecting an anchor, the columns get “chopped off” at the top as if the entire div moved up and is chopped because the overflow = hidden property of the container.
The culprit of this issue lies within the column that has the structure below:
padding-bottom: 2000em; /* X + padding-bottom */
margin-bottom: -2000em; /* X */
When the named anchor is clicked, the overflow:hidden atrribute and the above CSS structure kills the layout.
So if you need anchor tags in your site, avoid the equal height column method, there are other ways to achieve the same thing.
If interested, here’s some documentation on the problem. Click here
2 Dec, 2011
The E-Myth is a myth in the country that people start businesses because they are entrepreneurs. That is, they start businesses to take on risk in order to make capital. According to Michael Gerber, the real reason people start businesses is to get rid of their boss. Most people who start businesses are currently in a technical role. “Any idiot can run a business,” they think, it’s the technical work that needs to be done. So for those type of people, they start a business in order to not have a boss.
People who start a business have three distinct personalities: the entrepreneur, the manager and the technician.
The Entrepreneur is the dreamer, always thinking ahead, pushing the limits and leaving destruction in his path.
The Manager is the organizer, trailing behind the entrepreneur, struggling to keep up, organizing things in the entrepreneur’s wake.
The Technician is the doer, loves getting things done, if you want it done right, you have to do it yourself. The technician thinks of the entrepreneur as simply creating more work for him; and, thinks of the manager as the person holding him back from doing actual work.
If you put the three together, you end up with an incredibly capable individual. However, most people who start businesses are only 10% entrepreneur, 20% manager and 70% technician. This leads to the wrong person being at the helm – the technician is in charge.
30 Nov, 2011
Note, this may not always be the solution, but it is the solution to something that puzzled me for a while.
The div I wanted to have the drop-shadow was inside a table which had border-collapse set to ‘collapse’. Turning off the border collapse attribute made it work. For some reason having that set meant that the box shadow doesn’t draw.
Mischief Managed
27 Nov, 2011
I ran into a slow jQuery selector in older browsers on a large document and learned a cool tip: jQuery selectors should get more specific from left to right. Selectors are parsed in reverse order, so making sure that your most specific selector is on the right will increase performance.
The example below shows how not to write a jQuery selector using this technique:
$('p#intro b').each(function() {
//Do something
});
In the above example, jQuery finds all elements of type ‘b’ and then looks to see if the have a parent of p#intro.
Instead, it is recommended that you use the find function to speed up your jQuery. This took my 30,000 element document parsing time from 15 seconds to about 2:
$('p#intro').find('b').each(function() {
//Do something faster
});
This example does the opposite, finding a elements matching p#intro and looking in their children using the find() function to find all elements of type ‘b’.