帮助扩展 WordPress 插件中使用的类

发布于 2024-12-02 08:34:24 字数 1360 浏览 1 评论 0原文

首先,附带条件 - 我是一名设计师而不是开发人员,所以请保持温柔;)

我正在尝试通过扩展一个类来调整 WordPress 插件。在类中,在一些条件 if/elseif 中,代码调用两个函数之一。我想调用两个新函数而不是这些函数(实际上与旧函数相同,但再次稍作调整)。

我之前已经通过这样做成功地完成了此操作:

class MY_CLASS extends MY_NEW_CLASS { ...

然后我更改了类中的代码,然后我们就可以走了。但是,在这种情况下它似乎不起作用?

下面我粘贴了放入 Functions.php 文件中的类代码:

class QA_AJAX_new extends QA_AJAX {

function init() {
    add_action( 'wp_ajax_qa_vote', array( __CLASS__, 'vote' ) );
    add_action( 'wp_ajax_qa_accept', array( __CLASS__, 'accept' ) );
}

function vote() {
    global $_qa_votes;

    $_qa_votes->handle_voting();

    $id = $_POST['post_id'];
    $post_type = get_post_type( $id );

    if ( 'question' == $post_type )
        the_question_voting_new( $id );
    elseif ( 'answer' == $post_type )
        the_answer_voting_new( $id );
    else
        die( -1 );

    die;
}

function accept() {
    global $_qa_votes;

    $_qa_votes->handle_accepting();

    $id = $_POST['answer_id'];

    the_answer_accepted( $id );

    die;
}
}

QA_AJAX_new::init();

大约在中间,您可以看到我希望该类使用的两个新函数,the_question_voting_newthe_answer_voting_new。我还更改了最后的代码,现在显示 QA_AJAX_new::init(); - 我不确定我是否应该这样做,但我已经尝试了两种方法两者都没有什么区别。

我显然做错了一些事情(或者试图做一些不可能的事情),开发人员会立即发现一些事情,但我可怜的设计师大脑对 php 的了解不够,无法弄清楚。

谢谢。

First, a proviso - I'm a designer not a dev, so please be gentle ;)

I'm trying to tweak a Wordpress plugin by extending a class. In the class in a bit of conditional if/elseif-ing the code calls one of two functions. Instead of these functions I've want to call two new ones (effectively the same functions as the old ones but again, slightly tweaked).

I've done this successfully before by doing this:

class MY_CLASS extends MY_NEW_CLASS { ...

Then I change the code in the class and away we go. However, in this case it doesn't appear to be working?

Below I've pasted the class code that I've put into my functions.php file:

class QA_AJAX_new extends QA_AJAX {

function init() {
    add_action( 'wp_ajax_qa_vote', array( __CLASS__, 'vote' ) );
    add_action( 'wp_ajax_qa_accept', array( __CLASS__, 'accept' ) );
}

function vote() {
    global $_qa_votes;

    $_qa_votes->handle_voting();

    $id = $_POST['post_id'];
    $post_type = get_post_type( $id );

    if ( 'question' == $post_type )
        the_question_voting_new( $id );
    elseif ( 'answer' == $post_type )
        the_answer_voting_new( $id );
    else
        die( -1 );

    die;
}

function accept() {
    global $_qa_votes;

    $_qa_votes->handle_accepting();

    $id = $_POST['answer_id'];

    the_answer_accepted( $id );

    die;
}
}

QA_AJAX_new::init();

About half way down you can see the two new functions I want the class to use, the_question_voting_new and the_answer_voting_new. I've also changed the code at the end that now says QA_AJAX_new::init(); - I'm not sure if I'm supposed to do that, but I've tried it both ways and neither makes a difference.

I'm clearly doing something wrong (or trying to do something that's not possible), something a dev would spot instantly, but my poor designer brain doesn't know enough about php to figure it out.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱殇璃 2024-12-09 08:34:24

我的第一个想法是 Init 不是静态的,因此您不能使用 QA_AJAX_new::init(); 调用它。在表单发布的脚本中尝试:

$qa_ajax = new QA_AJAX_new();
$qa_ajax->init();

My first thought is that Init isn't static so you can't call it using QA_AJAX_new::init();. In the script that the form posts to try :

$qa_ajax = new QA_AJAX_new();
$qa_ajax->init();
○愚か者の日 2024-12-09 08:34:24

您可以查看对象继承< /a>,这似乎是问题所在。
您可以尝试以下一些其他操作...

  • wp-config.php 中,将 WP_DEBUG 设置为 true:define('WP_DEBUG', true);
  • 确保 PHP display_errors 指令设置为 1,您应该能够添加 ini_set('display_errors', 1); 到您的wp_config.php 文件。

这将有助于查看可能被触发和抑制的任何错误。

我和其他 WP 开发人员发现的最常见的事情之一是,很多插件/主题作者在编写代码时不会将 WP_DEBUG 设置为 true,很多时候你会发现错误在消息中。

You might look into Object Inheritance, which seems to be the problem.
Here's a couple of other things you might try...

  • In wp-config.php, set WP_DEBUG to true: define('WP_DEBUG', true);
  • Make sure the PHP display_errors directive is set to 1, which you should be able to add ini_set('display_errors', 1); to your wp_config.php file.

This will help see any errors that may be being triggered and suppressed.

One of the most common things I've found as well as other WP developers is that so many plugin/theme authors will not set WP_DEBUG to true while writing their code and many times you'll find your error in the messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文