Tutorials

Tutorials about HTML, CSS, PHP, Javascript, and Photoshop

  • Home
    Home This is where you can find all the blog posts throughout the site.
  • Categories
    Categories Displays a list of categories from this blog.
  • Tags
    Tags Displays a list of tags that has been used in the blog.
  • Archives
    Archives Contains a list of blog posts that were created previously.
  • Login
5169

Website speed is crucial for developing the best user experience possible, because, well, no one likes to wait for web pages to load.

One of the simplest ways for improving the performance of a site is by serving page dependencies like JavaScript libraries and CSS frameworks via a public content delivery network (CDN).

A public CDN is a web service that hosts and serves open source projects used for web development (e.g. jQuery, AngularJS, and Bootstrap). Public CDNs allow websites to use their services for free.

Here are the best public CDNs for web development.

jsDelivr

jsDelivr

With over 1,000 open source projects hosted on jsDelivr, it’s currently one of your most comprehensive options. jsDelivr relies on a couple of reputable CDN companies — CloudFront as the primary and MaxCDN as the failover — which ensures great service uptimes. jsDelivr is an open source project.

cdnjs

cdnjs

Another extensive public CDN is cdnjs, which has a little over 900 open source resources conveniently listed all in one page. Tip: See their list of plugins and extensions on GitHub to find useful tools related to the service — for example, there’s CDNJS for WordPress, a plugin for quickly implementing cdnjs-hosted projects on WP sites.

Microsoft Ajax Content Delivery Network

Microsoft Ajax Content Delivery Network

Many people outside of the ASP.NET development community don’t know that Microsoft has a public CDN. Among the open source projects on Microsoft’s Ajax CDN are jQuery, Bootstrap, and Respond.js. Compared to your other choices, Microsoft’s list of available open source projects is sparse, but what you do get is the peace of mind that comes from using a service maintained by a well-established, 39-year-old tech company.

Open Source Software CDN (OSSCDN)

Open Source Software CDN

This CDN is an open source project by MaxCDN and is powered by jsDelivr. The difference between OSSCDN and jsDelivr, besides the user interface and project ownership, is that MaxCDN, which is jsDelivr’s failover/backup CDN, is the primary content delivery network.

Google Hosted Libraries

Google Hosted Libraries

Google is a leader in the realm of website speed and performance. But not to be a company satisfied by just talking the talk; they also walk the walk by maintaining a public CDN that hosts popular JavaScript frameworks such as AngularJS, jQuery, and even older ones like MooTools and Prototype.

Project-specific CDNs

Some open source projects have their own CDNs. Here’s a couple of popular ones.

Bootstrap CDN

Bootstrap CDN

This public CDN can be used to serve Bootstrap core, as well as Font Awesome and Bootswatch.

jQuery CDN

jQuery CDN

The jQuery project has its own CDN for serving various versions of jQuery and its related projects (jQuery UI, Qunit, etc.).

Why Use a Public CDN?

Performance

Content delivery networks, in general, can reduce your page response times because they are able serve resources from multiple geographic locations, lowering the distance the data has to travel to reach the user.

Compared to multipurpose web servers, like the ones used by shared web hosting providers, CDNs also have optimized backend technology specifically for sending out static page resources such as images, JS files, stylesheets, and so forth.

Ease of use and maintenance

It’s a hassle to upload and update open source libraries on your server. With public CDNs, all you have to do is reference your dependencies and they will just work.

And, if you were away on vacation or aren’t keeping up with project updates as much as you should, you have little to worry about because the public CDN will likely update the files they’re serving.

Reliability

The public CDNs in this list are backed by major tech companies, giving you a bit of assurance with regards to their trustworthiness and service performance.

Higher cached probability

Because public CDNs are used by many websites, there’s a bigger chance that the user coming to your site already has a resource stored in their browser, further improving your page response times.

It’s free

In my definition of what a "public CDN" is, the service should have no fees and should be accessible by anyone, just like Gmail or Dropbox.

Related Content

About the Author

Jacob Gube is the founder of Six Revisions. He’s a front-end web developer by profession. If you’d like to connect with him, head on over to the contact page or follow him on Twitter: @sixrevisions.

The post A List of Free Public CDNs for Web Developers appeared first on Six Revisions.

Continue reading
0
4809

A fixed navigation bar, also referred to as a "sticky" navigation bar, is a toolbar that stays in place while the user is scrolling the web page.

It’s a commonly-used site navigation design pattern for displaying a site’s main navigation menu, as well as other essential interface components such as a search box, social media buttons, and notifications. The design pattern guarantees that important interface components are easily viewable and accessible regardless of where the user currently is on a web page.

In this tutorial, I’ll walk you through a simple CSS technique for implementing a top horizontal fixed navigation bar.

Examples

Before we get started with the tutorial, it’s probably best if we look at some sites that have fixed navigation bars, just to make sure we’re on the same page, and to show you practical applications of the design pattern.

Below are some examples of actual sites.

Niice has a fixed navigation bar that contains a search box and the site’s navigation menu. While you’re browsing designs for inspiration, you can rapidly filter them with minimal interruption to your experience simply by using the search box at the top of your screen.

An animated GIF demonstrating the fixed navigation bar on the website, Niice.

99U, an online publication, has a fixed navigation bar where the site’s navigation menu is seated. This gives users convenient access to the site’s menu at any point in the reading experience.

Animated GIF showing the fixed navigation bar on the website, 99U.

The Forbes.com site employs a fixed navigation bar to make its menu, search feature, and login widget available anywhere in the reading experience. The fixed navigation bar helps people quickly jump to another article after they have completed reading the current article. The fixed navigation bar can have the potential to reduce bounce rates because users are constantly given a menu of other articles to read.

An example of a fixed navigation bar on Forbes.com

As shown in the examples above, the navigation design pattern works well on web pages that have a lot of stuff.

A fixed navigation bar is a good way of minimizing the delay and interruption caused by switching to a new task (searching the site, logging in, or moving to other sections of the site). The design pattern, in essence, enhances usability through the application of Fitts’ Law.

Creating a Fixed Navigation Bar

Now that we’ve looked at a handful of real-world applications for the fixed navigation bar design pattern, as well as briefly discussing how it can enhance usability, I’ll now show you a quick and easy implementation method that only requires HTML and CSS.

Here’s a demo page that you may want to explore and review first.

Screenshot of a fixed navigation bar demo page.

View Demo

Download Source from GitHub

View GitHub Repository

HTML

The markup required is very minimal, just a block-level element that will hold the contents of the fixed navigation bar.

<nav class="fixed-nav-bar">
  <!-- Fixed navigation bar content -->
</nav>

For semantics and enhanced interoperability-potential with third-party web services, like search engine robots interested in finding and understanding your website’s IA, a nav element is a good option here. The nav element also happens to be a block-level element by default, which saves us a line of CSS (icing on the cake).

However, if you’d rather not use the nav element, any block-level element will do, whether it’s a natural block-level element like a div or an inline element such as a span that’s assigned a display: block CSS property/value.

CSS

Here’s the style rule that makes the fixed navigation bar stay in place.

.fixed-nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999;
  width: 100%;
  height: 50px;
  background-color: #00a087;
}

Earlier, we gave our HTML element a class attribute of fixed-nav-bar so that we can apply the above style rule to it.

The last three properties (width, height, and background-color) are variable; change their values according to your needs.

Let’s talk about the four key CSS properties responsible for the magic in greater detail.

position: fixed;

Giving the position property a value of fixed positions the bar relative to the viewport. This property declaration allows the bar to stay put even when the user is scrolling the document.

top: 0;
left: 0;
right: 0;

Setting the top, left, and right properties to 0 avoids unintended margins/padding at the top and the sides of the fixed navigation bar.

Tip: If you’d rather have a fixed bar that’s persistently at the bottom of the viewport, which is another common design pattern, simply change top: 0 to bottom: 0.

z-index: 9999;

An unusually high z-index value is used to significantly reduce the possibility that an HTML element is rendered on top of the fixed navigation bar, as long as there are no other z-index values higher than 9999.

That’s all there is to it.

Note

In the demo, there’s also a very rudimentary CSS-only responsive navigation menu. This menu is only a proof-of-concept, and it’s not production-ready. Since the focus of this tutorial is on building a fixed navigation bar, which can hold different types of menus and other interface components, I won’t be discussing that portion of the demo. I’ll just let you explore the source code if you’d like to see how that part of the demo works (if you’re having trouble or if you have questions about it, just send me a tweet and I’ll be happy to help out).

Wrapping Up

A fixed navigation bar is simple to implement. It requires minimal HTML markup and only a few CSS properties that you’re already familiar with.

The method discussed in this tutorial has excellent browser compatibility because it only uses tried-and-true CSS, and can thus be rendered properly even in some of the most ancient web browsers. However, if backwards-compatibility is important to your projects, then you may decide to replace the nav element (which is an HTML5 element) to a div.

In the proper context, a fixed navigation bar can improve usability and UX because it reduces the delay between switching to a new task. It therefore increases the affordance of the UI components within it, compared to the traditional top horizontal navigation bar that requires scrolling back up to the top of the web page.

Related Content

About the Author

Jacob Gube is the founder of Six Revisions. He’s a front-end web developer by profession. If you’d like to connect with him, head on over to the contact page or follow him on Twitter: @sixrevisions.

The post How to Create a Fixed Navigation Bar appeared first on Six Revisions.

Continue reading
0
4355

At our startup, Typeform, we have just shifted our entire web/UI design process from Photoshop and Fireworks over to Sketch. Based on our collective first-hand experiences, we’d like to discuss some of the app’s features that help us get our design work done more efficiently.

It gets CSS

At our company, we mainly use visual design software for web/UI design. This means that we almost always need to be able to convert a static design prototype to HTML and CSS. With Sketch, we’ve found this process to be incredibly easy.

The developers of Sketch created the app with CSS in mind. Everything you can do in the app can be reproduced using current CSS specifications. In Sketch, there’s no crazy, undoable visual effect that will make front-end developers hate you, just the ones you can actually write using CSS.

Also, obtaining the style rules for a specific element, such as a login button for example, is as easy as right-clicking on it and selecting "Copy CSS Attributes".

Vector graphics

Everything you create in Sketch is a vector element. Because of this, you can scale them without worrying about pixelation. This is particularly handy when exporting assets for @2x displays, such as Apple’s Retina screens, as well as when you’re working with SVG. The fact that this digital design tool is vector-based is also very helpful for responsive design.

Accurate font rendering for the Web

It’s annoying when you design an awesome interface in Photoshop, port it over to CSS/HTML, and then discover that the fonts look completely different compared to how PS showed it to you. Typography is key to the user experience, and therefore a correct representation in your web design tool is important in the design process.

One thing we’ve found with Sketch is that font-rendering is very close to how it would be on a web/mobile browser, meaning that we quickly know off-the-bat how everything will look together without having to cobble together a high-fidelity prototype.

iOS design testing

When designing our new site, we knew that it needed to look great on mobile. We’ve all done the standard tricks for testing designs in different viewport sizes such as manually resizing the browser window or using a Chrome extension to resize the viewport to match an iPhone or Nexus. But Sketch has something special for these responsive-design-testing occasions: A companion app called Sketch Mirror.

Sketch Mirror runs on iOS devices, and mirrors exactly what you see on the canvas on your Mac to all the nearby devices that have the app open. It’s great for testing your design on a series of devices all at the same time. (It would be even better if there was an Android version of Sketch Mirror.)

Symbols

If you use a design element repetitively, then making it a symbol is more efficient and improves consistency across a project.

A symbol links all the instances of a design element together. If you change one instance of the symbol, all instances you have made can be updated to reflect the change. This feature ultimately means no more searching through all your design files and layers for that "Share" button you want to tweak.

Symbols are very similar to Smart Objects in Photoshop, and this is no coincidence. The Symbols feature was originally created as a plugin for the app to bring Smart-Object-like features to Sketch. With the launch of Sketch 3.0, Symbols was brought into the core app.

Artboards and Pages

We used to have dozens of files littering our shared folders every time we were designing a new screen or component.

Since we’ve moved to Sketch, we can keep everything related to the same design or feature in the same file.

You can split Sketch’s infinite canvas up into smaller artboards, each with a unique size. We’ve found this useful when designing for a range of differently-sized devices, like when we need to know how our website will look on a 1080p display and an iPhone.

Besides artboards, you can also add pages to a Sketch file. We used this feature to split out each screen design for our new site onto a new page, keeping everything neatly organized.

Grid and Smart Guides

How could we ever live without Sketch’s Grids and Smart Guides?

First, let’s talk about grids. We all know we should use them to help us line up our design elements and get consistent spacing, but they can be such a pain to set up in our previous software.

In Sketch, however, it’s a trivial affair to set up your custom grid lines. Customizing your grid settings is easy, just go to the "View" menu. Now you have no excuse not to use grids.

And Smart Guides? They make aligning objects effortless. If you’ve ever used Keynote, you’ll be familiar with them. They tell you when an object lines up with another, and your object will snap to them as it moves.

It’s worth noting that a recent update has enhanced the Smart Guide functionality in Photoshop as well.

Small file size

For people used to the multi-gigabyte file sizes we’d seen with Photoshop, it was definitely a surprise when we found most of our designs coming in at under 100MB. The amount of money we’ll save in file storage alone makes it worth switching!

Passionate community

The people involved around Sketch are just brilliant; they always come out with something new that enhances the app, or teach you something you didn’t know.

Like Photoshop, there are some great plugins and tools that have been created by the Sketch community. One we use is Dynamic button plug-in for Sketch.app that’s useful when you need to make lots of UI buttons; as you edit the text of a button, other properties automatically adjust to keep a consistent border around the text, rounded corners, etc. Check out this list of open source Sketch plugins to get an idea of what community-developed plugins are currently available.

The community is also passionate about sharing knowledge about Sketch. There are tons of video tutorials and guides on how to get more out of Sketch, which was valuable when we made the switch.

Downside to Sketch

One drawback for a great deal of digital designers is the fact that Sketch is an Apple-centric app: It only works on Mac OS X (and Sketch Mirror only works on iOS).

Our design team primarily uses Mac OS, so for us this wasn’t an issue. But for many, this will be the ultimate deal breaker.

Do you use Sketch?

If you’re already using Sketch, or thinking of switching over, please share your experiences and thoughts in the comments.

Related Content

About the Author

David Okuniev is a UI/UX designer turned entrepreneur. He was born in Belgium, raised in England, and currently living in Barcelona where he founded Typeform.

The post Why Sketch is Ideal for Web Designers appeared first on Six Revisions.

Continue reading
0
5470

I can still remember how excited I was back in 2008 when I first discovered WordPress (late-adopter, I know).

What was particularly impressive for me was that I could change the designs of my WordPress sites with minimal effort.

And so the hunt for the perfect theme began!

As far as I can recall, there weren’t that many paid/premium themes back then. And, to be honest, I wasn’t even taking the possibility of paying for a theme into consideration (since money was a bit tight) so I was kind of stuck with free themes by default.

All was well for a while. My test sites and personal blogs were running just fine, until, one day, I noticed some strange links in the footer.

Those unusual links pointed not only to the theme creator’s site, but also to some questionable sites that had spammy anchor texts like "pay day loan", "cheap home insurance", "seo services", and so forth.

No problem, I thought, removing these should only take a minute.

But, to my surprise, those links were neither in the theme’s Widgets screen nor anywhere in the theme’s source code.

I searched all of the theme’s files in bulk with a source code editor, looking for those anchor texts.

I even went through the database using a SQL SELECT statement.

Nothing.

So how was it possible for these links to remain under the radar?

Well, as it turns out, this is the story of my first encounter with encrypted code in WordPress themes.

Shady WordPress theme practices

Encrypted code is just one piece of the puzzle; there are other distasteful WordPress theme development practices going on.

Static links pointing to suspicious websites

Not all static links (or embedded links) in WordPress themes are bad.

A lot of times, they are just links that acknowledge the developer of the theme. As long as the links are decent, and granted that the theme user is informed that the links will be displayed on their site, then they might be alright.

However, in some instances, they may be going to spammy websites. When links start pointing to casino sites, online pharmacies or some shady biz-op products, all without the site owner’s knowledge, then it’s a different story.

Search engines simply won’t be pleased to find out that your site is linking to the dark side of the internet, and they may penalize you for it even if you’re an unwitting participant.

Theme-locking

Some links and code blocks in shady WordPress themes are "locked in" with JavaScript and/or PHP. Removing these links or code blocks will make the entire WordPress site go blank, displacing your content with a message telling you that you need to put the links/code blocks back in place to be able to remove the message from your site.

Encrypted code

Encrypted code is a term in the WordPress community for code snippets that are purposely being obfuscated from the user of the theme. The intent of encrypted code is to hide portions of the theme’s source code and to make these portions difficult to remove. Encrypted code can do a variety of things, such as generating links to third-party sites and interfering with the user experience.

Why these things are happening

Some companies go out of their way to include harmful elements in their WordPress themes.

Why would they do this? You might ask. Because WordPress themes are a great online marketing tool.

The market for themes keeps growing year over year. It’s been reported that 18.9 percent of all sites are built with WordPress. And, at one point, all those site owners will go out looking for nice themes.

The way some people capitalize on this opportunity is by creating a great-looking WordPress theme and then including hidden links and obfuscated code in the theme.

Imagine what their reach could be like if they get even a few hundred domains installing their theme.

How to make sure you’re using a trustworthy theme

If you’re considering the use of a new WordPress theme, below are some steps that can help you stay out of trouble.

Of course, the first best option is to use a theme obtained from a reputable source and created by a trustworthy theme developer, both of which can significantly reduce the potential of these shady practices being employed. It wouldn’t hurt to go through these steps despite the reputation of your candidate theme.

Step 1: Install theme-checking plugins

It is very possible to investigate every theme by hand just by going through its files. But doing so with a trusty plugin is much quicker, and potentially more thorough.

You’ll need two plugins for this step.

  1. TAC scans all of your installed WordPress themes for potentially malicious code.
  2. Theme-Check tests your themes for compliance with all the latest WordPress standards and best practices.

Step 2: Test the theme for encrypted code

Before activating a WordPress theme, you should first test it in a safe development environment. One such environment is on your PC. However, if you don’t have time for that, you should still be fine as long as you don’t activate the theme before running your tests.

To see what the TAC plugin has to say about your new theme, in the WordPress administration screen, go to Appearance > TAC.

Scroll down to your theme to see if there are any issues.

Screenshot of a TAC test result.

If TAC says that everything’s fine (and you’re using a theme from a trusted source) then it probably is.

However, if it indicates the presence of any encrypted code then you need to be cautious.

It’s recommended to never use themes that have encrypted code. The main reason for this is that you have absolutely no control over what’s inside the encrypted block of code. It could be anything: third-party scripts, data-mining, ads, link-building schemes, etc.

Some PHP and JavaScript functions associated with encrypted code are designed to help developers perform difficult but benign processes, such as data-encoding conversion or parsing. But in the wrong hands, and in the context of WordPress theme development, they are often used to hide shady source code.

If you want to do some sniffing around on your own, you can batch-search theme files with a text editor like Notepad++. Look for the keyword "base64_decode".

The base64_decode() PHP function, together with the eval() function, is commonly used to execute encrypted code.

For example, if the encrypted code you find looks anything like this (just an example, not actual code) stay away:

$o="eHQvamF2YXNjcmlwdCI+PC9zY3JpcHQ+";
eval(base64_decode("PHNjcmlwdCBzcmM9Ii8vYS1zaGFkeS1zaXRlLmNvbS9qcy9zcGFtbGlua3MuanMiIHR5cGU9InRl" . $o));

Step 3: Evaluate all the static links

TAC will also let you know when it stumbles upon any static links inside the theme. When you click the Details button, you will see the exact files and lines where those links occur.

Find embedded static links with TAC.

Static links are common in free WordPress themes, and they’re not necessarily bad. For instance, some themes require attribution. The first thing you should do is decide whether or not you’re okay with those links being on your site. In the end, if you’re not okay with any link, you just shouldn’t use the theme.

When it comes to static links, most of the time the party’s going on in the footer.php file.

For instance, the theme I checked when doing research for this piece had links pointing to a site titled "Botox Tel Aviv" with an anchor text consisting entirely of non-Latin characters:

An example of an embedded static link in a WordPress theme.

For testing purposes, you can check what happens if you remove an embedded link. Sometimes, you will discover that your entire site will stop working as a result of this action. And that is exactly what happened to my test site; when I removed the link, the site stopped working and the content was replaced with this message:

The message being displayed on all pages when a static link is removed.

If you want to investigate some more, this is where the other plugin, Theme-Check, comes into play.

Go to Appearance > Theme Check and run a test for the theme in question. Most likely, the plugin will report a lot of stuff, so you will have to browse around for a while to find what might be causing the problems.

In my case, the instructions found in line 124 of the functions.php file were to blame:

Theme-check finding a WordPress theme error.

Conclusion

The moral of the story is that you should choose WordPress themes carefully. Before using any theme, it’s best to perform some research and testing on it.

Related Content

About the Author

Karol K is a blogger who’s passionate about WordPress. He works with ThemeIsle, a WordPress theme development company. His work has been published all over the Web, on sites like Web Design Ledger, Quick Sprout, ProBlogger, and others. Reach him at @carlosinho.

The post How to Make Sure You’re Not Using a Shady WordPress Theme appeared first on Six Revisions.

Continue reading
0
4242

In the first part of this seriesOn data in WordPress, I gave an overview of the WordPress database tables, and which tables are used to store what kind of data. I'll outline how WordPress manages the relationships between that data, In this second part

One-to-many and many-to-many, As you'll see, WordPress uses three kinds of data relationship - one-to-one. I'll look at each of these and what they mean for your WordPress site

One-to-One Relationships

A one-to-one relationship is the simplest relationship of all - it simply means that one record is related to just one other. Data like this is generally stored within one table (although not always, as we'll see later on in this tutorial)

Examples of one-to-one relationships in WordPress include:

  • Post ID and post content
  • Post title and post content
  • Post ID and post slug
  • Comment ID and comment content
  • User ID and username

The list could go on, but the main point is that any two records which are stored in different fields in one row of a table have a one-to-one relationship

Are the least interesting kind of database relationship, One-to-one relationships, however. So without further ado let's move on to a relationship type used a lot by WordPress - one-to-many

One-to-Many Relationships

One to many relationship are very common in databases, and are what makes a database more powerful than a 'flat' table such as a spreadsheet. These occur when one record has a relationship with more than one other record.  

Using a unique identifier to join the two, most one-to-many relationships are created by a link between two database tables, In WordPress. The, So for examplePost_idRecord will be unique to each post stored in theWp_postsTable, but will also be used in theWp_commentsTable to identify which post each comment was posted to. This means that eachPost_idValue will appear only once in theWp_postsTable but could appear multiple times - or not at all - in theWp_commentsTable

Some other examples in WordPress include:

  • Posts and post metadata
  • Posts and users
  • Users and user metadata
  • Taxonomies and taxonomy terms

Again, the list goes on. As this is an important topic in WordPress, let's take a look at the one-to-many relationships in more detail

One-to-Many Relationships Involving Posts

The table which is connected to the most other tables is theWp_postsTable, and most of the relationships these connections create are one-to-many relationships

As you can see in the diagram below, theWp_postsTable is linked to four other tables:

Posts can also have a one-to-many relationship with other posts, in the form of attachments or parent pages, In addition to this

Let's take a look at these relationships in more detail

Posts-to-Posts

Data stored in the, As explained aboveWp_postsThis relationship isn't between what you would think of as posts, table can have a one-to-many relationship with other data in the same table; however, but instead is between posts and attachments or pages and other pages. After all, attachments and pages are post types

An attachment stored in theWp_postsTable will have a record in thePost_parentField, which will be the ID of the post to which the attachment is attached (with apologies for the repetition. ). This is a one-to-many relationship because each attachment will only have one parent, while each post could have many attachments. This will apply even if you've used the Media Manager to add an attachment to another post - only the post to which it was originally added will be stored in the database as its parent

Posts can also have a one-to-many relationship with other posts when one page is specified as the parent of another. In the, The record is stored against the child pagePost_parentField - again this will be thePost_idOf the parent page. This is a one-to-many relationship because parent pages can have many child pages but child pages will only have one parent page

It's important to remember that when considering WordPress data handling, When thinking about relationships between posts like this, posts are not what you might normally refer to as posts. All of which are stored in the, attachments and other post types, They include pagesWp_postsTable

Posts-to-Post Metadata

Post metadata is stored in its own tableWp_postmetaThis table only includes four fields - for thePost_idTheMeta_idThe key and the value. One post can have many items of post metadata associated with it but each post metadata record will only be linked to one post

Posts-to-Comments

Comments also have their own tableWp_commentsEach comment will relate to only one post while each post can have multiple comments stored against it. As with all of the other tables linked toWp_postsThePost_idField is used for the link (in theComment_post_idField inWp_comments)

TheWp_commentsThe comment itself and its approval status, the author's email address, table has 15 fields by default, to store data about the author. It's also linked to theWp_usersTable, as we'll see in the next section

Which I'll come to shortly, stored in another table, Comments also have their own metadata

Posts-to-Users

Each post is attached to theWp_usersTable via theUser_idWhich is stored in the, recordPost_authorField inWp_postsThis is a one-to-many relationship because each post has only one author but each user can be the author of multiple posts

The relationship between posts and users is different rom that between posts and comments or metadata because its nature is of one user, not one posts multiple comments or mete data records, multiple posts. This is why theUser_idField provides the link instead of thePost_idField

One-to-Many Relationships Not Involving Posts

There are also three relationships which are one-to-many and don't involve posts. And users and their metadata, Two of these involve metadata - the relationship between comments and their metadata

Users-to-User Metadata

WordPress stores additional metadata about users in theWp_usermetaTable. This is generally used for non-standard data about users, such as metadata about superadmins in a multisite installation and metadata about the admin colors a user has selected if these aren't the default.  

Most data on users will be in theWp_usersTable. The two tables are linked via theUser_idField and one user record will have multiple items of metadata associated with it, whereas each item of metadata will only apply to one user

Comments-to-Comment Metadata

Again, most data about comments is stored in theWp_commentsTable along with the comment itself, but theWp_commentmetaTable is used to store additional metadata such as data created by plugins like Akismet. The relationship is the same as between users and usermeta

Comments-to-Users

The final one-to-many relationship is between comments and users. TheWp_commentsTable includes theUser_idWhich can be used to store the user ID when a logged-in user has posted a comment, field. Note that this field isn't mandatory for those sites where users don't have to be logged in to comment

Many-to-Many Relationships

Where the many link goes both ways, The final kind of database relationship is the one-to-many relationship. For taxonomy terms (including categories and tags as these are simply taxonomies), This is only used once in WordPress. One post can have many terms assigned to it and one term can be assigned to multiple posts

WordPress creates this relationship in the same way as any other database with manay-to-many database: by using an interim table linking the two tables with the key data. This is theWp_term_relationshipsWhich links the, tableWp_postsTable to theWp_term_taxonomyTable.  

Note: This table also links theWp_linksTable to theWp_term_taxonomyTable, as links behave in much the same way as posts - I'll come to links shortly

This is most easily explained by looking first at the two outlying tables. TheWp_postsTable contains data on each post, while theWp_term_taxonomyIncluding the taxonomy it is in and its ID, table contains data on each term.  

To create a link between a term in theWp_term_taxonomyTable and a post in theWp_postsWordPress creates a record in the, tableWp_term_relationshipsTable, which includes thePost_idAnd theTerm_id(stored asObject_idAndTerm_taxonomy_idRespectively in theWp_term_relationshipsTable). This means that theWp_term_relationshipsCreating the many-to-many relationship, table can include multiple records for each post and multiple records for each term

The diagram below show how this works:

The posts have the following terms:, In the example above
  • Post 1 has terms 1 and 3
  • Post 2 has term 2
  • Post 3 has term 4
  • Post 4 has terms 1 and 3

Term 3 relates to posts 1 and 4, You could also look at it the other way - for example

However things don't stop there. The, There is a fourth tableWp_termsTable. This holds data about each term, namely the name, slug and description for each term. Each term has only one record in theWp_termsSo the relationship between that and the, tableWp_term_taxonomyTable is actually one-to-one. There's no reason this data couldn't be held in the, Theoretically speakingWp_term_taxonomyBut it isn't, table

A Note on Links

Is a feature of WordPress which is far less prominent than it once was, Links, or the blogroll. Since version 3, In fact. 5 links have been turned off in the admin by default; however, they do have their own table: theWp_linksTable.  

This is very similar to theWp_postsTable. Links are a content type very similar to posts) and have the same many-to-many relationship with taxonomy terms, After all

Summary

As we've seen, WordPress uses a variety of relationships to link data in 10 of its 11 database tables. The only table I haven't mentioned here isWp_optionsAs it simply stores data about the site, rather than about content, because that isn't linked to any other tables. I'll cover that in more detail later in this series

By understanding the one-to-one, one-to-many, that can help you to identify ways to manipulate your data and write custom queries in your theme and/or plugins, and many-to-many relationships in WordPress

I'll cover content types in more detail, looking at the types of content stored in the WordPress database and the similarities and differences between them, In the next part of this series

Continue reading
0

Testimonial

Thank you so much! We are very happy with our new website. It is easy to use and all of our customers tell us, they love it.

Contact Us

  • 13245 Atlantic Blvd. #4352
    Jacksonville, FL 32225
  • 904-240-5823