Programmatically Create a Post in WordPress

If you're an advanced developer or just want the TL;DR version of this, skip to the code.

Create a Post in WordPress

For many, creating WordPress posts and pages is a simple matter of using the built-in editor. But if you’re working on a more advanced project – perhaps you need to automatically generate pages (or posts) to act as views or to restrict users from accessing the dashboard.

If that’s the case, then it may be better to programmatically create a post in WordPress.

Case in point: I’ve been working on a project where all user profile management (save for administrators) has to occur outside of the dashboard. This means that when the theme is installed, it needs to automatically generate pages that support this functionality.

It’s not terribly difficult, but it’s important to make sure that you aren’t overwriting existing pages, that you’re properly setting post attributes, and that you’re properly assigning templates when necessary.

Here’s a simple example of how to programmatically create a post in WordPress.

Continue reading “Programmatically Create a Post in WordPress”

Programmatically Create a User in WordPress

If you're an advanced developer or just want the TL;DR version of this, skip to the code.

When it comes to building a certain type of web app, I obviously think that WordPress is a viable option. Out of all of the contract work I’ve done in the past couple of years, about half of the projects have required some form of user management.

That is, they usually want users to create their own accounts, set a few fields such as their name and email address, and then be emailed when all is setup.

Out of the box, WordPress offers easy user management through its dashboard and if you’re running some type of blog or editorial site, there’s no need to deviate. But if you’re building an application, there are alternative ways to handle users.

Case in point: say that designer has created a solid look and feel for the site. If you force users to use the built-in dashboard to create or manage their profiles, then you’re yanking them out of the site, dropping them into some new experience, and then returning them to the site from which they came.

Lame, right?

There are better ways to introduce user registration and profile management into an app built on WordPress. Though your mileage may vary based on the needs of you project, here’s a how you can programmatically create a user in WordPress.

Continue reading “Programmatically Create a User in WordPress”

Add a Separator To The WordPress Menu

As far as the WordPress community is concerned, developers tend to be divided on how plugins and themes should insert themselves into the WordPress menu.

On one hand, some feel that because WordPress offers the ability to insert custom menu items into existing menus, then plugin and theme content should follow suit and exist only in the predefined menu; however, others feel that because WordPress offers the ability to define custom menu items, then they should take advantage of it.

In fact, opinionated plugins have been written specifically for on this.

I don’t have a hard stance on this issue – though I tend to lean in the direction of adding new functionality to existing menus, I think that there are perfectly suitable cases for defining custom menus.

But if you’re going to define a custom menu item, then you should place it logically among the existing menu items: If you’re writing a theme, its menu should be placed near the ‘Appearance’ options rather than, say, the ‘Dashboard’ options. Then again, there are times where makes sense to isolate a menu item to its own section. In order to do this, a custom separator may need to be added in addition to the menu.

Adding a separator to the WordPress menu isn’t difficult and though there are a variety of ways to do it, the easiest way to do about doing so is using a couple of custom functions registered with the WordPress API.

Below, I give a working example for how to add a separator to the WordPress menu. I also look at how the default menu is structured, the proper way to add your own separator, and an example plugin for doing just that. If you’re an advanced developer, it may be more beneficial (and quicker!) for you to refer to the source code on GitHub.

Continue reading “Add a Separator To The WordPress Menu”

Remove JavaScript Tags From WordPress Input Fields

When building custom functionality into WordPress, it’s important to make sure that you’re properly sanitizing data. WordPress provides an array of utility functions for doing this.

But there are times when completely sanitizing the input may be too much. For example, say you’re working on a widget that has a text field and you’re comfortable with allowing the user to input styles and markup but want to remove any JavaScript. Using strip_tags and strip_slashes is overkill, obviously. These would reduce the input into nothing but the raw text.

Here’s a simple way to removing JavaScript tags from input fields while still supporting inline CSS and HTML markup:

Continue reading “Remove JavaScript Tags From WordPress Input Fields”

Configure Coda 2 To Connect MySQL

If you’ve recently installed or upgraded to Coda 2, are using MAMP to manage your development stack, and are looking to use the built-in MySQL frontend, here’s what you need to do in order to connect to MySQL in Coda 2.

Coda 2 and MAMP MySQL Socket

Setting up a connection between your project and its database is easy. It really only takes a single step though it assumes you know the username and password to your local database (which I can’t imagine why you wouldn’t know it :).

Continue reading “Configure Coda 2 To Connect MySQL”