【问题标题】:class_exists not working WordPressclass_exists 不工作 WordPress
【发布时间】:2014-07-19 08:47:38
【问题描述】:

我在 WordPress functions.php 中有以下函数:

if ( ! function_exists( 'mm_single_slider' ) ) :
function mm_single_slider($echo = true){


if ( class_exists( 'RW_Meta_Box' ) ) { 

    //Do Something

} else {

    //Do other things

}

}
add_action('plugins_loaded','mm_single_slider' );

但是 class_exists 不起作用,返回 false。这个类在一个被激活并且明显存在的插件中。

我的 functions.php 文件中也有这个:

require_once ('inc/mm-metabox.php');

此文件包含 class_exists 正在工作的类似函数,返回 true。

我尝试过使用 'plugins_loaded' 钩子,但没有它。

我做错了什么?似乎当需要该文件时,class_exists 正在工作,但不是直接在 functions.php 文件中?

非常感谢您的帮助。

【问题讨论】:

    标签: php wordpress class


    【解决方案1】:
       function mm_single_slider($echo = true){
           if ( function_exists( 'mm_single_slider' ) ) 
           return;
    
           if ( class_exists( 'RW_Meta_Box' ) ) { 
    
    //Do Something
    
           } else {
    
    //Do other things
    
           }
    

    在你的函数文件中,你不应该在你的函数之外有任何代码。你的 if 语法也有错误。您使用的方法的正确方法是

     if(cond): 
       do something;
     else:
       something else;
     endif;
    

    有一个endif;在 20/14 该功能结束时。

    您的问题取决于您放置上述代码的位置。如果它在插件中,mm_single_slider 可能在您调用插件代码运行时的代码时不存在。因此,您将您的函数存在到您的函数中并挂钩它以便稍后运行。

    如果您将它放在主题中,插件会在主题之前运行,因此您在主题中为加载的插件而挂接的任何内容都不会被调用,因为挂接已通过。加载顺序见http://codex.wordpress.org/Plugin_API/Action_Reference

    【讨论】:

    • 你好,抱歉,Wordpress 241 是这样构造函数的,你可以看看。
    • 我更新了进一步的解释。检查哪些适用于您的代码。请记住,除了打破函数之外,return 不会做任何事情,没有返回值,那么您如何检查是否为真/假?
    猜你喜欢
    • 1970-01-01
    • 2015-09-20
    • 2015-03-06
    • 2015-02-05
    • 2014-12-15
    • 2013-09-15
    • 2012-11-12
    • 2016-06-11
    • 2018-10-16
    相关资源
    最近更新 更多