Phyo
Phyo

Reputation: 31

wordpress do_action without add_action and action hook names

I kind of understand add_action and then do_action. for example

function mywork()
{
    echo "display my work";
}
add_action('mytag','mywork',10);
do_action('mytag');

this will render "display my work";

But how about just do_action only. for example

function customfunction()
    {
        echo "custom";
    }
do_action('customfunction');

Nothing is rendered. I also found some wordpress api hook names. for example "login_form"

do_action(login_form);

But nothing happened.

Upvotes: 1

Views: 2209

Answers (1)

Bloodday
Bloodday

Reputation: 303

When you write do_action("actionName"); you are creating a hook, so you can write code that will be executed here from external sources.

So, you can write do_action("actionName"), but since you have not added code to execute when this do_action() get called it will do nothing.

When you write add_action("actioName","functionName",parameters) you are telling to WordPress: "Hey, whenever you make a call to do_action("actionNAme"), execute functionName"

Update

Answering to this comment: "But can I do do_action without any add_action first or callback functions?".

The answer is No, you can't. You have created the hook, but you need to attach code to it somewhere or it will do nothing, this is like an event, with do_action you are telling: "Hey, this happened". With add_action you are telling when happen run that function.

Upvotes: 2

Related Questions