Check if post is being published for the first time, or is an already published post being updated

Hook into edit_post to catch changes. And take a look at wp_transition_post_status() which is called on inserts and updates: function wp_transition_post_status($new_status, $old_status, $post) { do_action(‘transition_post_status’, $new_status, $old_status, $post); do_action(“{$old_status}_to_{$new_status}”, $post); do_action(“{$new_status}_{$post->post_type}”, $post->ID, $post); } On publish you hook into draft_to_publish, pending_to_publish and auto-draft_to_publish. For edits hook into publish_to_publish. Example A mini plugin that notifies all … Read more

Remove an action from an external Class

A simple way to achieve this (but without the Class approach) is by filtering the output of wp_head action hook using the output buffering. In your theme’s header.php, wrap the wp_head() call with ob_start($cb) and ob_end_flush(); functions like: ob_start(‘ad_filter_wp_head_output’); wp_head(); ob_end_flush(); Now in theme functions.php file, declare your output callback function (ad_filter_wp_head_output in this case): … Read more

Earliest hook to reliably get $post/$posts

For all admin pages and front end pages except individual post edit screens (wp-admin/post.php), ‘wp’ is the most reliable hook for getting the global values. http://phpxref.ftwr.co.uk/wordpress/nav.html?wp-includes/class-wp.php.source.html#l486 You can see there that it fires immediately after WP::main() fires WP::register_globals(). The problem with using things like post_results and get_posts is that it will run every time you … Read more

How many filter/action hooks are healthy?

As long as hooks don’t have anything hooked to them they are no-ops, that is do nearly nothing and have no considerable effect on runtime. It gets quite different when things are hooked and there are a lot of calls. Since you talk about customizing strings the good example would be gettext hook in core. … Read more

Please explain how these hooks work

It is possible for action hooks to be provided without being used, which is what’s happening here. woocommerce_before_checkout_process and woocommerce_checkout_process are hooks provided by WooCommerce, but WooCommerce does not itself attach callback functions to either of these hooks. They are provided to allow plugins and themes to run code at the time that the respective … Read more

Using jQuery to delete data stored in wp_options

Ajax in WordPress works by sending an HTTP post to /wp-admin/admin-ajax.php (by default) that then fires the corresponding hook. So, you attach some jquery to an event triggered by your delete button, which then posts to admin-ajax.php, which has an action, say, delete_my_options(), which actually runs the php to delete. Then, you have a function, … Read more

add action only on post publish – not update

Like @milo pointed out in the comment, checking if the post meta exists is the easiest way to achieve what you want – like this: add_action(‘publish_post’, ‘wpse120996_add_custom_field_automatically’); function wpse120996_add_custom_field_automatically($post_id) { global $wpdb; $votes_count = get_post_meta($post_id, ‘votes_count’, true); if( empty( $votes_count ) && ! wp_is_post_revision( $post_id ) ) { update_post_meta($post_id, ‘votes_count’, ‘0’); } } → On … Read more

Function to execute when a post is moved to trash .

This will do the trick! add_action(‘trash_post’,’my_trash_post_function’,1,1); function my_trash_post_function($post_id){ if(!did_action(‘trash_post’)){ // do stuff } } Here we add the function, and to prevent the hook from executing more than once using did_action: http://codex.wordpress.org/Function_Reference/did_action As always, these kinds of hooks take the form {post_status}_{post_type}, so trash_post, trash_page, trash_customposttype, publish_page etc

Trigger custom action when setting button pressed

You need a second form with admin_url(‘admin-post.php’) as form action. Then you can hook into admin_post_custom_action to execute your action. Sample code: add_action( ‘admin_post_wpse_79898’, ‘wpse_79898_test’ ); function wpse_79898_test() { if ( isset ( $_GET[‘test’] ) ) echo esc_html( $_GET[‘test’] ); die( __FUNCTION__ ); } In your settings page: <form action=”<?php echo admin_url( ‘admin-post.php’ ); ?>”> … Read more