Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create one basic plugin where I want to create my own action hook. Here is the code for the same.

<?php
/*
Plugin Name: Demo Plugin
Version: 1.0
*/

do_action('basic_action_demo');
?>

Now after activating this plugin I want to use this action hook in my current theme's function.php file, code for the same is as below:

add_action('basic_action_demo','action_demo');

function action_demo()
{
    echo "I am inside";
}

Now the problem is my hooked action never gets called, we can see that do_action is called unconditionally so it will be called on each page load but it never gets into "action_demo" method.

What I have figured out far is plugin is loaded before my theme'e function.php file is executed. So here do_action is called first and then add_action.

A hint would be really appreciated.

Update:

Below plugin action works.

<?php
/*
Plugin Name: Demo Plugin
Version: 1.0
*/

add_action('basic_action_demo','action_demo');
do_action('basic_action_demo');

function action_demo()
{
    echo "I am Inside";
    die;
}
?>
share|improve this question
    
The order of your do's and add's are not right. Please explain what you what to accomplish with your own action hook for us to help tell you where/how you need to add them. –  Waldermort Nov 25 '13 at 18:36
    
@Waldermort Well, Let's say I have plugin with simple form. I want to call my own action hook once that form is submitted. hope this is clear what I mean. –  Avinash Nov 25 '13 at 18:39
    
Sorry, not so clear. You can call your hook from your form handler script at which time the core should be fully loaded. It all really depends on how you create that code and how you handle your query variables. –  Waldermort Nov 25 '13 at 18:46
    
@Waldermort Problem which I have figured out (from your answer below) is plugin file is being called before theme file. Do you have any specific guidelines on how it should be handled? –  Avinash Nov 25 '13 at 18:53

1 Answer 1

up vote 2 down vote accepted

Your plugin needs to wait for the themes functions.php file to be loaded. Try this:

<?php
/*
Plugin Name: Demo Plugin
Version: 1.0
*/

add_action('after_setup_theme',function(){
    do_action('basic_action_demo')};
);
?>

The after_setup_theme hook is run immediately after functions.php is loaded.

Update for your comment below. In your plugin create a function for your form:

<?php
/*
Plugin Name: Demo Plugin
Version: 1.0
*/

function output_my_form () {
    echo "I'm a form";
    // do your action here
    do_action('basic_action_demo');
}
?>

Then in your theme's functions.php:

if ( function_exists('output_my_form') ) {
    output_my_form();
}

This is a simple example. In reality you would want to call that function from a form.php in your theme.

share|improve this answer
    
This works, but is this is the only way to add own action hook in plugin files or am I missing something? –  Avinash Nov 25 '13 at 18:48
2  
@Avinash When do you want your code to be run? A plugin is made to be available to a theme, that's why they are loaded first. If you want to run code in your plugin from your theme then just create a set of functions in your plugin and call them from your theme. If you want to run theme code from a plugin (not a good idea) then use the above action hook to wait for the theme then call those functions from the action handler. –  Waldermort Nov 25 '13 at 19:00
    
I have my form submission code in my plugin file so after that I have done do_action('my_action') which is not working :( –  Avinash Nov 25 '13 at 19:03
1  
OK. I'm beginning to understand. Don't output your form when your plugin is loaded. Make a function for it, then call that function from your theme. I'll update my answer. –  Waldermort Nov 25 '13 at 19:06
    
@Avinash Please remember to upvote/accept if you think the answer is useful :) –  Waldermort Nov 25 '13 at 19:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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