Drupal - Load page on hook_cron run - drupal

I'm developing a drupal module. I'm using only 2 hooks (hook_menu, hook_cron)
In hook_menu, I create a menu callback that does a certain function.
In hook_cron, the problem resides here. I wanna execute the path I created in hook_menu every time hook_cron runs!
How can I do that?!!!

You can use something like this.
drupal_http_request(url('your/path', array('absolute' => TRUE));
It's however not clear to me why you can't simply call an API function in your cron hook, another page request has quite an overhead.

Related

How to implement a WordPress Action Hook

I'm using the All-In-One Video Player plugin and want to alter its behaviour by listening to events that the player emits and taking actions based on them.
I contacted plugin's support team and got a very good response that I'm sure would mean something to someone who understands WordPress - I'm not one of those people.
The support team suggested using the action hook aiovg_player_footer. It looks like I have to implement that function, but I have no idea where to write that code. Is there a specific file that I need to create / update in order to get implement this function.
My function will need to alter the HTML that the plugin produces. Is this just a case of doing something like
echo '<script>console.log("helo");</script>' ?
You should add the following code in functions.php file located in the root of your theme directory:
function so61638829_aiovg_player_footer()
{
// Do something
}
add_action('aiovg_player_footer', 'so61638829_aiovg_player_footer');

call a function (actually an API call) whenever user place order

I need to call a function (actually an API call) whenever user place order(i.e after checkout).
I searched woo-commerce doc (https://docs.woothemes.com/wc-apidocs/hook-docs.html) but could not find proper hook.
Already i have wasted lot of time for this and client need this to be done.
If anyone can suggest anything then it will very helpful. I am new to woo-commerce.
Thanks for your time.
In your theme's function.php or in your custom plugin Place this:
add_action('woocommerce_payment_complete', 'my_function');
function my_function(){
// Whatever I want to do when payment is completed
// Like api call to external server etc
}
This action triggers when payment is completed. There are other actions available instead of woocommerce_payment_complete like woocommerce_order_status_pending , woocommerce_thankyou etc.
Hope that helps.

Completely custom page in WordPress generated by a plugin

I am writing a WordPress plugin that has an AJAX element to it - blocks of HTML are fetched by the front end from the plugin using AJAX.
I am having difficulty joining up the pieces here, and I suspect it is just a terminology issue. How would I implement a page completely provided by the plugin?
The content of the page will be HTML - the plugin can generate this in response to POST or GET parameters.
There needs to be a route to this page. The route does not have to be user-friendly or a REST style - just some kind of URI that gets to the plugin. Is there perhaps a way to register a custom page with an arbitrary name, without having to create it as a WP post?
I would like all this to be self-contained in the plugin, so should not involve the administrator having to create posts or pages, or have to add anything to the theme.
Ideally I would avoid any URLs that go into the wp-admin directory. End users don't belong in here.
I would strongly suggest referring to https://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side
You need to have a php script in your plugin directory that returns what you request, and you need to determine that url at run time for reference in your ajax. The above link gives an example for enqueuing and using wp_localize_script() to provide the url for your custom php script.
wp_enqueue_script( 'ajax-script',
plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );
// in JavaScript, object properties are accessed as
// ajax_object.ajax_url, ajax_object.we_value
wp_localize_script( 'ajax-script', 'ajax_object',
array( 'ajax_url' => plugins_url( '/php/myapi.php' ));
Your javascript file will be included on every page and will listen for events on the page which require a block of HTML, as you've described it.
Your file myapi.php then needs to take a request, probably using a query string, get the appropriate content from the wordpress api, and respond with said content, which your javascript will put into place.
To have access to the wordpress api as well though, you have two options:
Force wordpress to run starting with your file, by including wp-load.php. This is probably not the cleanest way.
Set up a custom page or slug to direct to your plugin.
I would advise the second option, and advise a slug, in which case you may find this post helpful: wp_rewrite in a WordPress Plugin
From Jason's comment, based on the link above:
The rewrite rules are mentioned a lot, but are really a distraction -
they just help to make URLs look more "friendly", which was not an
objective here. The key is: register a custom GET parameter; look for
that parameter early in the page rendering process; if you find the
parameter is set, then output/echo stuff and die(). There are a
number of hooks that can be used to look at the parameters, chosen
dependin on how much you want WP to set up and process first.

Drupal Webform hook_webform_submission_insert not firing

I am trying to use the hook_webform_submission_insert in my theme template.php file. I have 2 other webform hooks currently running in here and they work just fine. I am trying to get the submission data after it has been submitted. Below is my code.
function acquarius_hook_webform_submission_insert($node, $submission){
var_dump($node);
var_dump($submission);
}
I am sure I am missing something small here but everything I try seems to fail.
You need to replace hook keyword with your theme name. Also add hook functions in module.
YOURTHEMENAME_webform_submission_insert($node, $submission){
// give your code here
}
After you create a new hook, you need to clear the drupal caches.
Go to YOURSITE.com/admin/config/development/performance
And click on "Clear all caches"

drupal 7 hook_form_form_id_alter

I'm trying to hook into a form with ID equal to "block-admin-configure," mymodule_form_block_admin_configure_alter(&$form, $form_state, $form_id) is not being triggered. When I use mymodule_block_view_block_admin_configure_alter(&$data, $block), it works perfectly.
My goal is to add some additional configuration options to a regular drupal block.
hook_block_configure() etc are only called in modules for blocks that that pareticular module defines in hook_block_info(), so if you're trying to hook into a block defined by another module you'll definitely have to user a form_alter function.
Just a note, hook implementations are cached in Drupal 7 so any time you declare a new hook you have to clear the caches before it'll get called.

Resources