【问题标题】:WordPress Plugin Event Call Before Shortcode短代码之前的 WordPress 插件事件调用
【发布时间】:2013-10-13 04:07:24
【问题描述】:

我正在编写一个插件,但在何时触发插件代码的特定功能时遇到了一些困难。

/*
// Plugin information goes here
*/


// ***** Area A

$GLOBALS['example_class'] = new example_class;

class example_class {

    // ***** Area B

    public function admin_init() {
        add_menu_page(

            // ...

        );
    } // End of admin_init function
} // End of example class

add_action('init', function() {
    global $example_class;

    // ***** Area C

    if ( ?????? ) {

        // Sanitize and set the view role
        $view = ( isset( $_REQUEST['view'] ) ) ? sanitize_key( $_REQUEST['ex'] ) : 'get_all';
        // Manage submitted data
        switch ( $view ) {

            // ...

        } // End of switch for view

        // Sanitize and set the action role
        $action = ( isset( $_REQUEST['action'] ) ) ? sanitize_key( $_REQUEST['action'] ) : NULL;
        // Manage submitted data
        switch ( $action ) {

            //...

        } // End of switch for action

    } // End of if page is being shown
});

add_action( 'admin_menu', function() {
    global $example_class;
    $example_class->admin_init();
});

add_shortcode( 'show_public_random', function () {
    global $example_class;
    // ...
});

根据 stackexchange 上的 in a previous post 建议,我将插件的控制器端分离为一个由 init 事件调用的函数。但是,我不希望在每次页面加载时评估 init 事件函数中包含的代码 - 我希望仅在加载包含短代码的页面时评估我的代码。

我尝试加载一个布尔类变量,该变量初始化为 false,但在 add_shortcode 函数中更改为 true,但到那时,为时已晚 - init 事件已触发,并且函数的内容未运行.

请帮助我 - 我应该在代码的区域 C 中使用哪个表达式?我应该测试什么以确保仅在使用短代码时才运行 init 事件函数?

【问题讨论】:

  • 你要检查什么?
  • init意味着要执行的第一件事,在此之前你的条件都不会起作用..你应该尝试用其他 apt hook 挂钩你的函数
  • 我需要根据操作和视图运行条件来决定在页面上显示什么,所以我选择的 apt 钩子需要在调用短代码之前。

标签: wordpress events plugins shortcode


【解决方案1】:

我找到了答案,虽然很乱。

/*
// Plugin information goes here
*/
$GLOBALS['example_class'] = new example_class;

class example_class {

    var $public_loaded = false,
        $content = '';

    public function admin_init() {
        add_menu_page(
            // ...
        );
    } // End of admin_init function

    public function get_random( ) {
        // ...
    }
} // End of example class

add_action('init', function() {
    global $example_class;

    // ***** Area A
    // Check for arbitrary variable sent with every user interaction
    if ( if ( isset( sanitize_key( $_REQUEST['tni'] ) ) ) {

        // ***** Area B
        /* Set the class variable `public_loaded` to true after it's
         * clear we're loading a public page which uses our plugin */
        $example_class->public_loaded = true;

        // Sanitize and set the action role
        $action = ( isset( $_REQUEST['action'] ) ) ? sanitize_key( $_REQUEST['action'] ) : NULL;
        // Manage submitted data
        switch ( $action ) {
            // ...
        } // End of switch for action

        // Sanitize and set the view role
        $view = ( isset( $_REQUEST['view'] ) ) ? sanitize_key( $_REQUEST['ex'] ) : 'get_all';
        // Manage submitted data
        switch ( $view ) {
            // ... Generate content and store in $this->content
        } // End of switch for view
    } // End of if page is being shown
});

add_action( 'admin_menu', function() {
    global $example_class;
    $example_class->admin_init();
});

add_shortcode( 'show_public_random', function () {
    global $example_class;
    // ***** Area C
    /* Check to see if page has loaded using the telltale sign
     * If not, load a default view - a random post */
    if ( $example_class->public_loaded === false ) {
        $example_class->content = $example_class->get_random();
        // ...
    }

    // Return the generated content
    return $example_class->content;
});

在区域 A 中,我设置了一个限定语句,以查看用户是否提交了变量以及他们与我的插件的交互。如果插件是我的,则评估代码,评估 actionview 模式。同样,该函数会将类变量public_loaded 设置为true。

在C区我设置了一个限定语句来查看类变量是否被设置为true;如果没有,则为短代码设置默认视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多