Drupal hooks - registation - php

I have a Drupal website that I have integrated with a third party application. Currently I am required to execute 2-3 steps manually to ensure the new user account is integrated with my third party application. I can write a PHP application to execute these 2-3 steps. However I do not know how to execute this PHP file when a new user registers on Drupal.

This is done via the user hook. You can check for the different options from $op and do whatever you need to do. This needs to be done via a module, so read about how to create your own modules.

As you may know, Drupal permits to interact with its flow of operations via the so called "hooks". Whenever something relevant occurs, Drupal eventually fires these hook (they are functions, after all).
If you need to do something when a certain event occurs, you will create a module (third_party_integration.module, for example). In that module you'll write what you need to do when, for example, a new user is created.
In Drupal 7 the old hook_user() has been replaced with a whole set of more specific hooks: see http://api.drupal.org/api/search/7/hook_user_
The hook function must be called after your module name: for example: third_party_integration_user_insert()
The .module is just a plain PHP file, so you can add everything you need inside it (along with the actual hook function, of course).
Just remember to name EVERY functions in your module prepending your module name (with or without an prefixed _). This is just to play nice with other modules.

Related

WHMCS Hook for Client Downloads?

WHMCS has a feature for providing downloads to clients. The feature can be found in their admin area at /admin/supportdownloads.php
Is there any action hook for when files are uploaded via the admin area? The only related hook I can find is for when files are downloaded.
What I want do to is add a hook so any file I upload is added to the list of Associated Downloads for each of my products. With all the hook options they have, I'm surprised I can't find one for this...
While I have not done exactly what you are trying to do, I can tell you that anything you put in the /includes/hooks folder gets executed, regardless of whether it is actually hooked into a specific point. This is incredibly useful for those moments where there aren't any hook points available (or they are documented as being present but not on certain page renders).
So in a php file in the /includes/hooks folder, I start by echoing out the globals variable. WHMCS stores a lot of information in the globals variable that easily permits you to deduce which page you are on. For example, in one of my hooks, I have a function that determines what the file name is that I am on simply by calling up get_filename(); That function checks the request URI and finds the php file being called. If the filename is one I'm looking for (for example 'dologin') I can isolate the code being executed. You continue to isolate based on the page you are looking for (for example, if each request has a certain variable, or requires being logged in you look for those variables).
Once you are sure you have to be on the page in order for the code to fire, then you can write your PHP to grab the form posted data and store it how you like in a secondary table. The only catch is if the form must first be stored into the database, so you need a unique identifier, but that can be gotten around also.
Hope that is of help...

Plugin Access to When Files are Saved

I have created a snippet that takes specified code and minifies it, but I'm looking to turn this into a plugin to streamline the process. I'm looking to have the code minify when I save a file, and create a new file in that directory with a new name. I have this working as a snippet, but I don't want to be writing files every time the page is loaded.
Is there a way to check when a file (in the Files Tab/Assets) is saved in ModX, and then execute a plugin? I looked through the existing System Events, and didn't find anything.
Looks like you have 3 system events that may work for you: OnFileManagerUpload, OnFileEditFormPrerender & OnFileCreateFormPrerender. My guess would be OnFileManagerUpload.
Docs here:
http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/plugins/system-events/onfilemanagerupload
Though it's important to note [the last time I checked] that even if a system event is listed in the manager and/or documented, they are not all implemented [i.e. won't work] - best to check on this in the modx forums or dig through the code to see if it actually exists.
Now, you are trying to do this from the manager or some front end form?
In the manager attaching a plugin to the OnFileManagerUpload hopefully works. In the front end you may have to attach your plugin to one of the page rendering events either read the file's time/date or append/prepend a timestamp within the file itself.
[or if you are using formit, you can probably use a custom hook to get the job done as well]

Wordpress PHP Cron jobs

I have a plugin that imports csv data into wordpress tables. It runs through the Wordpress dashboard where you input some details and click a button to execute it.
I've altered the plugin so that the input data is static(from the same csv every time) and is now all located in one php file. I want to schedule a Cron job to execute this script every hour or so.
I tried to set it up using cPanel and directly accessing the php file but it does not work(nothing is displayed). I believe this is because the plugin uses wordpress functions such as wp_insert_post.
How can i run this script, as if it were run through wordpress dashboard, as a scheduled event?
Note: the file also contains some javascript.
You’ve got to include 2 files to get access to admin side functions: First, wp-load.php. wp-load.php gets everything set up, and fires up wordpress. However, you are calling this function from the plugin folder, inside the content directory (as opposed to the admin directory) – so when wp-load is called, you are not going to be in the admin section, and not going to get access to those functions. On the bright side, you also don’t have to deal with WordPress forcing you to login. Since you still need those admin functions, include wp-admin/admin-functions.php. This loads up the admin side and gives you access to the admin functions – and you are set to go
You should use wp_schedule_event. See in WordPress codex here : http://codex.wordpress.org/Function_Reference/wp_schedule_event. Use something like this:
register_activation_hook(__FILE__, 'my_activation');
add_action('my_hourly_event', 'do_this_hourly');
function my_activation() {
wp_schedule_event( time(), 'hourly', 'my_hourly_event');
}
function do_this_hourly() {
// do something every hour
}
You can perfectly call a PHP file inside your do_this_hourly() function.
Accepted answer worked well for me. I modded this, and here are my findings. It is the case that you can use this solution outside of a plugin scenario. Basically, you can have your server run using cron jobs using native wordpress functionality as followed. You can create a file at the top level of your wordpress app and then include /wp-load.php; therein.
This loads in native wordpress and allows you to call a class that you can define as part of your must use plugins. Then your good to go regarding basic wordpress functionality, things like $wpdb and get_usermeta()
Now you can instantiate your class with $class_variable = new YourClass; and from there you can call class functions. The great part about this is that you can schedule cron jobs as you would normally on your server, and when you do you have your wordpress function running as it would inside wordpress. This means you don't need to maintain your cron job stuff as part of a plugin, which may or may not be useful depending on what your up to.

run a function on each page

My codeigniter app is multilingual and I want to redirect users to their pages, by checking IP address.
I should check it at the top of all pages (i know i can set a session or cookie, but i want to check it on all pages); before any views or other.
Where should i put my code (function)? on Startup file or Loader? or create an extension or plugin and load it on Startup? if it can be done by an extension or plugin, how can I create it? (i've searched, but didn't find a useful tutorial)
Thanks.
If you're using a main front controller you can put your code in there. But a better way to do it would be to use CodeIgniters built in functionality to extend the core - hooks!
http://codeigniter.com/user_guide/general/hooks.html
Just select the point you want your script to be activated and take it from there.

Looking for a good way to organize a web app

I am doing a simple Web application which works more or less like this: (simplified)
Module Selection page: When the user
selects a module, the module page
appears
Module page: Contains buttons for
various items. Click on a button and a
small AJAX script retrieves the item
for a Module Handler
Module Handler: simple PHP script called from the module page through AJAX.
Does the job by querying the database and checking permissions
My problem is with the issue of the first item: When the user lands on the Module page, the first item should be already displayed, but all the code to retrieve the item is in the Module handler.
I could of course import the Module Handler inside the Module page and query the functions directly in PHP to get my first item, but it feels a bit dirty to include the handler, and then call to it.
Is there a better way of doing this?
If you are looking for a good design pattern for a web application, look no further :
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
The Model View Controller is a well tested one and various frameworks use it.
In your case, the modules are the controllers, the pages are the views and the handlers are the models.
Well i can simply suggest let your first view be loaded, be blank. Just do a include to the default module you want to handle. That way, the view will be just playing like a router. Rather then routing it to the request, it will just import it and use it.

Categories