Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Last active June 6, 2021 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Jany-M/ba36a0499e4fb505105e to your computer and use it in GitHub Desktop.
Save Jany-M/ba36a0499e4fb505105e to your computer and use it in GitHub Desktop.
[WordPress] Custom column with clone button to other custom post type
<?php
// Duplicate function
function duplicate_content(){
global $wpdb;
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'duplicate_content' == $_REQUEST['action'] ) ) ) {
wp_die('No post to duplicate has been supplied!');
}
// Get original ID
$post_id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
$post = get_post( $post_id );
// Get Post Type
$rp_post_type = (isset($_GET['post_type']) ? $_GET['post_type'] : $_POST['post_type']);
// Is it a cloned child?
$rp_post_parent = (isset($_GET['post_parent']) ? $_GET['post_parent'] : $_POST['post_parent']);
/*
* if you don't want current user to be the new post author,
* then change next couple of lines to this: $new_post_author = $post->post_author;
*/
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
/*
* if post data exists, create the post duplicate
*/
if (isset( $post ) && $post != null) {
// If post type isnt set, then clone post type
if($rp_post_type == '') {
$rp_post_type = $post->post_type;
}
// If it's not a child, then clone post parent
if($rp_post_parent == '') {
$rp_post_parent = $post->post_parent;
}
// If it's a child, then take the parent's post_status
if($rp_post_parent != '') {
$rp_post_status = get_post_status($post->ID);
} else {
$rp_post_status = 'draft';
}
/*
* new post data array
*/
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $rp_post_parent/*$post->post_parent*/,
'post_password' => $post->post_password,
'post_status' => $rp_post_status,
'post_title' => $post->post_title,
'post_type' => $rp_post_type/*$post->post_type*/,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
/*
* insert the post by wp_insert_post() function
*/
$new_post_id = wp_insert_post( $args );
/*
* get all current post terms ad set them to the new post draft
*/
$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
/*
* duplicate all post meta just in two SQL queries
*/
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos)!=0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
/*
* finally, redirect to the edit post screen for the new draft
*/
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
} else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}
add_action( 'admin_action_duplicate_content', 'duplicate_content' );
// Add Column
function add_custom_column($columns) {
$columns['divisions_custom'] = 'Divisions';
return $columns;
}
add_filter( 'manage_my-post-type_posts_columns', 'add_custom_column' );
// Add content to Column
function custom_division_column( $column, $post_id ) {
global $post;
$cur_usr = wp_get_current_user();
$cur_usr_role = $cur_usr->roles[0];
if($post->post_parent == 0) { // Exclude cloning for children posts
switch ( $column ) {
case 'divisions_custom' :
$post_obj = get_post ($post_id);
$post_title = $post_obj->post_title;
$post_status = array( 'publish', 'pending', 'draft', 'future', 'private' );
echo '<ul>';
// DIVISION 1
$args = array(
'post_type' => 'division-one',
'posts_per_page' => 1,
'post_parent' => 0,
's' => $post_title,
'post_status' => $post_status
);
// Search if Post already exists in this division
$division_query = new WP_Query($args);
// Check if user is authorized to clone in this division
if($cur_usr_role !== 'divisionone_author' && !current_user_can('moderate_comments')) {
$cant_click = ' class="unclickable"';
$can_edit = 'You are not authorized to edit in this Division';
$can_edit_draft = 'You are not authorized to edit in this Division';
$copy = 'You are not authorized to copy to this Division';
} else {
$cant_click = '';
$can_edit = 'Custom Post Type is already available &amp; editable in this Division';
$can_edit_draft = 'Custom Post Type is already available in this Division, but may require further review before publishing';
$copy = 'Custom Post Type is not available yet in this Division, click to Copy &amp; Edit Custom Post Type to this Division';
}
// If post already exists in this division...
if($division_query->have_posts()) : while ( $division_query->have_posts() ) : $division_query->the_post();
if(get_post_status() == 'publish') {
echo '<li class="green"><a href="'.get_edit_post_link().'" title="'.$can_edit.'"'.$cant_click.'>'.strtoupper(str_replace('division-', '', get_post_type())).'<i class="fa fa-check"></i></a></li>';
} else {
echo '<li class="blu"><a href="'.get_edit_post_link().'" title="'.$can_edit_draft.'"'.$cant_click.'>'.strtoupper(str_replace('-division', '', get_post_type())).'<i class="fa fa-search"></i></a></li>';
}
endwhile;
else : // If not, then enable cloning
echo '<li class="red"><a href="admin.php?action=duplicate_content&amp;post='.$post_id.'&amp;post_type=division-one" target="_blank" title="'.$copy.'"'.$cant_click.'>DIVISION 1<i class="fa fa-files-o"></i></a></li>';
endif; wp_reset_query(); wp_reset_postdata();
// DIVISION 2
$args = array(
'post_type' => 'division-two',
'posts_per_page' => 1,
'post_parent' => 0,
's' => $post_title,
'post_status' => $post_status
);
$division_query = new WP_Query($args);
if($cur_usr_role !== 'divisiontwo_author' && !current_user_can('moderate_comments')) {
$cant_click = ' class="unclickable"';
$can_edit = 'You are not authorized to edit in this Division';
$can_edit_draft = 'You are not authorized to edit in this Division';
$copy = 'You are not authorized to copy to this Division';
} else {
$cant_click = '';
$can_edit = 'Custom Post Type is already available &amp; editable in this Division';
$can_edit_draft = 'Custom Post Type is already available in this Division, but may require further review before publishing';
$copy = 'Custom Post Type is not available yet in this Division, click to Copy &amp; Edit Custom Post Type to this Division';
}
if($dvision_query->have_posts()) : while ( $division_query->have_posts() ) : $division_query->the_post();
if(get_post_status() == 'publish') {
echo '<li class="green"><a href="'.get_edit_post_link().'" title="'.$can_edit.'"'.$cant_click.'>'.strtoupper(str_replace('division-', '', get_post_type())).'<i class="fa fa-check"></i></a></li>';
} else {
echo '<li class="blu"><a href="'.get_edit_post_link().'" title="'.$can_edit_draft.'"'.$cant_click.'>'.strtoupper(str_replace('division-', '', get_post_type())).'<i class="fa fa-search"></i></a></li>';
}
endwhile;
else :
echo '<li class="red"><a href="admin.php?action=duplicate_content&amp;post='.$post_id.'&amp;post_type=division-two" target="_blank" title="'.$copy.'"'.$cant_click.'>DIVISION 2<i class="fa fa-files-o"></i></a></li>';
endif; wp_reset_query(); wp_reset_postdata();
echo '</ul>';
break;
}
} else { // Post is a child, display message instead of division icons/cloning
echo '<small>This Custom Post Type is a child and can only be created from an existing parent, within its own Division</small>';
}
}
add_action( 'manage_my-post-type_posts_custom_column' , 'custom_division_column', 10, 2 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment