2013-10-13 34 views
0

我正在編寫一個插件,並且在觸發插件代碼的特定功能時遇到一些困難。短代碼之前的WordPress插件事件調用

/* 
// 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; 
    // ... 
}); 

按照建議in a previous post上stackexchange,我分離我的插件的控制器側成由init事件調用的函數。但是,我不希望包含在init事件函數中的代碼在每次加載頁面時進行評估 - 我希望僅在加載包含簡碼的頁面時才評估我的代碼。

我已經嘗試加載一個布爾類變量,它初始化爲false,但在add_shortcode函數內更改爲true,但到那時爲時已晚 - 已觸發init事件,並且該函數的內容未運行。

請幫我 - 我應該在我的代碼的C區使用哪種表達方式?我應該測試什麼以確保僅在使用短代碼時才運行init事件函數?

+0

你想檢查什麼? – codepixlabs

+0

init'means第一件事就是要執行,在你之前沒有任何條件可以工作..你應該嘗試用你的函數鉤住其他的鉤子 – codepixlabs

+0

我需要運行基於動作和視圖的條件來決定顯示什麼在頁面上,所以我選擇的apt鉤子需要在調用shortcode之前。 –

回答

0

我找到了答案,雖然凌亂。

/* 
// 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;如果不是,則爲短代碼設置默認視圖。