How can i create my own hook in drupal6? - php

How does drupal create its own hook. Similarly i want a customized hook for myself. How am I to proceed ?

May be you are looking for module_invoke_all
Some useful links to start with:
Creating hooks for your Drupal modules
How hooks are invoked with module_invoke_all()

If you have a hook that passes a parameter by reference, and you can not use drupal_alter (for example a presave hook that passes in more than one argument) you can use module_implements.
This way a module could implement it as modulename_foo_presave instead of modulename_presave_alter. It is good for when you you want to let modules alter something at multiple points in its life cycle.
For an example in drupal core checkout the code for the node_validate (drupal 7).
foreach (module_implements('node_validate') as $module) {
$function = $module . '_node_validate';
$function($node, $form, $form_state);
}
from http://api.drupal.org/api/drupal/modules%21node%21node.module/function/node_validate/7
The same approach works in Drupal 6 if you want to create hook that can be implemented in this way.

Related

WordPress do_action and add_action; running arguments in add_action call

I am currently writing a plugin for WordPress, and I'm stumped with the add_action and do_action functions. I think I may be trying to use them in the wrong context, and if that's the case - could someone point me in the right direction?
I've come from hating WordPress to loving it, I never thought I'd write a wordpress plugin but now I am getting the hang of things, it's not all that different from normal PHP Development.
Anyway, if I was to have the following code in a plugin:
class aCoolPlugin {
function __construct() {
add_action('clear_auth_cookie', array( $this, 'aCoolFunction' ), 10, 2 );
}
function aCoolFunction( $arg1, $arg2 ) {
// Do something with the arguments
}
}
How would I actually run the aCoolFunction function? Now, I have tried the following:
do_action( 'init', "arg1 value", "arg2 value" );
However, before even trying to run that code I realised:
It makes no sense, since it will essentially be running the init action far too early, and;
It just doesn't work anyway!
So, from that I learnt:
I use add_action to hook into already existing functions for WP
do_action is reserved for new hooks really, can't really think of a useful situation where a hook should be called earlier, and then again later?
So, my question now is: How the heck can I pass my variables into the add_action code? The Codex doesn't say anything regarding arguments, so what are my options? or, is my logic and understanding flawed?
And what is the overall goal? The goal is to have a function with set arguments run every time a specific hook is called, and for the original hook to not be called any earlier/later than it should be
The add action does not accept any new variables nor will it return any variables to the function that runs it. But you can access variables that are passed to the function.
function custom_function($arga){
echo $arga;
}
add_action ('callname', 'custom_function', 10, 1);// 1= number of arguments accepted, use 2 for 2 etc...must add variables to do action call
//sometime later in the code
do_action('callname', $arga);
If you want to inject variables in you have to think a little bit back to basics. If you want to access variables within functions that are not passed you have 2 options:
Retrieve them from DB or server storage or similar
Use the Global declaration. You can accesss all variables that are set at the time the do_action is run.
You would rarely call do_action for wordpress hooks (see note below), but you may end up in a situation where you are coding a template FILE (which runs after plugins and then after themes) and you want to keep your logic in the plugin you could add do_action() into the template file and the actions set in the plugin will run at that point. Or similarly if you design a plugin that you want to be able to modify in the theme you could add an action to a late wp hook, within the called function, you could call do_action for your custom hook and hook functions to it in your theme. Loads of possibilities.
Whatever your logic for calling a WP hook early is, don't if you are within the WP load pattern

WordPress hooks for executing right before any action or page loading

I'm quite new to WP. Task is to develop a plugin for oauth authentication on one of not popular openID providers. I did the same for CodeIgniter project, but WP is a CMS and is little bit complex for me to understand. In Codeigniter i check authorisation before each action. In WP i need a hook which uses for it... before each page printing, or maybe.. it would be right to say before each action in terms of frameworks. What is this hook's name?
Last hook before loading the template is template_redirect
You can use it like this:
function my_function(){
// your code goes here
}
add_action( "template_redirect", "my_function" );
You can use init hook. It will be performed before element or HTML code. It's also useful to manage POST and GET variables. The syntax is something like this:
function yourfunction() {
dosomething();
}
add_action('init', 'yourfunction');
A list of all available hooks can be found here: https://codex.wordpress.org/Plugin_API/Action_Reference
Information about Hooks: https://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters
Other hooks must be suggested and will be added in a future release if is a good suggestion.
Or you'd have to edit the core files ;)
You mean a hook when all wordpress function will available but before any output including headers sent?
Well hook your function on init. That will call when visiting site. If you want this hook only for admin area then it is admin_init.

Can I override a PHP Function that is called in another file/plugin?

First of, I have read "Override default php function" and namespaces doesn't fulfil my need. I have also looked into PHP.net's override_function() but that can't help me with my problem either.
I am using a Wordpress plugin called Jigoshop as an eCommerce solution but in some cases I cannot remove the actions I need to apply my own structure to a 'single product' page. I do not want to edit the plugin files themselves as a plugin update may negate and remove my previous changes. Essentially, I want to control the output through my /themes/mytheme/functions.php file.
Has anyone come across this before whereby the original function is contained in a file I do not want to edit for that same 'updating' reason?
Thanks
EDIT (2012-11-21):
I have a custom function in my functions.php file like so:
function prepare_jigoshop_wrappers() {
remove_action('jigoshop_before_main_content', 'jigoshop_breadcrumb', 20);
add_action('jigoshop_before_main_content', 'custom_jigoshop_breadcrumb', 10);
}
add_action('wp_head', 'prepare_jigoshop_wrappers');
This essentially allows me to apply my own structure & configuration. For other default functions, it is a little more difficult. For example, the 'quantity selector', 'Add to Cart' button and 'stock availability' all are contained within a function called jigoshop_template_single_summary in the jigoshop_template_actions.php file calling the likes of _title, _price, _excerpt, _meta, _sharing & _add_to_cart.
The order these are displayed, I cannot seem to change. Therefore, I want to essentially redefine function jigoshop_template_single_summary() {...}
You may want to look into PECL extension. It may do what you need, docs on PHP.NET.
runkit_function_redefine
(PECL runkit >= 0.7.0)
I don't know this plugin very well but probably this is what you Need Theming with Jigoshop
Override some functions in php(also in common..) is never a good idea. If the plugin doesn't provide a way to hook into it's functionality is sometimes better to use an alternative plugin or rewrite the core code..

Observer Pattern Logic Without OOP?

I was thinking about implementing a logic similar to observer pattern on my website, for implementing hooks.
What I am looking for is something similar to this Best way to allow plugins for a PHP application
However the code there is too limited, as I cant attach multiple hooks to same listener.
I am clueless about how to amplify that code to make it able to listen multiple actions at one event.
Thank You
You can do as ircmaxell suggests: add hooks. But clearly, the information he gave was not enough for you.
If you like learning by example, you may look at the CMS Drupal, wich is not OOP, but uses the observer pattern, called hooks all over the place to allow a modular design.
A hook works as follows:
a piece of php looks for the existence of a specially named function.
If that exists, call it and use its output (or do nothing with it)
For example:
Just before an article gets saved in Drupal, the article-system calls the hook_insert
Every module that has a function in the name of ModuleName_insert, will see that function being called. Example: pirate.module may have a function pirate_insert(). The article system makes a roundtrip along all the modules and sees if ModuleName_insert exists. It will pass by pirate module and finds pirate_insert(). It will then call that function (and pass some arguments along too). As such, allowing the pirate.module to change the article just before insertation (or fire some actions, such as turning the body-text into pirate-speek).
The magic happens in so called user_callbacks. An example:
$hook = 'insert'
foreach (module_implements($hook) as $module) {
$function = $module .'_'. $hook;
$result = call_user_func_array($function, $args);
}
And the function module_implements might look something like:
$list = module_list(FALSE, TRUE, $sort); //looks for files that are considered "modules" or "addons".
foreach ($list as $module) {
if (function_exists($module.'_'.$hook)) { //see if the module has the hook 'registered'
$implementations[$hook][] = $module; //if so: add it to a list with functions to be called.
}
}
Simply add a ' * ' hook, and modify the hook() function to call all the 'hooks' in both the named event and the ' * ' event.
Then, simply do:
add_listener('*', 'mycallback');
Take a look at Spl_Observer.
You said you didn't want OOP, but you can easily implement a non-OOP wrapper around this.

How to make a PHP script that's pluginable?

I'm creating my own script using the CodeIgniter MVC framework. Now, i want users to easily modify the site functionality and adding their own without modifying the code which i've already written.
How do i make my site pluginable ?
EDIT: The users would be the site admins. Not the end user. Basically just like drupal or joomla. Want the admin to be able to create/add plugins to extend site functionality.
There may be a better way that's specific to CodeIgniter, but this is what I would do:
First, create functions for various "hook points" in your code. Say, a function named PreArticle that you call in your code, before an article is displayed.
Allow the user to write code like this:
addHook_PreArticle('funcToCall');
function funcToCall( &$articleText ) {
$articleText = str_replace('Hello', 'World', $articleText);
}
addHook_PreArticle is a function you've defined, which would add the passed string to some internal list. Then when the PreArticle function is called, each of those functions are executed, passing in any appropriate parameters that you define.
Many CMS's Like Joomla and Blogs like Wordpress use variable function names:
$function="phpinfo";
$function();
You could load this into an array to create a list of functions that can be overridden.
That's a perfect case to use the Observer Pattern.
http://devzone.zend.com/article/5

Categories