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

How to check if a hook is hooked or not?

Sure, that’s called has_action, which is an alias of has_filter. Usage: if ( has_action(‘hook_name’) ) { throw new \Exception(‘You cannot hook to a protected action.’); } else { do_action(‘hook_name’); } These two functions access the global array $wp_filter that stores all of the filters / actions

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)