Add a WordPress action during Ajax call - wordpress

I included a file (some-file.php) in my Ajax callback function. In that file I added some actions:
// some-file.php
add_action('load-post.php', 'function1');
add_action('load-post-new.php', 'function2');
But the functions (function1 and function2) never executed, however I'm positively sure that some-file.php is included.
add_action works perfectly if I called it inside my plugin file directly. I have problem with it when I'm calling it inside my Ajax callback.
Why this happens and how can I fix it?

Related

Why does my Meteor template rendered callback not fired when using sub-manager?

I am not sure if this is related to the subs-manager package I am using: https://github.com/kadirahq/subs-manager
But whenever I navigate between routes that do not load any data from the server, the rendered callback for the template does not get called.
This is a big issue because I have all the functions that are supposed to run when the template is rendered.
So Meteor removes the template from the DOM, and when it renders it back it does not call the rendered callback. Why? This is a huge issue for me.

Meteor - Execute code on every page load

I want to execute a function when any page loads.
Things like Meteor.startup() and Template.myTemplate.onRendered() are not what I want because they are triggered only once, when the app is loaded.
Basically I need an event that is triggered every time the URL changes, is there one?
You can use onRun or onBeforeAction to run arbitrary code on every route change.
Router.onRun(function(){
console.log('onRun', this.current().route.getName());
this.next();
});
Router.onBeforeAction(function(){
console.log('onBeforeAction', this.current().route.getName());
this.next();
});
Use this placeholder code to detect when this code will actually run.
onRun will run only once on every route change. (good for analytics related stuff)
onBeforeAction will reactively rerun when the current route data context is modified.

How to reset Meteor AutoForm validation when leaving form page?

We have a site with several AutoForms on different pages / routes. If a user triggers validation on a particular form and triggers some validation errors, and then leaves the page with that form, when they return to that page later on the data they have entered is gone, but any validation errors remain (fields are highlighted and validation error messages are shown).
How can we reset the form validation when a user leaves the form page?
I know we can use AutoForm.resetForm('our-form-id') to do the reset, and this works in the console, but I can't find the proper hook to hang it on. The onStop IronRouter hook for the page would seem to be the right place, but that triggers an error Can't call Tracker.flush while flushing. If I wrap it in a setTimeout with timeout 0, so it doesn't run until the next tick, it has no effect (presumably the template is destroyed by that point).
I would think when the template is destroyed the error state would be, too, but its not. Is there an undocumented way to "hard" reset errors even if an active template no longer exists?
We are using AutoForm 4.2.2.
You can reset the validations using
AutoForm.getValidationContext('form-id').resetValidation();
You need to use your router's exit hook to resetValidation, but you have to first get the context for your form. Here is how it is done in FlowRouter:
FlowRouter.route('/edit/:_id', {
name: 'editPage',
action: function() {
BlazeLayout.render('appLayout', {main: 'editPage'});
},
triggersExit: [function() {
AutoForm.getValidationContext('edit-form-id').resetValidation();
}]
});
For more information on flowRouter triggers see https://github.com/kadirahq/flow-router#triggers

Regenerate JS function in partial postback after full posrback

My server-side ASP.NET code generates a JS function (actual function, e.g. function Foo() {...}) on initial page load using ClientScript.RegisterStartupScript. That function becomes part of ASPX markup and executes when client calls it.
The problem is: I need to modify that function's code in server-side partial postback (Update panel). But when I generate the function with the same name function Foo() {...} via ScriptManager.RegisterStartupScript it doesn't seem to take effect - original function generated in PageLoad's ClientScript.RegisterStartupScript is called.
Is there any way to regenerate client-side function in partial postback after it had been generated on initial page load?
Thanks!
Answered in my first comment to the question.

Javascript object not initialized on slow connections

Here's the odd situation:
we have a piece of javascript library that is being called on our onload of aspx page.
It works everytime for us, but the clients that have low speed modems get an error, because the object is not getting initialized and the aspx page is already loaded.!!
Is there any suggestions on how to call this piece of js code?
Thanks,
make sure you have your end tags.. i have seen onLoads in the not working right when your core tags are incomplete or not properly formatted
The onload even happens when everything in the page is loaded. If you have some script that is loading from a different server (ads, statistics), the onload event won't fire until those are loaded also. If their server is having problems, your onload may never fire at all, or after several minutes when the browser gives up waiting.
Instead of using onload you could put your code in a script tag as early as possible in the page, i.e. after the last element that the script needs.
If you have some external script that doesn't need a specific place in the page (statistics for example), you can move it to the bottom of the page to minimise the risk of interference with the rest of the page.
With JQuery you can call your functions with ready event :
$(document).ready(function() {
// call your functions here
});
The event will be called when the DOM is loaded.

Resources