Strange behaviour with add_{$meta_type}_metadata | add_post_metadata filter

Ok I’ve found the solution… I have had to call add_filter outside my plugin class, because of WordPress hooks priority. With the actual code, I call add_filter after the ‘init’ event, that’s probably too late. If I move this line of code: add_filter(‘add_post_metadata’,array(&$this,’filter_negozi_add_metadata’),10,5); on top of the file, the hook works like expected. So to … Read more

Implement Hooks Using Array

What’s wrong with good old do_action? 😉 You could write a simple wrapper: function wpse81578_hook( $hook ) { do_action( $hook ); } If you’re looking for something dynamic, take a look at the do_atomic function of the Hybrid theme framework: it “adds contextual action hooks to the theme. This allows users to easily add context-based … Read more

new_to_publish fires multiple times

OK! found how to – there is 2 little information about how to attach a function to a status change but this hook does the job and does it well. function miniupdate_webnews( $new_status, $old_status, $post ) { if ( $old_status == ‘draft’ && $new_status == ‘publish’ ) { // YOUR FUNCTION HERE… } } add_action( … Read more

WordPress capabilities and restricted categories access

I think best hook should be ‘template_redirect’, when this hook is fired main query is already set, so you can look at queried object and if the user has no required capability you can redirect request somewhere: add_action( ‘template_redirect’, function() { if ( ( is_category( ‘special-category’ ) || is_singular() && has_category( ‘special-category’, get_queried_object() ) ) … Read more

Get post or page id early

For mee it seems that the template_redirect hook worked. Thanks to Pieter Goosen. Heres a solution that should work on earlier hooks: function gdymc_object_exists( $object_id ) { return ( get_the_title( $object_id ) ) ? true : false; } function gdymc_objectID() { if( is_numeric( $_GET[‘page_id’] ) ): $object_id = $_GET[‘page_id’]; elseif( is_numeric( $_GET[‘p’] ) ): $object_id … Read more

Hook for when a page template is changed

If you want to toggle the editor “on the fly”, you’ll need to revert to a pure JavaScript solution, and only ever “visually” hide it (as opposed to removing it server-side): function wpse_189693_hide_on_template_toggle() { $screen = get_current_screen(); if ( $screen && $screen->id === ‘page’ ) : ?> <script> jQuery( “#page_template” ).change( function() { jQuery( “#postdivrich” … Read more

Is it possible to change image urls by hooks?

As you want to change (use different) URL for featured image only, you can use pre_option_upload_url_path hook. // Setting the uploads directory URL function wpse_change_featured_img_url() { return ‘http://www.example.com/media/uploads’; } add_filter( ‘pre_option_upload_url_path’, ‘wpse_change_featured_img_url’ ); This hook will not change URLs permanently but it will set uploads directory to some different URL temporally. This will also not … Read more

Understanding WP

The reason hooks ( Filters and Actions ) are in these templates is specifically so you don’t need to copy the templates into your theme folder. The biggest issue with copying WooCommerce templates into your theme is that often time whenever WooCommerce updates they also update the templates. Then you’ll need to bring your changes … Read more