This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

This holiday season, maximize your sales through enhanced website performance and 24/7 caring support. SAVE 30% NOW

How to Programmatically Create Nodes in Drupal 7

Updated on  7th August

3 Min Read
Reading Time: 3 minutes

The prime purpose of content management systems like Drupal is to make it easier for the end users to create websites. For the developers, customizing the CMS to the specific requirements of the projects is a great way of getting things up and ready quickly and efficiently.

Create Drupal Nodes Programmatically

Nodes are a great Drupal feature that organizes and controls the way in which all content within a Drupal website is displayed. All published content pieces are “nodes” that could be individually or collectively created and manipulated. To add a custom content type, a developer only has to create or modify the associated node either through the GUI or programmatically.

In this post, I’ll show you how to programmatically create nodes in Drupal 7 through simple code snippets. Let’s get into it.

Initialize Node Object

The following snippet of code will initialize a node object:

Note: If you have a multi language site, you will have to enable the locale module and put the code of the language you wish your node to have in place of ‘LANGUAGE_NONE’ for $node->language.

Add Custom Fields

Adding custom fields is easy and could be done through the following snippet:

Add Files or Images

Images or other file types could be easily added to nodes without much trouble:

Add a term to node

You can also add terms to a node for easier categorization. Terms allow developers to improve content categorization and enhance the content search process.  Adding terms to nodes requires the following single line of code:

Save the node

The only thing remaining now is to save the node created earlier. Here is how to do that:


Nodes are a great way of managing content on Drupal website. The best thing is that the developers could easily manage various aspects of nodes pro through simple code snippets. I hope that you could now confidently handle nodes at your Drupal website. If you found this blog post useful you might like to check out another post by us on, How To Install And Enable Elasticsearch On Drupal 7If you need help, just leave a comment and I will get back to you.


Share your opinion in the comment section. COMMENT NOW

Hamza Zia

Hamza is a Drupal Community Manager at Cloudways - A Managed Drupal Hosting Platform. He loves to write about Drupal and related topics. During his free time, he can be seen obsessing over Football, Cars, Android and Gaming.

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.