惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
C
Cisco Blogs
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
V2EX - 技术
V2EX - 技术
H
Heimdal Security Blog
S
Security Affairs
L
Lohrmann on Cybersecurity
Hacker News - Newest:
Hacker News - Newest: "LLM"
Simon Willison's Weblog
Simon Willison's Weblog
WordPress大学
WordPress大学
小众软件
小众软件
Security Latest
Security Latest
AWS News Blog
AWS News Blog
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
F
Full Disclosure
S
Schneier on Security
L
LangChain Blog
MyScale Blog
MyScale Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
Google Online Security Blog
Google Online Security Blog
Scott Helme
Scott Helme
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
A
Arctic Wolf
Martin Fowler
Martin Fowler
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
The Register - Security
The Register - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
Latest news
Latest news
F
Fortinet All Blogs
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
Hacker News: Ask HN
Hacker News: Ask HN

Chip Cullen

The need for importance, and AI: Chip Cullen An updated Colorosetta: Chip Cullen The Return of the Font Combinator!: Chip Cullen Changing the number of an item in an ordered list: Chip Cullen My pizza dough recipe as of May 2025: Chip Cullen Gonna try to be a bit more personal: Chip Cullen How I built dynamic social media images in Eleventy using Cloudinary: Chip Cullen My current approach to AI : Chip Cullen Lessons Learned Surviving a Major Product Launch: Chip Cullen How to Build a Drop Down Menu with Modern CSS: Chip Cullen How to stop page scrolling when you have an open dialog element: Chip Cullen Distraction Driven Development: Chip Cullen How I learned to code: the art of letting go: Chip Cullen In praise of the switch statement: Chip Cullen Project stuck? Think about how you’re breaking it down & question everything: Chip Cullen So how did the onboarding experiment go?: Chip Cullen Ideas for an Onboarding Checklist: Chip Cullen I really like Post Mortems: Chip Cullen Raise Red Flags Early: Chip Cullen How to mock fetch requests in React Testing Librarty tests: Chip Cullen Running a Structured Meeting: Chip Cullen Adding the View Transitions API to my personal site: Chip Cullen A Lightweight Way to Read GraphQL Data: Chip Cullen How to make a color changing favicon: Chip Cullen Using a Pros/Cons list to help navigate technical discussions: Chip Cullen How to use variable fonts from Google Fonts: Chip Cullen A new website: now on Eleventy!: Chip Cullen How to Truncate Type at More Than One Line with Just CSS: Chip Cullen Colorosetta: the VS Code Extension!: Chip Cullen Using CSS Custom Properties and Logical Properties Together: Chip Cullen Browser Dev Tools: Element Inspector Popover: Chip Cullen The Link with rel=preload is a Seperate Thing: Chip Cullen How to have Dark & Light Mode Images that also works with User Choice: Chip Cullen Don’t use Viewport Units for Font Size on their own: Chip Cullen A little known Media Query: Aspect Ratio: Chip Cullen Meta thinking: Managing Decisions: Chip Cullen Give Your To-Do's Context: Chip Cullen Say What the Impact is when Reporting Issues: Chip Cullen Firefighting 101: How to Manage Breakages: Chip Cullen How to Deal With Large Pieces of Technical Debt: Chip Cullen Make Your Request Clear: Chip Cullen Analytics events, HTML classes, and protecting against refactoring: Chip Cullen How We Removed jQuery from a large app: Chip Cullen New tool: ColoRosetta: Chip Cullen What width and height attributes should you use with responsive images?: Chip Cullen Django 3.1 gotcha: Referrer Policy has a new default, and it might break iframes and links: Chip Cullen A Javascript Component Pattern: Chip Cullen CSS min(), max() and clamp() Functions: Chip Cullen Pointer Events and Inline Elements in Chrome: Chip Cullen Resolving a github repo and a new Create React App: Chip Cullen How to POST *Data* with the Fetch API: Chip Cullen The Contrast Triangle: Chip Cullen Advice on interviewing for Junior Developers: Chip Cullen Life Lessons Learned From Running a Marathon: How to do something really hard: Chip Cullen A (Brief) intro to Search Engine Structured Data: Chip Cullen Javascript Fallback Values on Variables and Booleans - a hard lesson: Chip Cullen Alfred Tip: Quickly Access Common URLs: Chip Cullen Responsive Images in Hugo - by Laura Kalbag: Chip Cullen Making a Gatsby Site with Multiple Content Types: Chip Cullen How to Create and Use Fixtures in Cypress Tests: Chip Cullen Fixing the 'Bad Interpreter' Error from AWS and Python 3.7: Chip Cullen Creating a Canonical Tag in a Django Template: Chip Cullen Responsive spacing with viewport and ch units: Chip Cullen Welcome to my New Design - 2019: Chip Cullen Django Templates: Block and If statements don’t work like you might expect: Chip Cullen Books I Read in 2018: Chip Cullen Lifehack: 4 ways to help tame common email noise: Chip Cullen How to make better Pull Requests: Adding Steps to Test: Chip Cullen The unsung develpment tool: Spreadsheets: Chip Cullen Troubleshooting Adding and Removing EventListeners: with Arguments, Debounced, and in a React Class: Chip Cullen How to Fake the Window Object in Jest and Enzyme: Chip Cullen Migrating From Wordpress to Hugo: Chip Cullen Background Repeat and its Possibilities: Chip Cullen Getting Started With Front End Tests: a Mindset: Chip Cullen Migrating a Blog - An Opportunity for a Content Inventory: Chip Cullen Moving to Hugo: Chip Cullen JavaScript events: .target vs .currentTarget: Chip Cullen Things I wish I knew when starting with Python: Chip Cullen Leading Ampersands for modifiers in Sass: An anti-pattern: Chip Cullen How to get rid of the "You have mail" message in your terminal: Chip Cullen Why three typefaces rule the web, and what you can do about it: Chip Cullen You shouldn't worry about Section 508 - it's Section 504: Chip Cullen Looping Video Backgrounds: pointers and pitfalls: Chip Cullen How to “preview” a click event tag in the Google Tag Manager console: Chip Cullen Moving on from a technology, or: life after Drupal: Chip Cullen Don’t be a dumb developer: Chip Cullen Two level breadcrumbs with CSS :only-child: Chip Cullen Simplicity comes with experience: Chip Cullen Do the least amount possible: Chip Cullen SVGs vs. Icon Fonts: Two points in favor of Icon Fonts: Chip Cullen Accessible links without underlines: Chip Cullen The Strategic Job Hunt: Chip Cullen Surviving Getting Laid Off: Chip Cullen How to structure your typography in Sass: Chip Cullen Layer Cake: A Responsive Design Layout Pattern: Chip Cullen Creativity is yet to come in Web Design: Chip Cullen Front End Testing with Wraith: A Step by Step Recipe: Chip Cullen Where to begin? How I start a visual design for the web: Chip Cullen If you could only have five Google Fonts: Chip Cullen Why SVG is so cool (or: what happens when you're late to the party on something): Chip Cullen
Altering the comment form in Wordpress: Chip Cullen
' . '' . __( 'Your Name' ) . ' · 2012-04-04 · via Chip Cullen

Intro

Recently a client wanted to change the comment form on a Wordpress site I was building for them. Specifically, they just wanted to have a Name field and the Comment field - omitting the E-mail and Website fields. They also wanted their labels to have particular wording.

I had never done this before, and after a lot of googling, I figured it out. I didn’t feel that a lot of the tutorials out there provided enough information, so I’m hoping to rectify that problem with this post.

Assumptions

I’m going to assume that you are a developer type who is setting up a Wordpress site. These techniques work on Wordpress version 3 and up. In my examples, though, I’ll be writing to the current release at the time of this post - 3.3.1. I am going to assume that you are familiar with how a theme is constructed, and that you aren't scared by a little PHP. It’s really not that bad.

What I’m not going to cover

I’m not going to go into how to add pieces to the comment form that aren't already there - e.g. adding another field or button. That involves intercepting the form and making sure the database can handle it - and frankly is above my head at the moment.

Context

These techniques all involve files that are found in your theme. While you could hack Wordpress core, that is highly inadvisable. Every time you updated your Wordpress install, you’d have to redo your changes.

I’m going to discuss two techniques, and each affects a particular file. The first technique involves comments.php, which is found in most themes. A theme doesn’t necessarily have a comments.php file, but it is generally considered best practice to do so.

The second technique involves changes to functions.php, which again is found in most themes. Any real logic that happens in your theme tends to be found in a theme’s functions.php.

Brief historical interlude

The comment form in Wordpress used to be a fairly straightforward affair, but a messy one. Previous to version 3, the entire comment form, markup and everything, lived in a theme’s comments.php file. While this made it a snap to alter the comment form, it resulted in a big hairy ball of code living in a theme.

In Wordpress 3, there was an effort to abstract this functionality, resulting in one simple call that would be needed in your theme:

This one line will produce the comment form that we all know and love. Which is, ultimately, a good thing. 96.429% of Wordpress sites out there (I asked) use the default comment form. There is no reason to have that big mess living in the theme of the vast majority of sites.

However, if you do want to alter the comment form, it’s now a little more tricky than simply locating the markup you want to change.

A tale of two methods

There are two ways to go about altering your comment form, as I’ve mentioned. Which method you choose depends on what you want to accomplish.

Please note: Method 2 will trump Method 1, so choose wisely

Method 1: Altering the comment_form() call in comments.php

This method is good for

  • Altering all of the details about the comment form
  • Changing the markup of the fields, including labels

This method basically entails taking the normal comment form call:

And expanding it with new details about how you want the form to be generated. If you want to change some of the details, you need to specify those changes with an array that will be the argument for the function. Like so:

<?php comment_form(array()); >

You put your changes in that array:

'value', 'another_parameter' => 'value' )); ?>

What parameters can you set? Pretty much anything to do with the comment form. Take a look at the Wordpress Codex entry on comment_form(), which gives you a run down of the parameters that you can set, and their default values (which Wordpress will use unless you specify otherwise).

Want to change the explanatory text after the comment field to have a link to a privacy policy, instead of the list of allowed HTML? No problem:

'

Please see our privacy policy.

' )); ?>

If you need to set more than one parameter, but sure to include a comma at the end of each parameter, except the last one.

If you want to change the fields of the comment form, that takes a little bit more effort. You will need to create a fields array (I’m going to call it $fields) within the array you just created, and that second array will be called by the 'fields' parameter. So we will start out with that new array:

'

' ); )); ?>

This has redefined how I want the “author” field to be generated. In this case, I’ve changed the label text to say “Your Name” and the initial value of the field itself. Now I need to call that field within the parameters:

<?php comment_form(array( $fields = array( 'author' => '

' );

'fields' => apply_filters( 'comment_form_default_fields', $fields ), 'comment_notes_after' => '

Please see our privacy policy.

' )); ?>

This piece:

apply_filters( 'comment_form_default_fields',

Tells Wordpress you’re going to change the fields of the comment form, then, you just supply in the name of your array ($fields, in this case). You can change each of the fields using this array technique - author, email, website and comment.

This whole comment_form() function now goes in your comments.php file at the point where you want the comment form to appear - which is usually after the listing of existing comments.

Method 2: Using filters in functions.php

This method is good for

  • Omitting or changing fields
  • If you want the nuclear option, and never want to hear from those fields again

The second method employs Wordpress filters. Using a filter, you can override the comment form before final output.

You will want to add this filter to your theme’s functions.php - which most themes will have. If your theme doesn't have a functions.php file, simply create one and make sure it opens with

<?php

Next you will want to create a function that defines your changes. As far as I’m aware, you can only modify the fields of the comment form with this method. This is an example function:

function alter_comment_form_fields($fields){ $fields['email'] = ''; //removes email field $fields['url'] = ''; //removes website field return $fields; }

The name of the function can be whatever you want - it will be called later. This example function will remove the email and website fields from the comment form. Want to change the author field as well?

function alter_comment_form_fields($fields){ $fields['author'] = '

'; $fields['email'] = ''; //removes email field $fields['url'] = ''; //removes website field

return $fields; }

Keep in mind that if you want to modify a field, you have to include everything that is part of the field - label and input. In this example, I’m changing the label to say “Your name, please”, and I’m modifying the input to have a placeholder attribute.

Okay, you’ve made the modifications, now you have to call this function. You do that with a filter - in this case, one that Wordpress has built in and understands - it’s called “comment_form_default_fields”:

function alter_comment_form_fields($fields){ $fields['author'] = '

'; $fields['email'] = ''; //removes email field $fields['url'] = ''; //removes website field return $fields; }

add_filter('comment_form_default_fields','alter_comment_form_fields');

So you take that function and filter call, put it in your functions.php file, and give it a whirl. This won’t affect where on the page your comment form appears, but rather the fields within it. The placement of the form is still controlled by your comments.php file, and where you put the comment_form() call.

Again, bear in mind that the filter method will override whatever you do in comments.php.

As an aside, I like to keep this snippet around so I can drop any of the fields quickly -

function alter_comment_form_fields($fields){ //$fields['author'] = ''; //removes name field //$fields['email'] = ''; //removes email field //$fields['url'] = ''; //removes website field return $fields; }

add_filter('comment_form_default_fields','alter_comment_form_fields');

Just put that in your functions.php and uncomment the line with the field you want to omit. Boom. Done.

Conclusion

I hope this post will help you make changes to your Wordpress comment form. I’ve tried to be comprehensive and provide context, so sorry it’s a little long. If you use these techniques - I’d love to hear about it in the comments.