duplicate-post
duplicate-post copied to clipboard
duplicate_post_get_original & duplicate_post_create_duplicate not working
- [x] I've searched for any related issues and avoided creating a duplicate issue.
Please give us a description of what happened
I'm trying to make use of the functions duplicate_post_get_original & duplicate_post_create_duplicate without success with Yoast Duplicate Post Version 4.5 and Wordpress 6.1.1. The first does not create a duplicate/return the original post. The second seems not to be defined.
duplicate_post_get_original is neither duplicating a post nor returning the original post id. Calling duplicate_post_create_duplicate leads to a fatal/uncaught error: Call to undefined function duplicate_post_create_duplicate()
To Reproduce
Step-by-step reproduction instructions
I've created the folowwing simple test code as page template. Post ID 1 is the "Hello World" Blog Post
<?
duplicate_post_clone_post_link("Link Text","Before","After",1);
?>
<br><br>
<?
$original_post = duplicate_post_get_original(1);
echo "Original post: '" . $original_post->ID . "'";
?>
<br><br>
<?
$old_post = get_post(1);
$new_post_id = duplicate_post_create_duplicate($old_post);
echo "New post: '" . $new_post_id . "'";
?>
Expected results
- A link should be created, to duplicate the blog post with ID 1
- The blog post with ID one should be duplicated, but ID of old blog post (ID: 1) should be echoed
- Another duplicate should be created. ID of created blog post (ID: 3) should be echoed.
Actual results
The actual output looks like the following.
Before
[Link Text](https://logistik.at-fire.de/wp-admin/admin.php?action=duplicate_post_new_draft&post=1&_wpnonce=63f8d20dd5)
After
Warning
: Attempt to read property "ID" on null in
/homepages/14/####/htdocs/####/wp-content/themes/asset-management/geraet-copy.php
on line
32
Original post: ''
Fatal error
: Uncaught Error: Call to undefined function duplicate_post_create_duplicate() in /homepages/14/####/htdocs/####/wp-content/themes/asset-management/geraet-copy.php:37
Stack trace:
#0 /homepages/14/####/htdocs/####/wp-includes/template-loader.php(106): include()
#1 /homepages/14/####/htdocs/####/wp-blog-header.php(19): require_once('/homepages/14/d...')
#2 /homepages/14/####/htdocs/####/index.php(17): require('/homepages/14/d...')
#3 {main} thrown in /homepages/14/####/htdocs/####/wp-content/themes/asset-management/geraet-copy.php on line 37
- duplicate_post_clone_post_link creates the link correctly. On pressing, the blog post gets duplicated.
- duplicate_post_get_original obviously does not return anything / null
- duplicate_post_create_duplicate seems to be undefined.
- Which browser is affected (or browsers):
- [X] Chrome
- [ ] Firefox
- [X] Safari
- [ ] Other:
Used versions
- PHP version: 8.0
- WordPress version: 6.1.1
- WordPress Theme: Own
- Yoast Duplicate Post version: 4.1
Got the same issue
Hey @sebastianhodapp , I figured it out. The reason is that this function is only available from the admin area, so it's not available if you call it somewhere else.
If you put the code below in your function, it should work.
if ( ! function_exists( 'duplicate_post_create_duplicate' ) ) { require_once( plugin_dir_path( __FILE__ ) . 'path-to-plugin-folder/admin-functions.php' ); }