WordPress动态Gutenberg块未在编辑器中初始化平滑旋转木马

我用创建了一个动态古腾堡块,该块是用PHP呈现的。cronjob每晚都会将客户评论导入MySQL数据库。该块在一个光滑的旋转木马中显示这些客户评论。 前端的一切都很好,但该块不会在编辑器(后端)中初始化光滑的旋转木马。看起来根本没有执行JavaScript。我已经将我的呈现PHP文件简化为以下内容: 索引.php /** * Server-side rendering of the dynamic \"mna/testimonial\" block. */function =>> Behaviour in the frontend: H1 is displayed, Alert is displayed Behaviour in the editor:H1 is displayed, Alert is not displayed I added setTimeout out of desperation and got the same result, still no alert. I tried using a customEvent正如coderaaron在GitHub的评论中所描述的:https://github.com/WordPress/gutenberg/issues/12603#issuecomment-513945557 但是由于警报不起作用,它也不会执行document.addEventListener。 你知道为什么JS没有运行吗在编辑器中执行? @maaarsl-抱歉,另一个回复是个人资料垃圾邮件发送者。他们的回复和你混乱的回复已经从公众视野中删除。 你的意思是旋转木马的JS没有运行吗?我怀疑它甚至没有载入页面。JS代码通常只为前端请求加载。对于后端屏幕,脚本需要从完全不同的操作挂钩(adminu enqueueu scripts)排队 @bcworkz–感谢您的回复! 我怀疑它甚至没有被加载到页面上。 它被加载了,它在DOM中,但是没有被执行。我将尝试对我构建的内容进行更深入的了解: 索引.php 两个文件(slick.css和slick.min.js)都加载在前端和编辑器中。我已经检查过多次了。 /* * Enqueues 3rd party JavaScript and CSS. */function mna_gutenberg_enqueue_external_assets() {wp_enqueue_style(\"slick-carousel-style\", // Handle, should be unique.plugins_url(trailingslashit(basename(__DIR__)).\"external/slick-carousel/slick.css\"),array(), // Dependenciesfilemtime(PLUGIN_DIR.\"external/slick-carousel/slick.css\") // Version);wp_enqueue_script(\"slick-carousel\", // Handle, should be unique.plugins_url(trailingslashit(basename(__DIR__)).\"external/slick-carousel/slick.min.js\"),array(\"jquery\"),filemtime(PLUGIN_DIR.\"external/slick-carousel/slick.min.js\"), // Versiontrue // Load script in footer.);}add_action(\"enqueue_block_assets\", \"mna_gutenberg_enqueue_external_assets\"); 索引.js 注册一个块类别和所有我的块。 /** * WordPress dependencies */import { registerBlockType } from \"@wordpress/blocks\";/** * Register block category first. */import \"./utils/block-category\";/** * Internal dependencies */import * as testimonial from \"./blocks/testimonial\";import * as mediaTeaser from \"./blocks/media-teaser\";/** * Function to register an individual block. ** @param {Object} block The block to be registered. => block/index.js Everything is pretty default here, except the save函数,返回null,因为它是一个带有服务器端渲染的动态块。 import icons from \"../../utils/icons\";import edit from \"./edit\";export> block/edit.js Contains the edit块的函数。 /** * Block edit function */class TestimonialEdit extends Component {render () {const> block/index.php Contains the rendering PHP function and an inline JavaScript block for the initialization of the slick carousel. function = // I\"ve removed the function which gets the reviews out of the db to make it easier to read. $review_rows is a php array with objects inside.if(!empty($review_rows)) {$output => array(// I\"ve removed all of the attributes here to make it easier to> $js_output只是一个内联JavaScript块,它同时存在于DOM的前端和编辑器中。但它只在前端工作。在前端我得到警报,它将启动传送带。在编辑器中,我没有收到警报,也没有启动旋转木马。 我知道内联JavaScript块根本不是一个好的解决方案,但我不知道如何使它更好,因为我需要PHP文件中$attributes的值来初始化carousel。 我的错误诊断是一个预感,但对情况并不是很了解。我错了并不奇怪。谢谢你的详细信息。遗憾的是,我没有解决办法。很明显,在编辑页面上发生的事情和编辑器本身发生的事情之间有一个脱节。就像它是一个不同的DOM,虽然这显然不是真正的问题。 如果alert()甚至不执行,显然块渲染中的内联脚本是不可行的。当脚本与wp_localize_script()排队时,我们通常将PHP值传递给JS。这对您没有帮助,因为此时块属性是未知的。我认为carouselinit脚本应该是块回调的一部分,而块回调是注册设置的一部分。但我对自定义块的处理速度不快,所以我真的不知道我在说什么。我所能做的就是祝你好运

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1169

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1070

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this->id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:806

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this->id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:898

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:930

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:877

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:864

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:833

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1726

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1308