Can I hook inside another hook?

When it comes to the oop way of doing things you need more than just the class, you also need to instantiate it as an object at some point if you want the action to fire. class myClass{ function __construct(){ add_action( ‘init’, array( $this, ‘someFun’ ) ); } function someFun(){ include( ‘my-script.php’ ); } } … Read more

__NAMESPACE__ with register_activation_hook

The reason that the plugin activation hook wasn’t working in the code provided in the question is that on plugin activation the plugins_loaded hook is never run. Since the register_activation_hook hook was hooked from plugins_loaded it was never run on activation. And since it’s never fired ever again, this way of hooking results in register_activation_hook … Read more

switch_to_blog(): Load textdomain

You could hook into the action switch_blog. You get the new blog ID as the first argument here. But loading the complete translation files here is expensive, you also have to restore the old files after that. WordPress does not use native gettext functions, but some custom code that is much slower. See #17268. The … Read more

What is the earliest possible hook for safely using `is_front_page`?

The parse_query() method of the WP_Query class sets the variables on which the conditional tags are based. The parse_query hook are executed before returning from parse_query() method, variables are already set (eg. is_home, used by is_front_page()) and is_front_page() will work, but function that changes the current state can be hooked to parse_query (it’s still parse_query() … Read more

Do WordPress Core Filenames Work as Hooks?

The short answer is ‘yes’. Taken from this article the suffix of those ‘screen specific hooks’ is given by: For custom admin pages added via add_menu_page() – including settings pages – (and related functions) it is the screen ID (the value returned by add_menu_page()) For the admin page listing posts of any post type, it … Read more

Adding onload to body

And here’s a jQuery solution (as Mike suggested in his first comment). function add_my_scripts() { wp_enqueue_script( ‘jquery’ ); wp_enqueue_script( ‘my_init_script’, SCRIPTSRC, ‘jquery’, ‘1.0’ ); } add_action( ‘init’, ‘add_my_scripts’ ); Then add a script to your plug-in that does this: jQuery.noConflict(); jQuery(document).ready(function($) { init(); }); This will start jQuery in no conflict mode (if it isn’t … Read more

Implementing advanced add_* function wrappers

For simple cases like quick one-liner returns one should remember that it’s possible to hook an anonymous function directly in the add filter call, e.g: add_filter(‘some_filter_hook’, function($v){return str_replace(‘..’, ‘.’, $v);}); add_action(‘some_action_hook’, function(){echo ‘….’;});

trigger save_post event programmatically

It’s wp_insert_post() vs. wp_update_post() – where update will ultimately also call: return wp_insert_post( $postarr, $wp_error, $fire_after_hooks ); The term “once” implies that it is being fired “afterwards”. /** * Fires once a post has been saved. * * @since 1.5.0 * * @param int $post_ID Post ID. * @param WP_Post $post Post object. * @param … Read more