>处理和显示WordPress save_post
挂钩的错误:三种方法
save_post
全局,利用wordpress瞬变,并在重定向中采用get参数。$_SESSION
>挂钩后,用户重定向,打破了执行线程并失去对全局变量的访问。 因此,需要一种机制将错误信息从保存操作传输到重定向页面。
save_post
>
$_SESSION
这个简单的方法将错误消息存储在
$_SESSION
实现:
if ( !session_id() ) { session_start(); } if ($error) { $_SESSION['my_plugin_errors'] = $error->get_error_message(); } // ...in admin_notices hook... add_action( 'admin_notices', 'my_error_message' ); function my_error_message() { if ( isset( $_SESSION['my_plugin_errors'] ) ) { ?> <div class="error"> <p><?php echo $_SESSION['my_plugin_errors']; ?></p> </div> <?php unset( $_SESSION['my_plugin_errors'] ); } }轻松实现,没有数据库开销。
- cons:>不是标准的WordPress练习;依靠会话处理,这可能不会在所有设置中始终启用。
> - 方法2:使用WordPress瞬变>
瞬态提供了一种WordPress本地的缓存机制。 他们将数据存储在有效期的时间内,使用对象缓存(如果可用)或落回数据库。 。
实现:
-
pros:对WordPress友好,自动清理过期的数据。
如果未配置对象缓存,则 >
if ($error) { set_transient("my_save_post_errors_{$post_id}_{$user_id}", $error, 45); } // ...in admin_notices hook... add_action( 'admin_notices', 'my_error_message' ); function my_error_message() { if ( $error = get_transient( "my_save_post_errors_{$post_id}_{$user_id}" ) ) { ?> <div class="error"> <p><?php echo $error->get_error_message(); ?></p> </div> <?php delete_transient("my_save_post_errors_{$post_id}_{$user_id}"); } }
- 方法3:将get参数附加到重定向url
- >此镜像WordPress自己的方法用于显示更新消息。 错误代码作为查询参数添加到重定向URL。 实现:
pros:
- 高性能,无数据库访问。
- > cons: >需要重复错误消息;维护开销可能会随大量的错误代码而增加。
if ($error) { add_filter('redirect_post_location', function( $location ) use ( $error ) { return add_query_arg( 'my-plugin-error', $error->get_error_code(), $location ); }); } // ...in admin_notices hook... add_action( 'admin_notices', 'my_error_message' ); function my_error_message() { if ( isset( $_GET['my-plugin-error'] ) ) { // Handle error based on error code (switch statement) ?> <div class="error"> <p><?php // Display error message based on $_GET['my-plugin-error'] ?></p> </div> <?php } }
- 结论
最佳方法取决于特定上下文。瞬态在WordPress集成和性能之间提供了良好的平衡,而GET参数方法则提供了最佳性能,但需要对错误消息进行更仔细的管理。在不同的WordPress环境中,该方法最简单,但可靠性较差。 选择最适合您需求和项目量表的方法。
$_SESSION
以上是在WordPress中显示从save_post钩中显示错误的详细内容。更多信息请关注PHP中文网其他相关文章!

WordPressCanHandLeLArgeWebsitesWithCareFulplanningAndOptimization.1)USECACHINGTOREDUCESERVERVERLOAD.2)优化YourdataBaseRegularly.3)actizeyourdatabaseregularly.3)ackdntododistibutecontent.4))

WordPress的定制化程度非常高,提供了广泛的灵活性和可定制性。1)通过主题和插件生态系统,2)利用RESTAPI进行前端开发,3)深入代码层面进行修改,用户可以实现高度个性化的体验。然而,定制化需要掌握PHP、JavaScript、CSS等技术,且需注意性能优化和插件选择,以避免潜在的问题。

WordPressisanexcellentchoiceforaCMSduetoitsuser-friendlyinterface,extensiveecosystem,SEOcapabilities,scalability,andsupportivecommunity.1)Itsintuitivedashboardmakescontentmanagementeasyforallusers.2)Thevastarrayofthemesandpluginsallowsforextensivecus

WordPressiser-FrightlyDuetLoitsIntuitiveInterfaceAndcms,whosparateContentFromDesign.itoffersArichTextedextEditorforeasyContentCreationAndialibraryFororRaryFororRory.itsflexeNhangedBancedBynhangedBynHangedBynumereNumerSandeMesandPlugins,elloverSandplugins,elloverOverUseCanimpActpercrance

1)ITSUPPORTSE-CommerceWithPluginSlikeWooCommerce,允许ProductManagementAndAndPaymentProcessing.2)ITSERVESASASASACMSACMSACMSMSACMSMSACMSMASTRATATEBLOGS,增强SeoAndEngagement.3)

WordPressIsnotIdeAlforHigh-TrafficWebsites,customandCompleXapplications,Security-SensitiveApplications,Real-TimedataProcessing,AndhighlyCustomizedUserInterfaces.forhigh-Trafficsites,USENENEXT.JSORXOLCT.JSORXORX.JSORCUSTOMSOMOLTICTS; forCompleXapplications; forcomplexapplications; optfordjangoorrub

Yes,youcanbuildablogwithWordPress.1)ChoosebetweenWordPress.comforbeginnersorWordPress.orgformorecontrol.2)Selectathemetopersonalizeyourblog'slook.3)Usepluginstoenhancefunctionality,likeSEOandsocialmediaintegration.4)Customizeyourthemewithsimplecodetw

WordPressCanbeseCureifManagedProperly.1)keepthewordPressCoreUpdatedTopatchVulnerabilities.2)vetandupdatepluginsandthemesfromreputables.3)EnforcestrongpasseTSandusetWordssandusetWordwordwo-factorauthenticaliation.4)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

Dreamweaver Mac版
视觉化网页开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。