Created
April 8, 2016 14:38
-
-
Save Jany-M/58d42ed7799e1bbe0590a1912c003d3c to your computer and use it in GitHub Desktop.
[WordPress[ Extend Duplicate Content functions to create clones and identical post children
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
<?php | |
// Requires the duplicate_content() function | |
// https://gist.github.com/Jany-M/ba36a0499e4fb505105e | |
// Add link to create a cloned CHILD of this post, to action list for post_row_actions | |
function duplicate_content_link( $actions, $post ) { | |
global $typenow; | |
$user_id = get_current_user_id(); | |
$user_data = get_userdata($user_id); | |
$user_role = $user_data->roles[0]; | |
// Manipulate user role based on something useful - not required, used later to check user Role | |
$mod_role = str_replace('_author', '', $user_role); | |
if ($post->post_type !== 'page' || $post->post_type !== 'post') { | |
if (!get_post_ancestors($post) && current_user_can('edit_posts')) { | |
// this is not required, it only checks for a specific Role and uses it to determine if they are authorize to create posts in this custom post type | |
if(current_user_can('edit_your-custom-post-type-slug-'.$mod_role.'s') && $typenow != 'your-custom-post-type-slug' || current_user_can('moderate_comments')) { | |
$actions['duplicate'] = '<a href="admin.php?action=duplicate_content&post=' . $post->ID . '&post_parent='.$post->ID.'" title="By creating a Child from this post, this will become the parent" rel="permalink">Create Child</a>'; | |
} | |
} | |
} | |
return $actions; | |
} | |
add_filter( 'page_row_actions', 'duplicate_content_link', 10, 2 ); | |
// Add a button to create a cloned CHILD of this post, in the edit screen | |
function duplicate_content_button( $post ) { | |
global $typenow; | |
$my_custom_post_type_array = array('some-custom-post-type', 'another-custom-post-type'); | |
if(!get_post_ancestors($post) && in_array($typenow, $my_custom_post_type_array)) { | |
echo '<a href="admin.php?action=duplicate_content&post=' . $post->ID . '&post_parent='.$post->ID.'" title="By creating a Child from this post, this will become the parent" rel="permalink" id="duplicate_content_button" class="page-title-action">Create Child</a>'; | |
} | |
} | |
add_action( 'edit_form_top', 'duplicate_content_button', 10, 1 ); | |
// Move the clone CHILD button somewhere nicer - requires an admin style.css | |
function duplicate_content_button_move() { | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready( function($){ | |
$('#duplicate_content_button').appendTo(".wrap h1"); | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'admin_head-post.php', 'duplicate_content_button_move' ); | |
add_action( 'admin_head-post-new.php', 'duplicate_content_button_move' ); | |
// Add the CLONE AS NEW duplicate link to action list for post_row_actions | |
function duplicate_clone_content_link( $actions, $post ) { | |
global $typenow; | |
$user_id = get_current_user_id(); | |
$user_data = get_userdata($user_id); | |
$user_role = $user_data->roles[0]; | |
// Manipulate user role based on something useful - not required, used later to check user Role | |
$mod_role = str_replace('_author', '', $user_role); | |
if($post->post_parent != 0) { | |
$post_parent = '&post_parent='.get_post_ancestors($post)[0]; | |
} else { | |
$post_parent = ''; | |
} | |
if ($post->post_type !== 'page' || $post->post_type !== 'post') { | |
if (current_user_can('edit_posts')) { | |
// this is not required, it only checks for a specific Role and uses it to determine if they are authorize to create posts in this custom post type | |
if(current_user_can('edit_your-custom-post-type-slug-'.$mod_role.'s') && $typenow != 'your-custom-post-type-slug' || current_user_can('moderate_comments')) { | |
$actions['clone'] = '<a href="admin.php?action=duplicate_content&post='.$post->ID.''.$post_parent.'" title="You can clone this Post as a new one, they will be identical and you will be able to edit it" rel="permalink">Clone as New</a>'; | |
} | |
} | |
} | |
return $actions; | |
} | |
add_filter( 'page_row_actions', 'duplicate_clone_content_link', 10, 2 ); | |
// Add the CLONE AS NEW duplicate button in the edit screen | |
function duplicate_clone_content_button( $post ) { | |
global $typenow; | |
if($post->post_parent != 0) { | |
$post_parent = '&post_parent='.$post->ID; | |
} else { | |
$post_parent = ''; | |
} | |
$my_custom_post_type_array = array('some-custom-post-type', 'another-custom-post-type'); | |
if(in_array($typenow, $my_custom_post_type_array)) { | |
echo '<a href="admin.php?action=duplicate_content&post='.$post->ID.''.$post_parent.'" title="You can clone this Post as a new one, they will be identical and you will be able to edit it" rel="permalink" id="duplicate_clone_content_button" class="page-title-action">Clone as New</a>'; | |
} | |
} | |
add_action( 'edit_form_top', 'duplicate_clone_content_button', 10, 1 ); | |
// Move the CLONE AS NEW duplicate button somewhere nicer | |
function duplicate_clone_content_button_move() { | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready( function($){ | |
$('#duplicate_clone_content_button').appendTo(".wrap h1"); | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'admin_head-post.php', 'duplicate_clone_content_button_move' ); | |
add_action( 'admin_head-post-new.php', 'duplicate_clone_content_button_move' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment