Created
May 14, 2018 23:21
-
-
Save swinggraphics/78057a3af32eec21c8bf47c734638528 to your computer and use it in GitHub Desktop.
Replace site_url in posts syndicated by WP Broadcast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/** | |
* Hook into Broadcast | |
* Modify links in the broadcasted post content | |
* "Replace Site URL" checkbox appears in child post meta box to show if this was applied | |
*/ | |
add_action( 'threewp_broadcast_prepare_meta_box', 'my_custom_prepare_meta_box' ); | |
function my_custom_prepare_meta_box( $action ) { | |
$meta_box_data = $action->meta_box_data; // Convenience. | |
$form = $meta_box_data->form; // Convenience | |
$form->prefix( 'broadcast' ); // Create all inputs with this prefix. | |
if ( is_super_admin() OR $this->user_has_roles( $this->get_site_option( 'role_link' ) ) ) { | |
// "Replace Site URL" should be checked by default | |
$checked = 'add' == get_current_screen()->action ? true : 'on' == get_post_meta( $meta_box_data->post_id, '_my_custom_broadcast_replace_site_url', true ); | |
$site_url_input = $form->checkbox( 'replace_site_url' ) | |
->checked( $checked ) | |
// Input label for meta box | |
->label( __( 'Replace Site URL', 'my_custom' ) ) | |
// Input title for meta box | |
->title( __( 'Replace the domain name in links to match the child site URL.', 'my_custom' ) ); | |
$meta_box_data->convert_form_input_later( 'replace_site_url' ); | |
} | |
} | |
add_action( 'threewp_broadcast_broadcasting_modify_post', 'my_custom_broadcasting_modify_post', 20 ); | |
function my_custom_broadcasting_modify_post( $action ) { | |
$bcd = $action->broadcasting_data; | |
$form = $bcd->meta_box_data->form; | |
$replace_site_url = $form->checkbox( 'replace_site_url' )->get_post_value(); // 'on' or NULL | |
if ( $replace_site_url ) { | |
$parent_site_url = get_site_url( $bcd->parent_blog_id ); | |
$child_site_url = get_site_url( $bcd->current_child_blog_id ); | |
$modified_content = $bcd->modified_post->post_content; | |
$modified_content = str_replace( $parent_site_url, $child_site_url, $modified_content ); | |
$bcd->modified_post->post_content = $modified_content; | |
} | |
// Update our custom Broadcast option meta field value… | |
$_my_custom_broadcast_replace_site_url = $replace_site_url ? 'on' : 'off'; | |
$bcd->custom_fields->original[ '_my_custom_broadcast_replace_site_url' ] = $_my_custom_broadcast_replace_site_url; | |
// …and on parent site | |
switch_to_blog( $bcd->parent_blog_id ); | |
update_post_meta( | |
$bcd->broadcast_data->post_id, | |
'_my_custom_broadcast_replace_site_url', | |
$_my_custom_broadcast_replace_site_url | |
); | |
restore_current_blog(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is exactly the solution we needed for a customer of ours.
Many thanks and greetings from pxMEDIA.