Is there any action filter/hook for validating a custom field before publishing the post?

At the beginning of wp_insert_post, the function that saves/updates a post, there is a filter called wp_insert_post_empty_content. By default this filter checks whether the title, editor, and excerpt fields are all empty, in which case the save process will be halted. However, since all the fields to be saved are passed to this filter, you … Read more

add_role() run only once?

The user roles and capabilities are saved in the database so once you have you have used add_role() its saved and then next load WordPress will know that role just like the built in roles. Now if you look at the function add_role() more specifically at line 141 you will see that it only saves … Read more

When to use add_action(‘init’) vs add_action(‘wp_enqueue_scripts’)

A lot of plugin developers don’t do things the right way. The right way is to hook on to wp_enqueue_scripts like you’re trying to do. However, here’s the order of the hooks run in a typical request: muplugins_loaded registered_taxonomy registered_post_type plugins_loaded sanitize_comment_cookies setup_theme load_textdomain after_setup_theme auth_cookie_malformed auth_cookie_valid set_current_user init widgets_init register_sidebar wp_register_sidebar_widget wp_default_scripts wp_default_stypes admin_bar_init … Read more

How to do_action and get a return value?

The cool thing is a filter is the same as an action, only it returns a value, so just set it up as a filter instead: add_filter( ‘myplugin_clean_logs’, array( ‘MyPlugin_Logs’, ‘clean_logs’ ) ); Then something like: $affected_rows=””; $affected_rows = apply_filters( ‘myplugin_clean_logs’, $affected_rows ); should pass $affected_rows to clean_logs() (and whatever other functions you may have … Read more

How to pass parameters to admin_notices?

I think a better implementation would be a “message” class e.g.: class WPSE_224485_Message { private $_message; function __construct( $message ) { $this->_message = $message; add_action( ‘admin_notices’, array( $this, ‘render’ ) ); } function render() { printf( ‘<div class=”updated”>%s</div>’, $this->_message ); } } This allows you to instantiate the message at any time prior to rendering: … Read more

after_setup_theme always runs

SOLUTION: after_switch_theme does exactly what I intended here. It fires after the theme is switched TO your theme. One of the solutions mentioned below uses switch_theme. This does not have the desired results, since it only happens upon switching away from your theme. Here is an article that I found as reference: http://core.trac.wordpress.org/ticket/7795#comment:29 Here is … Read more