NodeJS: How to kill process after stopping with CTRL+Z
I've made the mistake of killing a node.js process using CTRL+Z instead of CTRL+C enough times that I figured it'd be a good idea to keep the fix handy...
From the Mac terminal (or Linux command line), type:
$ lsof -i tcp:1234
Replace 1234 with the port number that your node application was listening on.
Then use this command to kill the process:
$ kill -9 <PID>
Replace <PID> with the PID of the running process, which may have a name like:
I recently began coding in Java -- my first compiled language -- and really digging into object-oriented programming. This made me rethink the way I write plugins for WordPress, because I see the benefits of working with objects. In particular, doing so decouples your code and makes it easier to test, modify and debug.
With that in mind, I started using an autoloader function in my plugins. An autoloader, if you are unfamiliar, is a function that attempts to automatically include a class file when an undefined class is instantiated.
CAUTION: In WordPress, you can't just create a function called __autoload(), because that would overwrite the WP core autoload function. Instead, you have to register your autoloader using spl_autoload_register(). That's my understanding, anyway...please correct me if I'm wrong.
Anyhow, I wrote this plugin class autoloader in a sample plugin so I could share it, in the hope that it would be helpful to someone out there.
In order to keep the code clean and modular, I also created a separate function called 'wp_classname_to_filename()' that accepts a class name and converts it to a properly-formatted filename according to WordPress naming conventions.
Naming functions with the 'wp_' prefix is generally considered a no-no, but my hope is that this function will someday be added to WP core...and I can't imagine a different core function ever being given that name!
In case 'wp_classname_to_filename()' is added to core someday, I've written it in a separate file and only require that file if the function does not already exist.
Do you have an opinion on any of this? I'd love to hear your thoughts...
Twitter Bootstrap: How To Prevent Jump When Tabs Are Clicked
Recently, I set up a website using Twitter Bootstrap. On certain pages, there were vertical tabs along the side of the content. Unfortunately, when you clicked on a tab, the page would jump. This is because the tab uses an href value equal to the ID of the tab content.
The Difference Between get_user_meta() and get_userdata()
If you've ever tried to create a user profile template or user directory template in WordPress, you've probably come across one (or both) of these functions: get_userdata() and get_user_meta().
These two functions seem similar, so anyone who is not familiar with the WordPress database might get confused. To clarify, WordPress stores user data in two tables: 'user' and 'usermeta'.
In the 'user' table, the following values are stored:
ID
user_login
user_pass
user_nicename
user_email
user_url
user_registered
display_name
In the 'usermeta' table, these values are stored (along with any custom fields that are added):
user_firstname
user_lastname
nickname
user_description
wp_capabilities (array)
admin_color (Theme of your admin page. Default is fresh.)
I've been developing WordPress themes and plugins for a while now, and I'm confident enough in my PHP skills to offer help to beginners who need it. Today, I received an email from someone who I have been showing the ropes. In his email, he asked:
I know this line is wrong: echo '<li class="' . $is_active . '"><a href="#tab"' . $i . '">data-toggle="tab">."' . get_avatar( $administrator_info->ID ) . '"</a></li>'; However, I'm not sure why exactly. Any ideas?
I ended up writing a longer response than I had intended. The advice I gave seemed pretty useful, so I decided to share it with the rest of the world.
My response was this:
Here's a neat little trick when echoing a largely-concatenated string: break it across multiple lines, like this:
echo
'<li class="'
. $is_active
. '"><a href="#tab"'
. $i
. '">data-toggle="tab">."'
. get_avatar( $administrator_info->ID )
. '"</a></li>'
;
In doing so, you can more easily see each part of the string and find your errors. Your string should look like this:
echo
'<li class="'
. $is_active
. '"><a href="#tab'
. $i
. '" data-toggle="tab">'
. get_avatar( $administrator_info->ID )
. '</a></li>'
;
Another way to write the same thing is to use the PHP functions printf() and sprintf(). The latter returns the concatenated string for use as a variable; if you just want to echo to the screen, you would use printf().
Here is how your code would look using that function:
printf(
'<li class="%1$s"><a href="#tab%2$s" data-toggle="tab">%3$s</a></li>',
$is_active,
$i,
get_avatar( $administrator_info->ID )
);
The first parameter of that function is the string, with variables like %1$s, %2$s and %3$s mixed right in with the HTML. The next three parameters define those variables, in numeric order. So %1$s is replaced by the string value of $is_active, and so on.
But perhaps the BEST way to solve this problem is to use an editor with good syntax highlighting, like PHP Storm. The dark background gives good contrast and it highlights or underlines errors making them easy to spot.
In PHP Storm, the code looks like this:
The person I wrote this for found it helpful; hopefully you will too!
Tonight, I had the pleasure of meeting Brad Touesnard at the Boston WordPress Meetup and learning about his awesome plugin, WP Migrate DB. A few hours later, this little gem saved me a ton of time when I was launching a new website for one of my clients.
If you ever need to move a WordPress installation to a different domain name, do yourself a favor and download this plugin. There is a premium version with some neat features, but even the free version handles finding & replacing your URLs and file paths very well.
Serialized data? No problem here. Widgets and menus and theme options all stayed in tact!
Quality-crafted web hosting: top site speed, unmatched security, 24/7 fast support! Trusted by 250,000 domains.
I have been using SiteGround as a web host for six years now, and I can honestly say they are the best low-cost web hosting service out there. I've dealt with a number of other hosting service providers for clients, but none of them has ever come close.
If you're looking for an affordable hosting solution that can scale as your website grows, definitely check out SiteGround. If you decide to use them, please use my affiliate link above. :-)
So anyone who has ever trolled the WordPress support forum has seen more than one moderator or seasoned community member preach the value of using Child Themes to some n00b...and I'm willing to bet that sound advice is ignored (or at least not acted upon) 9 out of 10 times.
Which begs the question: Why do many folks who are new to WordPress resist using Child Themes?
Well, I have an anecdotal answer to that very question, based on my own newbie experiences many moons ago...
The main reason I avoided using a Child Theme when I was getting started was that I had no stinking clue how to create a PHP or CSS file! I didn't realize you could just use a text editor like Notepad...and I certainly hadn't heard of Notepad++.
Second, when I did learn how to create one of those files I had to clear the learning curve of uploading files to the correct folders on my web server. I didn't fully understand the folder structure for WordPress, which can be a little confusing for beginners.
And finally -- and I think this one applies to most WP novices -- I didn't like flying blind. When you're editing your theme from WP-Admin > Appearance > Editor (and all newbies do it!) a child theme looks pretty bare. "Where is my index.php?" "How do I edit the parent theme CSS?"
Of course, those were just my reasons for resisting Child Themes when I was getting started. I'm curious to hear what other reasons folks might have...or how we work to improve the experience...or if I'm completely wrong and nobody is resisting using Child Themes! :-)
Add this little code snippet to your archive.php or search.php, just after `if ( have_posts() )` to show a results count heading. - Gist is a simple way to share snippets of text and code with others.
Okay, maybe that title is a little too clever. If you’re not a nerd, you will be disappointed to find that this post is about databases and referential integrity. But if you are a nerd, or aspire to be one, keep reading…
This is a diagram of the WP 3.0 database, which you can also find here: http://codex.wordpress.org/File:WP3.0-ERD.png
Notice how some of the lines between the database tables are solid, while others are dashed? The solid lines indicate that the database management system enforces “referential integrity”…meaning you cannot add a row to one table unless it refers to an entry that exists in the other table.
Enforcing referential integrity also causes a cascading effect when rows are changed or deleted. This is relevant to the diagram above, because it answers the question: “Why does WordPress enforce referential integrity for some table relationships and not others?”
The tables wp_posts, wp_links, and wp_term_taxonomy each have a one-to-many relationship with the table wp_term_relationships. The relationship table associates a post or link with a taxonomy term, establishing a relationship. If a post or link is deleted, the cascading effect will delete all relationships to various taxonomy terms, preventing ‘orphaned data’ from cluttering up the database.
You wouldn’t want the same sort of thing to happen to a post, for example, after a user who created it is deleted — because that might be an unintended or undesired consequence. Instead, WordPress keeps the post and allows you to assign a different user as the author.
A sentence should contain no unnecessary words, a paragraph no unnecessary sentences, for the same reason that a drawing should have no unnecessary lines and a machine no unnecessary parts. This requires not that the writer make all his sentences short, or that he avoid all detail and treat his subjects only in outline, but that every word tell.
The Elements of Style by William Strunk Jr. & E. B. White
In my ongoing quest to become a hardcore developer, I had an awesome session yesterday with Ben Doherty at Oomph, a web marketing firm here in Boston. Ben helped me wrap my head around recursion -- creating a function that calls itself -- by asking me to write a PHP function that would calculate the factorial of a given number ($n).
It took me a few minutes, mainly because I've never studied trigonometry or learned what a factorial was! We were working on a whiteboard, which made it even more difficult...but on my second attempt I got it to work. Then I asked Ben how he would write it and, as an added bonus, he showed me a bit of PHP shorthand that reduced the code to a single line.
Here is the code, with a simple HTML form added so you can actually use it: http://pastebin.com/9436TUYk