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 ‘….’;});

Execute function when post is published

The correct action is ‘draft_to_publish’. To be sure you used the correct status try to get a list of all registered post statuses (including custom statuses) with: <pre><?php print ‘- ‘ . implode( “\n- “, array_keys( get_post_stati() ) );?></pre> On a vanilla installation you should get: publish future draft pending private trash auto-draft inherit Note … Read more

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

Global Objects and Public Methods

This is not common practice, but it works. A better approach, is to use a class and with the singleton pattern, just like WooCommerce and many others, where you have: A static function (called instance, getInstance…) that: Creates an instance (object) if not already done and returns it Or returns the existing instance Let’s continue … Read more

Preventing a plugin from updating

First of all… It is a really bad idea to modify existing plugin. But if you really have to do this, then you can hide update link with this code (this one works for Yoast SEO): function remove_update_notification_link($value) { if ( array_key_exists(‘wordpress-seo/wp-seo.php’, $value->response) ) { $value->response[ ‘wordpress-seo/wp-seo.php’ ]->package=””; } return $value; } add_filter(‘site_transient_update_plugins’, ‘remove_update_notification_link’); The … Read more