the_posts filter been called multiple time

‘the_posts’ is an action fired everytime WP_Query get posts, for main query and for other secondary queries, so when you do something like: $foo = new WP_Query($args); in a widget or elsewhere (shortcode, related posts…) ‘the_posts’ is triggered again. However there is only one main query, so you can use a conditional and do what … Read more

Remove/Unregister or hide a widget added by a plugin

Thanks Stephen for helping me wrap my head around this today. Even though it’s something so small, I absolutely hate when develpers bloat plugins with crap you don’t want. I was very frustrated after WPengine, telling me it can’t be removed and that if I really want to remove it than I need to find … Read more

Admin: global $post empty

The basic mistake is, that every data is available at the point where you write your code. That isn’t the case and hooks are there to provide access to the core code during different states of the core loading procedure graph. In short and very simplified things run like this: # core loads // …more … Read more

add_action not using ‘delete_post’ action with wp_delete_post

You need to use before_delete_post action. As its already very late when delete_post action is run. WordPress un-attach all the attachments before running delete_post action. So when you run get_posts in your delete_attachment function there are no attachments found. So you need to hook your function to before_delete_post. add_action(‘before_delete_post’,’abc_delete_attachment’);

Hook to get image filename when it is uploaded

The handle_upload hook is called after wp_handle_upload is run, and it sends in a named array consisting of ‘file’, ‘url’ & ‘type’. You could possibly use code like this to utilise it, depending on what you need to achieve: function process_images($results) { if( $results[‘type’] === ‘image/jpeg’ ) { // or /png or /tif / /whatever … Read more

Plugin custom Action Hook not working

Your plugin needs to wait for the themes functions.php file to be loaded. Try this: <?php /* Plugin Name: Demo Plugin Version: 1.0 */ add_action( ‘after_setup_theme’, function() { do_action( ‘basic_action_demo’ ); } ); ?> The after_setup_theme hook is run immediately after functions.php is loaded. Update for your comment below. In your plugin create a function … Read more

add_action and Ajax

I skimmed through the plugin’s code and you could try to use the define_public_hooks() method of the Plugin_Name class to register your ajax action callbacks of the Plugin_Name_Public class: /** * Register all of the hooks related to the public-facing functionality * of the plugin. * * @since 1.0.0 * @access private */ private function … Read more