【问题标题】:wp_ajax_ actions that use object-oriented action handlers?wp_ajax_ 使用面向对象的操作处理程序的操作?
【发布时间】:2012-10-09 01:15:12
【问题描述】:

我正在尝试将 ajax 操作传递给 /wp-admin/admin-ajax.php,但是我似乎无法让它调用任何使用类来封装命名空间的函数。我收到以下错误:

Warning:  call_user_func_array() expects parameter 1 to be a valid callback, class 'PHP_faq_backend' not found in /wp-includes/plugin.php

我的操作如下:

add_action('wp_ajax_edit_form', array('PHP_faq_backend', 'edit_form'));

显然我不想通过修改 admin-ajax.php 文件来强制执行此操作,但是如何加载我的类文件以便操作生效?

【问题讨论】:

  • 将它们包含在您的插件中。

标签: php ajax wordpress namespaces include


【解决方案1】:

确保您将类文件包含在您的 Wordpress 主题 (functions.php) 或插件中的某个位置。

我通常使用这样的东西;

<?php
class Some_Class {

    public function __construct() {
        //logged in users
        add_action('wp_ajax_some_method', array($this,'some_method'));
        //non logged in users
        add_action('wp_ajax_nopriv_some_method', array($this,'some_method'));
    }

    public function some_method(){
        //check nonce values etc
        if ( ! isset( $_REQUEST['some_nonce'] ) || ! wp_verify_nonce( $_REQUEST['some_nonce'], 'class_some_nonce' ) ) {
            wp_die(__('Naughty', 'some-class-textdomain'));
        }

        //proceed with post data validation - then execute method
    }

}

$some_class_instance = new Some_Class();
?>

以上内容用于从类实例中注册操作。

【讨论】:

  • 我正在使用具有多个类的 OO 方法,并且在我期望的时候没有包含这个特定的类文件。进行了一些调试,但解决了。感谢您的建议 - 这是 wp_ajax_ 调用在 OO 插件中的外观的可靠示例!
【解决方案2】:

错误消息的最后一部分是您的第一条线索。我在多个版本的 WP 上遇到过同样的问题。在某些环境中,安装无法识别第三方类。您可能可以做几件事来尝试解决这个问题:

  1. 检查您的类文件的权限,看看您是否有足够的权限
  2. 检查您的类是否正在加载,例如明确地将它们包含在您的 functions.php 文件中(不是理想或推荐的方法)

我发现一直有效的一个解决方案是:

  1. 创建需要通过插件管理器激活的骨架插件
  2. 您的插件文件应该包含您想要使用的所有类。
  3. 激活您的插件。完毕。

这种方法可以通过几种不同的方式来实现;第一个是您可以定义一个驻留在当前主题目录中的插件目录——这可以让您将所有文件放在一起;二是走正常路线,在wp-content/plugins/my-skeleton-plugin下创建一个插件文件夹。如果您采用这种方法,这里有一个框架插件可以帮助您入门:

<?php
/*
Plugin Name: Skeleton Plugin Base
Plugin URI: http://www.yourdomain.com
Description: This is a base plugin to use as a template for other plugins.
Author: Author Name
Version: v0.0.1
Author URI: http://www.yourdomain.com
*/

    # +------------------------------------------------------------------------+
    # PLUGIN URL AND PATH CONSTANTS
    # +------------------------------------------------------------------------+
    /**
     * Don't depend on WP to always provide a consistent directory/path to use.
     * Extrapolate one the location of this file. The reason for this is that some
     * themes might re-locate the plugins and themes directory to another folder
     * and therefore break the normal WP structure.
     */
    define( DS, DIRECTORY_SEPARATOR, true );

    $PLUGIN_URL = WP_PLUGIN_URL . DS . str_replace( basename( __FILE__ ),'', plugin_basename(__FILE__) ); 
    $PLUGIN_DIR = dirname(__FILE__);

    define( MY_PLUGIN_URL, $PLUGIN_URL, true );
    define( MY_PLUGIN_DIR, $PLUGIN_DIR, true );


    # +------------------------------------------------------------------------+
    # INCLUDES
    # +------------------------------------------------------------------------+
    /**
     * Include the plugin.
     */
    // Use include() if you prefe
    include_once( MY_PLUGIN_DIR . '/inc/classes/my_skeleton_plugin.class.php' );

    try
    {

        /**
         * Initialize the plugin
         */

    }
    catch( Exception $e )
    {
        throw $e;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    相关资源
    最近更新 更多