【发布时间】:2017-09-04 20:04:20
【问题描述】:
Advanced Custom Fields 文档始终建议使用 <?php if(get_field('repeater_field_name')): ?> 等验证开始任何内容,但当插件未激活时,此行会触发致命错误。如何避免?
【问题讨论】:
标签: wordpress advanced-custom-fields
Advanced Custom Fields 文档始终建议使用 <?php if(get_field('repeater_field_name')): ?> 等验证开始任何内容,但当插件未激活时,此行会触发致命错误。如何避免?
【问题讨论】:
标签: wordpress advanced-custom-fields
您可以尝试像这样使用 php 的 function_exists():
<?php
if ( function_exists('get_field') && get_field('repeater_field_name') ):
// do ACF things
endif;
?>
因为 && is a "short-cut" operator 它会从 L 到 R 进行检查,如果该功能不存在(即插件被禁用),它将退出并立即跳过整个 if 条件,并且永远不会检查是否get_field('repeater_field_name') 评估为真。所以你不能颠倒这两个条件的顺序,否则它仍然无法在插件禁用的情况下工作。
【讨论】:
// 3. Hide ACF field group menu item add_filter('acf/settings/show_admin', '__return_false'); 在开发过程中去掉这部分。然后将其添加到您的生产站点中,这样您的客户/站点管理员就不会弄乱您的 ACF 结构。
您可以使用https://codex.wordpress.org/Function_Reference/is_plugin_active 检查插件是否处于活动状态,但这仅适用于管理端,在前端您必须包含 plugin.php。我认为最干净的解决方案是简单地将这一行替换为您自己的包装函数,例如
function my_get_field($fld) {
if function_exists('get_field') return get_field($fld);
return false; //or whatever value suits your purpose
}
或者,如果这是临时情况,只需定义自己的虚拟 get_field() 并在不再需要时将其删除。
【讨论】: