I've got some code that updates the functionality of the Insert Hyperlink button in TinyMCE (WP5.9.x) so that I can create links to "Draft" and "Pending" posts, in addition to posts that are already published.
The code (in my functions.php) follows:
function adventuretacoplugin_wp_link_query_args( $query ) {
//Abort early if the user will never see TinyMCE
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') && get_user_option('rich_editing') == 'true')
return;
$post_status_array[] = $query['post_status'];
$query['post_status'] = array_push($post_status_array, 'draft', 'pending', 'auto-draft');
return $query;
}
function update_drafts( $result) {
$post = get_post( $result['ID'] );
if (in_array($post->post_status, array('draft', 'pending', 'auto-draft', 'future'))) {
$my_post = clone $post;
$my_post->post_status = 'publish';
$my_post->post_name = sanitize_title($my_post->post_name ? $my_post->post_name : $my_post->post_title, $my_post->ID);
$result['permalink'] = get_permalink($my_post);
$result['info'] = '(' . $post->post_status . ') ' . $result['info'];
} else {
// just pass through the $result
}
return $result;
}
function adventuretacoplugin_wp_link_query( $results ) {
foreach ( $results as &$result ) {
$result = update_drafts ( $result );
}
return $results;
}
add_filter( 'wp_link_query_args', 'adventuretacoplugin_wp_link_query_args' );
add_filter( 'wp_link_query', 'adventuretacoplugin_wp_link_query' );
This has been working splendidly for the last couple of years while my WP has been running in a Windows IIS-based, PHP7.x environment.
The modifications above returned what I consider to be "linkable" items of various post_types (post, pages, custom post types).
Recently, I migrated to a Linux-based, PHP8.x environment and now I'm seeing strange behavior. I'm getting a bunch of "duplicate" items returned in the Insert Hyperlink dropdown - essentially, all the the post_type = post items have a second entry in the list, and say "Media" on the right side (where usually the publish date of the post would be shown).
Since this all happens as part of an ajax call, I'm not sure of the best way to debug this. Any help would be appreciated, or, if there is an obvious problem with my code due to some PHP7/8 issue that I don't know about, I'd love to know that as well.
Thanks!
