I need to automatically flag a node after it has been published. All nodes of a certain content type will be flagged.

In order to solve that problem, I have installed the Rules module hoping that I would be able to automatically flag the nodes that are created. Rules module does not come prepacked with flag actions.

Is it possible to flag an item that has just been created?

3 Answers 3


To flag a node when it is published, you need to:

  • In admin/config/workflow/rules, create a new rule selecting "After saving new content" as event
  • Create a "Content is of type" condition selecting the content type you want to be flagged
  • Click on the "Add and" link that appears on the bottom of the conditions table
  • On the "AND" row, click on "Add condition"
  • Create a new condition choosing "Content is published" as condition type
  • Add a new action selecting "Execute custom PHP code" as type
  • Use the following code as custom PHP code

    $flag = flag_get_flag('published'); // Replace "published" with the name of the flag you want to add.
    $flag->flag('flag', $node->nid);

On Drupal 6, Rules exposed the custom actions set on admin/config/system/actions, but the version of Rules for Drupal 7 doesn't seem to have that possibility. Flags for Drupal 6, as far as I remember, exposed some actions to Rules, but I doesn't seem it does it on Drupal 7.
That is the reason I used a custom PHP action to flag the node.

  • This can be done without custom PHP using Rules 2
    – Citricguy
    Jan 31, 2012 at 4:40
  • many thanks to poster 1, this nearly did the trick for me. in drupal 7.x even with flags 2.x-dev i stumbled into the problem that i could not flag with rules. the task was somehow simple: if flag1 is flagged (and condition was true) just flag another flag2... there is the option in rules +action dropdown-menu to do that, but it never worked. maybe because it was that an anonymous user could start the event? but with the code $flag = flag_get_flag('published'); $flag->flag('flag', $node->nid); i could do that - just that i had to alter $node->nid to flagged_node->nid (otherwise he threw an erro
    – user12754
    Jan 16, 2013 at 22:46

FYI, after a lot of search I discovered that Rules2 support in Flag for Drupal7 is already in dev. I tried it and it works well!

There is also a patch as stated here: http://nodeone.se/blogg/learn-flag-with-nodeone-part-7

Not easy to google out! :P Bye!


Yes, this can be done with Rules 2 through the Rules UI without creating custom PHP.

I need to automatically flag a node after it has been published.

If you are also 'creating' the node in the same action you will need to modify your Rules actions to be able to flag the node without using custom PHP code.

Create a new rule and add your 'create new entity' action. See image below:

Add Create new entity action

Next you will need to save the entity (force immediate save) and then flag the entity. See image below:

enter image description here

Without first saving the entity I was receiving all kinds of SQL errors and such. The above settings work for me and does not require custom PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.