admin管理员组文章数量:1289508
I'm working on an internal knowledgebase and I want to show the post history on the front end (with something like author, date and difference).
I first landed on wp_get_post_revision()
(/) and after more searching I found wp_list_post_revisions()
(/) which looks closer to what I want.
However, while messing with this I'm running into errors while updating posts (Updating failed. The response is not a valid JSON response.) and I don't see anything on the front end when I return the shortcode. I was hoping there was a plugin for this but since I cannot find one I'm creating a quick one. What am I doing wrong and what's the best way to accomplish this?
Here is what I've tried:
function history_shortcode() {
//global $wpdb;
//$revisions = $wpdb->get_results("select * from {$wpdb->posts} where post_parent={$post_id} and post_type='revision'");
$revisions = wp_list_post_revisions(get_the_ID());
return $revisions;
}
add_shortcode( 'history', 'history_shortcode' );
I'm working on an internal knowledgebase and I want to show the post history on the front end (with something like author, date and difference).
I first landed on wp_get_post_revision()
(https://developer.wordpress/reference/functions/wp_get_post_revision/) and after more searching I found wp_list_post_revisions()
(https://developer.wordpress/reference/functions/wp_list_post_revisions/) which looks closer to what I want.
However, while messing with this I'm running into errors while updating posts (Updating failed. The response is not a valid JSON response.) and I don't see anything on the front end when I return the shortcode. I was hoping there was a plugin for this but since I cannot find one I'm creating a quick one. What am I doing wrong and what's the best way to accomplish this?
Here is what I've tried:
function history_shortcode() {
//global $wpdb;
//$revisions = $wpdb->get_results("select * from {$wpdb->posts} where post_parent={$post_id} and post_type='revision'");
$revisions = wp_list_post_revisions(get_the_ID());
return $revisions;
}
add_shortcode( 'history', 'history_shortcode' );
Share
Improve this question
asked Jan 29, 2021 at 3:43
DerekDerek
3732 gold badges6 silver badges17 bronze badges
1 Answer
Reset to default 2Your shortcode calls wp_list_post_revisions()
which echo the output (revision list), hence you get the "updating error".
To fix the issue, you can use output buffering like so:
ob_start();
wp_list_post_revisions( get_the_ID() );
$revisions = ob_get_clean();
Or you could use wp_get_post_revisions()
and build the HTML list manually. Here's an example based on the wp_list_post_revisions()
:
$output = '';
if ( ! $revisions = wp_get_post_revisions( get_the_ID() ) ) {
return $output;
}
$rows = '';
foreach ( $revisions as $revision ) {
if ( ! current_user_can( 'read_post', $revision->ID ) ) {
continue;
}
$rows .= "\t<li>" . wp_post_revision_title_expanded( $revision ) . "</li>\n";
}
$output .= "<ul class='post-revisions hide-if-no-js'>\n";
$output .= $rows;
$output .= '</ul>';
// At the end of your shortcode function, make sure to..
return $output;
本文标签: pluginsShow history of post revisions on front end
版权声明:本文标题:plugins - Show history of post revisions on front end 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741408390a2377085.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论