Skip to content

Instantly share code, notes, and snippets.

@davilera
Last active Jan 15, 2018
Embed
What would you like to do?

Bulk Edit example in WordPress.

<?php
add_filter( 'manage_posts_columns', 'nelio_add_social_resharing_column', 10, 2 );
function nelio_add_social_resharing_column( $columns, $post_type ) {
if ( 'post' !== $post_type ) {
return $columns;
}//end if
$columns['nelio_social_resharing'] = __( 'Social Reshare', 'nelio' );
return $columns;
}//end nelio_add_social_resharing_column()
<?php
add_action( 'manage_posts_custom_column', 'nelio_set_social_resharing_column_value', 10, 2 );
function nelio_set_social_resharing_column_value( $column, $post_id ) {
if ( 'nelio_social_resharing' !== $column ) {
return;
}//end if
if ( nelio_exclude_post_from_resharing( $post_id ) ) {
_e( 'Disabled', 'nelio' );
} else {
_e( 'Enabled', 'nelio' );
}//end if
}//end nelio_set_social_resharing_column_value()
<?php
add_action( 'bulk_edit_custom_box', 'nelio_maybe_add_bulk_edit_for_social_resharing', 10, 2 );
function nelio_maybe_add_bulk_edit_for_social_resharing( $column, $post_type ) {
if ( 'nelio_social_resharing' !== $column ) {
return;
}//end if
if ( 'post' !== $post_type ) {
return;
}//end if
?>
<label class="alignleft">
<span class="title"><?php esc_html_e( 'Social Networks', 'nelio-content' ); ?></span>
<select name="nelio_reshare">
<option value="no-change"><?php _e( '&mdash; No Change &mdash;', 'nelio-content' ); ?></option>
<option value="exclude"><?php esc_html_e( 'Exclude from Automatic Reshare in social networks', 'nelio-content' ); ?></option>
<option value="include"><?php esc_html_e( 'Include in Automatic Reshare in social networks', 'nelio-content' ); ?></option>
</select></label>
<?php
}//end nelio_maybe_add_bulk_edit_for_social_resharing()
<?php
add_action( 'save_post', 'nelio_update_social_resharing_on_post_save' ), 10, 2 );
function nelio_update_social_resharing_on_post_save( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}//end if
if ( 'post' !== $post->post_type ) {
return;
}//end if
$reshare = false;
if ( isset( $_REQUEST['nelio_reshare'] ) ) {
$reshare = sanitize_text_field( wp_unslash( $_REQUEST['nelio_reshare'] ) );
}//end if
switch ( $reshare ) {
case 'no-change':
// Do nothing.
break;
case 'exclude':
nelio_exclude_from_reshare( $post_id );
break;
case 'include':
nelio_include_in_reshare( $post_id );
break;
}//end switch
}//end nelio_update_social_resharing_on_post_save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment