The snippet below shows how to broadcast a broadcastable shortcode using a value from a custom field.
For example, you have a Gravity Form ID in a custom field and want:
- the form ID detected
- the form broadcasted to the child blog
- the form ID value in the custom field updated
This example requires the Gravity Forms Broadcast add-on, of course.
// There are three CONFIG variables for you to customize.
add_action( 'threewp_broadcast_broadcasting_started', 'my_threewp_broadcast_broadcasting_started' );
/**
@brief Broadcast a broadcastable shortcode, perhaps a Gravity Form, using a value from a custom field.
@details This involves using the item ID from the field to fake a shortcode that is sent to be preparsed and then parsed.
@since 2018-02-20 11:15:21
**/
function my_threewp_broadcast_broadcasting_started( $action )
{
/**
@brief CONFIG: This is the name of the custom field which contains the item ID.
@since 2018-02-20 11:22:00
**/
$custom_field_key = 'the_custom_field_name';
/**
@brief CONFIG: This is the shortcode you want to fake.
@since 2018-02-20 11:26:40
**/
$shortcode = '[gravityform id="%d"]';
/**
@brief CONFIG: This is the key in the shortcode you want to put back into the custom field.
@since 2018-02-20 11:36:27
**/
$item_key = 'id';
// Done with config!
$bcd = $action->broadcasting_data;
$custom_field_id = $bcd->custom_fields()->get_single( $custom_field_key );
// There must be a custom field with a valid value.
if ( $custom_field_id < 1 )
return;
$bcd->fake_shortcode = (object)[];
$bcd->fake_shortcode->content = sprintf( $shortcode, $custom_field_id );
$bcd->fake_shortcode->custom_field_key = $custom_field_key;
$bcd->fake_shortcode->id = $custom_field_key;
$bcd->fake_shortcode->item_key = $item_key;
// Tell all Broadcast add-ons to process this shortcode.
$action = ThreeWP_Broadcast()->new_action( 'preparse_content' );
$action->broadcasting_data = $bcd;
$action->content = $bcd->fake_shortcode->content;
$action->id = $bcd->fake_shortcode->id;
$action->execute();
}
add_action( 'threewp_broadcast_broadcasting_before_restore_current_blog', 'my_threewp_broadcast_broadcasting_before_restore_current_blog' );
/**
@brief This will get the equivalent item ID on this blog and put it back in the custom field.
@since 2018-02-20 11:29:42
**/
function my_threewp_broadcast_broadcasting_before_restore_current_blog( $action )
{
$bcd = $action->broadcasting_data;
if ( ! isset( $bcd->fake_shortcode ) )
return;
// And now process to get the item broadcasted.
$action = ThreeWP_Broadcast()->new_action( 'parse_content' );
$action->broadcasting_data = $bcd;
$action->content = $bcd->fake_shortcode->content;
$action->id = $bcd->fake_shortcode->id;
$action->execute();
// $action->content will contain the new shortcode.
// We need to extract the ID attribute from it.
// Remove the []
$action->content = trim( $action->content, '[]' );
// And break it out into an array.
$atts = shortcode_parse_atts( $action->content );
$item_key = $bcd->fake_shortcode->item_key;
$new_value = $atts[ $item_key ];
ThreeWP_Broadcast()->debug( 'Replacing custom field %s with %s', $bcd->fake_shortcode->custom_field_key, $new_value );
// And now update the custom field.
$bcd->custom_fields()
->child_fields()
->update_meta( $bcd->fake_shortcode->custom_field_key, $new_value );
}