• Hi,
    there is an option in admin panel, when you can clone a few posts/pages at once. Is it possible to have the same functionality on frontend?

    For now i added a single button “Clone this post” to each post( via duplicate_post_clone_post_link() ), and users( editor ) have ability to clone post only one by one. Is there a way to add button “Clone all posts” and gave users ability to clone all posts by one click?

    I know, this is strange request, but here is my scenario:
    i have a site with two pages – /homepage and /user.
    /homepage contains all posts from administrator. at this page users( with role “editor” ) have ability to clone each post. each cloned post will appear at /user page. further user can edit these cloned posts( actually only text ) on his page.

    But i want to give a user ability to clone all posts in one click, not one by one. There might be around 60-70 posts, and clone them one by one could be annoying and time costly.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter markilfin

    (@markilfin)

    Here is how i did it:
    1. i added data-id for every post
    <article <?php post_class( 'single-slide' ); ?> id="post-<?php the_ID(); ?>" data-id="<?php the_ID(); ?>"></article>
    2. via js i’m collecting these ids and redirect the page with predefined parameter posts_to_clone:

    const button = document.getElementById('clone-all-slides');
    button.addEventListener('click', () => {
    	let slides = document.getElementsByClassName('single-slide');
    	let ids = '';
    	for (let i = 0; i < slides.length; i++) {
    		ids += (slides.length - 1) !== i ? <code>${slides[i].dataset.id},</code> : <code>${slides[i].dataset.id}</code>;
    	}
    	window.location.href = <code>${window.location.href}wp-admin/edit.php?posts_to_clone=${ids}</code>;
    });

    3. in functions.php i’m checking if parameter posts_to_clone exists and if it does, i’m making clone for every id it has via duplicate_post_create_duplicate:

    function frontend_bulk_clone () {
    	if ( is_admin() && isset( $_GET['posts_to_clone'] ) ) {
    		$posts_to_clone = ( explode( ',', $_GET['posts_to_clone'] ) );
    		foreach ( $posts_to_clone as $post_id ) {
    			$post = get_post( $post_id );
    			if ( ! empty( $post ) ) {
    				if ( ! is_post_type_hierarchical( $post->post_type ) || is_post_type_hierarchical( $post->post_type ) ) {
    					duplicate_post_create_duplicate( $post );
    				}
    			}
    		}
    		wp_redirect( get_site_url() . '/user', 301 );
    		exit;
    	}
    }
    add_action( 'wp', 'frontend_bulk_clone' );

    I know, this is a little bit dirty, but it works.
    Any improvements or remarks are welcome.
    Thanks!

    • This reply was modified 4 years, 6 months ago by markilfin.
    Plugin Author Enrico Battocchi

    (@lopo)

    Hi @markilfin,
    sorry for the delay in answering!

    It looks like a really clever solution!
    In the future, we hope to make things smoother for people who want to call the functions of Duplicate Post from their code, so stay tuned!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Bulk action on frontend’ is closed to new replies.