Developer: Post actions

Post actions are functions that can be applied to posts, either singly or in bulk. Both need to first be announced to Broadcast, and then they need to be handled.

Single actions

Announce that you have an action that can be applied to a parent post. Child posts can only be unlinked.

add_action( 'threewp_broadcast_get_post_actions', 'my_threewp_broadcast_get_post_actions' );

Now add your post action.

function my_threewp_broadcast_get_post_actions( $action )
{
 $a = new \threewp_broadcast\posts\actions\action;
 $a->set_action( 'wink' );
 $a->set_name( 'Wink at the children' );
 $action->add( $a );
}

Your action will now be presented in the action select when a user clicks on a parent post.

We must now handle the action.

add_action( 'threewp_broadcast_post_action', 'my_threewp_broadcast_post_action' );
function threewp_broadcast_post_action( $action )
{
 // Check that the $action->action is 'wink'
 // Apply the action on the $action->post_id.
 // See includes/traits/post_actions.php
}

Bulk actions

Similar to single actions, except that they can be applied to several posts at once.

add_action( 'threewp_broadcast_get_post_bulk_actions', 'my_threewp_broadcast_get_post_bulk_actions' );

Here it diverges somewhat from the single actions: bulk actions only function due to javascript manipulation of the bulk actions select input. WordPress does not yet offer any hooks into which devs can add custom bulk actions.

In keeping with the javascript manipulation, the pressing of the apply button is taken over by Broadcast javascript and the bulk action is handed over to bulk action javascripts.

You have the choice of either handling the button press yourself, or using the skeleton \threewp_broadcast\posts\actions\bulk\wp_ajax post bulk action. Using this action, everthing is set up for you, and all you have to do is just handle the ajax action in WordPress code.

add_action( 'wp_ajax_broadcast_post_bulk_action', 'my_wp_ajax_broadcast_post_bulk_action' );
function my_wp_ajax_broadcast_post_bulk_action( $action )
{
 // See includes/traits/post_actions.php
}

Remove some built-in post bulk actions

The following code will remove the delete and find unlinked bulk post actions for all users.

add_action( 'threewp_broadcast_get_post_bulk_actions', 'remove_dangerous_broadcast_post_bulk_actions', 100 );
function remove_dangerous_broadcast_post_bulk_actions( $action )
{
 foreach( $action->actions as $index => $bulk_action )
 {
  // Here we know that the bulk actions we are looking for have this general action
  // See threewp_broadcast_get_post_bulk_actions() in src/traits/post_actions.php
  if ( $bulk_action->data[ 'action' ] !== 'broadcast_post_bulk_action' )
   continue;
  // The specific action is stored in the subaction array key.
  if ( in_array( $bulk_action->data[ 'subaction' ], [ 'delete', 'find_unlinked' ] ) )
   $action->actions->forget( $index );
 }
}

More examples

For other examples, see the following add-ons:

Comments

  1. Hi!
    You created great plugin!

    Could you help me implement this:
    I broadcasted post, after that I changed status of child post (broadcasted post) to “Draft”.
    Expected result: when I visit child post – redirect me to home page of this blog. Also I should hide hreflang on parent post.

    Many thanks.
    Paul

    1. That’s quite a lot to do. Perhaps contact me via e-mail and I can point you in the right direction(s)?

Leave a Reply

Your email address will not be published. Required fields are marked *