在视频的wp_editor()、tinyMCE和the_content过滤器方面,我遇到了困难。
我正在输出一个前端wp_editor()表单,允许注册用户从站点的前端创建新的帖子。创建的新帖子是自定义的post类型。
目标行为是:
除了视频oEmbed之外,一切都像预期的那样工作。
如果将视频链接添加到内容中(在wp_editor中的新行上),则Ajax回调生成的内容包括包装在段落标记中的视频URL -- oEmbed没有工作,尽管oEmbed已经应用了“the_content”过滤器。
刷新页面,在循环中显示新的帖子,内容由the_content()标记显示-视频显示正确(oEmbed工作过)。
在'wpautop' => false参数中设置wp_editor无助于--破坏格式,也不修复视频。
是否有我缺少的tinyMCE设置?
我如何应用'the_content‘过滤器和/或为Ajax回调构建一个HTML有问题吗?
相关代码如下所示。
谢谢!
JQuery
(function( $ ) { 'use strict';
$(function() {
$('#student-submission-button').click( function(event) {
// Prevent default action
// -----------------------
event.preventDefault();
var submission_nonce_id = $('#the_nonce_field').val();
var submission_title = $('#inputTitle').val();
tinyMCE.triggerSave();
var submission_content = $('#editor').val();
var Data = {
action: 'student_submission',
nonce: submission_nonce_id,
workbook_ID: submission_workbook_ID,
content: submission_content,
title: submission_title,
};
// Do AJAX request
$.post( ajax_url, Data, function(Response) {
if( Response ) {
var submissionStatus = Response.status;
var submissionMessage = Response.report;
var postHTML = Response.content;
if ( 'success' == submissionStatus ) {
$('#user-feedback').html( submissionMessage );
$('#new-post').append( postHTML );
}
// Hide the form
$('.carawebs-frontend-form').hide(800, function() {
$(this).remove();
});
}
});
});
});
})( jQuery );PHP
/**
* Return data via Ajax (excerpt)
*
*
*/
$response = array();
if( is_int( $new_submission_ID ) ) {
// Build a success response
// ------------------------
$new_post = get_post( $new_submission_ID, OBJECT );
$new_post_content = $new_post->post_content;
$return_content = "<h2>$new_post->post_title</h2>";
$return_content .= apply_filters( 'the_content', $new_post_content );
$response['status'] = "success";
$response['report'] = "New post created, ID: $new_submission_ID";
$response['content'] = $return_content;
} else {
// error report
}
wp_send_json( $response ); // send $response as a JSON object表单HTML和wp_editor()
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post" enctype="multipart/form-data" class="carawebs-frontend-form">
<label for="inputTitle">Title</label>
<input type="text" class="form-control" id="inputTitle" name="inputTitle" placeholder="Title" value="" />
<label for="inputContent" class="topspace">Your Content</label>
<?php
$args = array(
'textarea_rows' => 45,
'teeny' => false,
'editor_height' => 400,
'editor_class' => 'cwfrontendadmin',
'quicktags' => false,
'textarea_name' => 'cw_content',
'tinymce' => array(
'content_css' => get_template_directory_uri() . '/assets/css/editor-style.css'
),
);
wp_editor( 'Enter your content...', 'editor', $args );
wp_nonce_field('name_of_action','the_nonce_field', true, true ); // name of action, name of nonce field
?>
<input id="student-submission-button" class="btn btn-primary" type="submit" name="submission-form" value="Save Content" />
更新
我已经将其缩小到了应用the_content过滤器的方式。我认为过滤后的内容是缓存的,所以如果post内容在循环之外返回,oEmbed可能不会应用于所有内容。
现在我有了视频oEmbed工作--使用了将post_content插入变量的另一种方法:
<?php
global $post;
$post = get_post($new_submission_ID);
setup_postdata( $post );
$new_content = apply_filters('the_content', get_the_content());
$new_post_link = get_the_permalink();
$new_post_title = get_the_title();
wp_reset_postdata( $post );这很好,但是如果有人能解释为什么最初构建HTML的方法不起作用,那就太好了。
发布于 2015-08-04 09:40:11
如果在循环之外返回post内容,并且没有设置oEmbed过滤器,则不会应用global $post筛选器。
这是因为视频嵌入内容缓存在postmeta表中,如果在循环外返回post内容,则无法访问。WP_Embed类是由the_content过滤器连接到的,它不是为在循环之外使用而设计的-这是在Trac 这里上引用的。
下面的方法返回与原始场景相同的工作视频oEmbed。需要设置global $post以使缓存的视频嵌入数据可用:
<?php
global $post;
$post = get_post( $new_submission_ID );
setup_postdata( $post );
$new_content = apply_filters( 'the_content', get_the_content() );
wp_reset_postdata( $post );
// return what you need via Ajax callback -
// $new_content contains the proper video embed HTMLTL;DR版本
如果需要在循环之外应用oEmbed过滤器,则需要设置全局post变量,以便WP_Embed类能够访问post的元数据中缓存的视频嵌入html。
发布于 2015-07-29 15:39:53
根据其他来源的说法,原因是WP_Embed::shortcode() (负责将视频交换给嵌入的html)正在调用global $post变量获取信息。
这意味着您需要确保global $post变量包含您请求的帖子的信息。
我的AJAX响应方法现在集成了global $post变量:
global $post;
$post = get_post( $id );
echo apply_filters( 'the_content', $post->post_content );
die();这本质上只是对您自己发现的一个扩展,但我认为,对于oEmbed + AJAX的WordPress问题,有一个清晰的答案/解决方案可能会有所帮助。
https://stackoverflow.com/questions/28364568
复制相似问题