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;
}
?>
Your plugin needs to wait for the themes
functions.php
file to be loaded. Try this:The
after_setup_theme
hook is run immediately afterfunctions.php
is loaded.Update for your comment below.
In your plugin create a function for your form:
Then in your theme’s
functions.php
:This is a simple example. In reality you would want to call that function from a
form.php
in your theme.