簡體   English   中英

導航到其他管理菜單項時,我的自定義Wordpress插件在管理區域中導致白屏

[英]My custom Wordpress plugin is causing a white screen in the admin area when navigating to a different admin menu item

好吧..我將盡我所能,盡可能詳細地解決我遇到的問題。 另外我很有可能在php / wordpress代碼中做錯了事,因為我已經使用很長時間了。

我創建了兩個wordpress插件。 一個是圖庫,一個是頁面構建插件。 這兩個插件都可以從前端和管理區域正常工作。 當您離開管理區域中的插件設置頁面時,會出現此問題,除非您單擊根插件設置鏈接或另一個的根設置鏈接,否則導航到的任何其他管理區域鏈接都將顯示為空白頁面。我制作的插件(雙向均可)。

另外,如果您從任一插件根菜單導航,都不會重現此問題。 僅當您位於插件的“添加/編輯”部分時。 插件的該區域正在訪問$ wpdb對象,並從我為每個插件創建的SQL表中提取數據。 同樣,兩個“添加新/編輯”頁面都使用了媒體上傳器,同時使用了wp_equeue_media()。

我試圖打開所有形式的日志,但沒有一個報告/顯示任何內容。 我嘗試注釋掉對$ wpdb和wp_equeue_media()的調用。 我嘗試過調整php內存限制以及wp-config.php中的限制。 這些都不起作用。

這是Gallery插件的代碼,因為它是兩者中較小的一個,並且它們都是以類似的方式構建的...

這是主插件頁面php

<?php
/**
 * Plugin Name: BoxGallery
 * Plugin URI: asd
 * Description: asd
 * Version: 1.0
 * Author: asd
 * Author URI: asd
 * License: GPL2
**/

// GLOBALS
global $boxgal_table_name;
global $boxgal_galleries;
global $toplevel_slug;

// INCLUDES
include('admin/boxgal-admin.php');

function boxgal_init()
{
    global $wpdb;
    global $boxgal_table_name;
    global $boxgal_galleries;

    global $toplevel_slug;
    $toplevel_slug = 'boxgallery';  

    $boxgal_table_name = $wpdb->prefix."boxgal";

    if($wpdb->get_var("SHOW TABLES LIKE '$boxgal_table_name'") == $boxgal_table_name)
        $boxgal_galleries = $wpdb->get_results("SELECT * FROM $boxgal_table_name");
}
add_action('init', 'boxgal_init'); 

function boxgal_enqueue()
{
    wp_enqueue_script('box-slider', plugins_url('/slider/box-slider.js', __FILE__ ));
    wp_enqueue_style('box-slider-style', plugins_url('/slider/box-slider-style.css', __FILE__ ));   
}
add_action('wp_enqueue_scripts', 'boxgal_enqueue');

function boxgal_install() 
{
    global $wpdb;
    global $boxgal_table_name;  
    $boxgal_table_name = $wpdb->prefix."boxgal";

    if($wpdb->get_var("SHOW TABLES LIKE '$boxgal_table_name'") != $boxgal_table_name) { 
        $sql = 'CREATE TABLE '.$boxgal_table_name.'(
                id INTEGER NOT NULL AUTO_INCREMENT,
                name VARCHAR(255),
                slug VARCHAR(255),
                size_x INTEGER,
                size_y INTEGER,
                images VARCHAR(255),
                trans_type VARCHAR(255),
                trans_time INTEGER,
                slide_hold INTEGER,             
                PRIMARY KEY  (id))';

        require_once(ABSPATH.'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }
}
register_activation_hook( __FILE__, 'boxgal_install');

function boxgal_shortcode($atts)
{
    if (isset($atts['tag'])) {      
        global $boxgal_galleries;
        $tag = $atts['tag'];

        $tag_gallery = null;
        $tag_gallery_imgs;      
        foreach ($boxgal_galleries as $gallery) {
            if ($tag == $gallery->slug) {
                $tag_gallery = $gallery;
                $tag_gallery_imgs = explode(",", $tag_gallery->images);
                break;  
            }               
        }

        if ($tag_gallery == null) {
            return '<span style="color:#F00;"><b>BoxGallery </b>- Could not find the '.$tag.' gallery. Please check spelling and that the gallery exists.</span>';  
        }
        else 
        {
            $output_html = '<script type="text/javascript">'.
                                'slideWidth = '.$tag_gallery->size_x.';'.
                                'slideHeight = '.$tag_gallery->size_y.';'.
                                'currentTransition = "'.$tag_gallery->trans_type.'";'.
                                'transitionTime = '.$tag_gallery->trans_time.';'.
                                'slideHold = '.$tag_gallery->slide_hold.';'.
                            '</script>';    
            $output_html .= '<div id="boxslider">';
            for($i = 0; $i < count($tag_gallery_imgs); $i++) 
            {
                $attachment = get_post($tag_gallery_imgs[$i]);
                $image_attributes = wp_get_attachment_image_src($tag_gallery_imgs[$i], 'full');              
                $output_html .= '<div class="boxslider_slide" style="background-image: url('.$image_attributes[0].')">'.
                                    '<div class="boxslider_caption">'.
                                        '<p><b>'.$attachment->post_title.'</b></p>'.
                                        '<p>'.$attachment->post_excerpt.'</p>'.
                                    '</div>'.                                   
                                '</div>';               
            }
            $output_html .= '</div>';
            return $output_html;
        }
    }
    else
        return '<span style="color:#F00;"><b>BoxGallery </b>- You forgot the tag attribute! i.e. [boxgal tag="gallery_tag"]</span>';
}
add_shortcode('boxgal', 'boxgal_shortcode');

?>

這是管理員包括PHP

<?php
function boxgal_menu()
{
    global $toplevel_slug;

    add_menu_page('BoxGallery options', 'BoxGallery', 'manage_options', $toplevel_slug, 'boxgal_options');
    add_submenu_page($toplevel_slug, 'Add new gallery', 'Add new...', 'manage_options', $toplevel_slug.'_add', 'boxgal_options_addnew');
}
add_action('admin_menu', 'boxgal_menu');

function boxgal_admin_enqueue(){
    wp_enqueue_script('media-upload'); 
    wp_enqueue_script('thickbox');
    wp_enqueue_style('thickbox'); 
    wp_enqueue_script('boxgal-admin', plugins_url('/boxgal-admin.js', __FILE__ ));
}
add_action('admin_enqueue_scripts','boxgal_admin_enqueue');

function boxgal_options()
{   
    global $wpdb;
    global $boxgal_table_name;
    global $boxgal_galleries;
    global $toplevel_slug;

    if (isset($_GET['delid']))
    {
        $wpdb->delete($boxgal_table_name, array('id' => $_GET['delid']));
        $boxgal_galleries = $wpdb->get_results("SELECT * FROM $boxgal_table_name");     
    }

    ?>
    <div class="wrap">
        <h2>BoxGallery</h2>
        <table class="widefat" style="margin: 10px 0px 50px 0px;">
            <thead>
                <tr>
                    <th>Gallery Name</th>
                    <th>Gallery Tag</th>
                    <th>Images</th>        
                    <th>Delete</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                <th>Gallery Name</th>                
                <th>Gallery Tag</th>
                <th>Images</th>
                <th>Delete</th>
                </tr>
            </tfoot>
            <tbody id="gallery_table">
            <?php               
                foreach ($boxgal_galleries as $gallery) {
                    $table_row = '';
                    $table_row .= '<tr>';
                    $table_row .= '<td><a href="'.admin_url('admin.php').'/?page='.$toplevel_slug.'_add&tag='.$gallery->slug.'">'.$gallery->name.'</a></td>';
                    $table_row .= '<td>'.$gallery->slug.'</td>';
                    $gal_imgs = explode(',', $gallery->images);
                    $table_row .= '<td>';
                    for($i = 0; $i < count($gal_imgs); $i++)
                        $table_row .= wp_get_attachment_image($gal_imgs[$i]);               
                    $table_row .= '</td>';
                    $table_row .= '<td><a href="'.admin_url('admin.php').'/?page='.$toplevel_slug.'&delid='.$gallery->id.'">Delete</a></td>';
                    $table_row .= '</tr>';
                    echo $table_row;
                }
            ?>              
            </tbody>
        </table>   
    </div>
    <?php   
}

function boxgal_options_addnew()
{   
    wp_enqueue_media(); 

    $edit_page = isset($_GET['tag']);

    if ($edit_page)
    {   
        global $wpdb;
        global $boxgal_table_name;
        global $boxgal_galleries;
        global $toplevel_slug;

        $tag = $_GET['tag'];

        $tag_gallery = null;
        $tag_gallery_imgs;      
        foreach ($boxgal_galleries as $gallery) 
        {
            if ($tag == $gallery->slug) 
            {
                $tag_gallery = $gallery;
                $tag_gallery_imgs = explode(",", $tag_gallery->images);             
                break;  
            }               
        }
    }           
    ?>  
    <script type="text/javascript">
        currentImgIds = Array(<?php echo $tag_gallery->images; ?>);
    </script>   
    <div class="wrap">
        <?php if (!$edit_page) { ?>
            <h2>Add new</h2>    
        <?php } else { ?>
            <h2>Edit "<?php echo $tag_gallery->name; ?>"</h2>
        <?php } ?>          
        <table class="form-table">
            <tr valign="top">
                <th scope="row"><label for="boxgal_name">Gallery Name:</label></th>
                <td><input name="boxgal_name" type="text" id="boxgal_name" value="<?php if($edit_page) echo $tag_gallery->name; ?>" class="regular-text" /></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="boxgal_slug">Gallery Tag:</label></th>
                <td><input name="boxgal_slug" type="text" id="boxgal_slug" value="<?php if($edit_page) echo $tag_gallery->slug; ?>" class="regular-text" />
                <p class="description">This is the tag that will be used in the BoxGallery shortcode. This should contain no white-space / spaces.</p></td>                
            </tr>
            <tr valign="top">
                <th scope="row"><label for="boxgal_size_x">Size:</label></th>
                <td><input name="boxgal_size_x" type="text" id="boxgal_size_x" value="<?php if($edit_page) echo $tag_gallery->size_x; ?>" class="regular-text" style="width: 50px;" /> x 
                <input name="boxgal_size_y" type="text" id="boxgal_size_y" value="<?php if($edit_page) echo $tag_gallery->size_y; ?>" class="regular-text" style="width: 50px;" /></td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="boxgal_transtype">Transition Type:</label></th>
                <td>
                    <?php 
                    if($edit_page)
                        $trans_type = $tag_gallery->trans_type;
                    ?>
                    <select id="boxgal_transtype">
                      <option <?php if ($edit_page && $trans_type == 'fade') echo 'selected'; ?> value="boxgal_tt_fade">Fade</option>
                      <option <?php if ($edit_page && $trans_type == 'slide-left') echo 'selected'; ?> value="boxgal_tt_slide-left">Slide Left</option>
                      <option <?php if ($edit_page && $trans_type == 'slide-right') echo 'selected'; ?> value="boxgal_tt_slide-right">Slide Right</option>
                    </select>
                </td>
            </tr>
            <tr valign="top">
                <th scope="row"><label for="boxgal_tt_time">Transition Time:</label></th>
                <td><input name="boxgal_tt_time" type="text" id="boxgal_tt_time" value="<?php if($edit_page) echo $tag_gallery->trans_time; ?>" class="regular-text" style="width: 50px;" />
                <p class="description">How long in milliseconds should the transition take?</p></td>
            </tr> 
            <tr valign="top">
                <th scope="row"><label for="boxgal_hold_time">Slide Hold:</label></th>
                <td><input name="boxgal_hold_time" type="text" id="boxgal_hold_time" value="<?php if($edit_page) echo $tag_gallery->slide_hold; ?>" class="regular-text" style="width: 50px;" />
                <p class="description">How long in milliseconds should the slide be on screen for?</p></td>
            </tr>           
        </table>
        <div style="margin: 50px 0px 10px 0px;">
            <input type="submit" class="button-secondary" id="add_image" value="Add Image" />
        </div>
        <table class="widefat" style="margin: 10px 0px 50px 0px;">
            <thead>
                <tr>
                    <th>Image</th>
                    <th>Caption</th>       
                    <th>Delete</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                <th>Image</th>
                <th>Caption</th>
                <th>Delete</th>
                </tr>
            </tfoot>
            <tbody id="gallery_table">
                <?php if ($edit_page) 
                {                   
                    for($i = 0; $i < count($tag_gallery_imgs); $i++)
                    {   
                        $attachment = get_post($tag_gallery_imgs[$i]);  
                        echo '<tr>';                                    
                        echo '<td>'.wp_get_attachment_image($tag_gallery_imgs[$i]).'</td>';
                        echo '<td>'.$attachment->post_excerpt.'</td>';
                        echo '<td><a id="'.$tag_gallery_imgs[$i].'" class="boxgal_del" href="#">Delete</a></td>';
                        echo '</tr>';
                    }
                }
                ?>                         
            </tbody>
        </table>   
        <input class="button-primary" type="submit" name="save_gallery" value="Save Gallery" id="save_gallery" />             
    </div>
    <?php
}


function boxgal_save_callback() 
{
    global $wpdb;
    global $boxgal_table_name;
    global $boxgal_galleries;

    $boxgal_name = $_POST['boxgal_name'];
    $boxgal_slug = $_POST['boxgal_slug'];
    $boxgal_size_x = $_POST['boxgal_size_x'];   
    $boxgal_size_y = $_POST['boxgal_size_y'];
    $boxgal_transtype = $_POST['boxgal_transtype'];
    $boxgal_tt_time = $_POST['boxgal_tt_time'];
    $boxgal_hold_time = $_POST['boxgal_hold_time'];

    $boxgal_img_ids = "";   
    $img_count = count($_POST['boxgal_img_ids']);
    for($i = 0; $i < $img_count; $i++) 
    {
        $boxgal_img_ids .= $_POST['boxgal_img_ids'][$i];
        if ($i < $img_count - 1)
            $boxgal_img_ids .= ",";
    }       

    $table_id = -1;
    foreach ($boxgal_galleries as $gallery) 
    {
        if ($boxgal_slug == $gallery->slug) 
        {
            $table_id = $gallery->id;
            break;  
        }               
    }

    if ($table_id != -1)
    {   
        $wpdb->update($boxgal_table_name, array('name' => $boxgal_name, 
                                                'slug' => $boxgal_slug, 
                                                'size_x' => $boxgal_size_x, 
                                                'size_y' => $boxgal_size_y,
                                                'trans_type' => $boxgal_transtype,
                                                'trans_time' => $boxgal_tt_time,
                                                'slide_hold' => $boxgal_hold_time, 
                                                'images' => $boxgal_img_ids), array('id' => $table_id));
    }
    else
    {
        $wpdb->insert($boxgal_table_name, array('name' => $boxgal_name, 
                                                'slug' => $boxgal_slug, 
                                                'size_x' => $boxgal_size_x, 
                                                'size_y' => $boxgal_size_y, 
                                                'images' => $boxgal_img_ids,
                                                'trans_type' => $boxgal_transtype,
                                                'trans_time' => $boxgal_tt_time,
                                                'slide_hold' => $boxgal_hold_time));
    }   

    echo "Saved";   
    die();
}
add_action('wp_ajax_boxgal_save', 'boxgal_save_callback');
?>

感謝任何人都可以提供的幫助!

我剛剛遇到此問題,並發現了以下可疑因素:我安裝了幾個插件,在本地運行時沒有問題,而在將生產服務器放入服務器時,出現了白屏死機。

Localhost設置:PHP 5.3.1

伺服器:PHP 5.2.17

因此,第一個可疑之處是PHP版本差異。

通過瀏覽Chrome中的腳本發現了第二個嫌疑犯。 首先,根據標題信息,我發現白屏死機實際上是HTTP 500 Internal Server錯誤。 然后我注釋掉以下內容后,

require_once(ABSPATH.'wp-admin/includes/upgrade.php');
dbDelta($sql);

該插件可以再次使用。 而奇怪的是,此問題僅在同時打開幾個插件的情況下發生。 只有具有上述語句的單個插件不會產生任何錯誤。

該插件的組合為:

  • 付費會員Pro 1.7.9.1
  • 活動記錄1.1
  • 主題我的登錄6.3.9

他們似乎都在爭奪會話或標頭中的某些內容,而我尚未找到確切的原因。 我認為這個方向可能會有所幫助。

關於錯誤日志,由於它會導致500內部錯誤,因此不會輸出到debug.log,我在插件的幾行中添加了一些“ echo”,以查看它在哪一點上產生了500錯誤。

如果很難攔截,則可以劫持wp-settings.php ,在“ ... as $plugin) ”行中添加一個echo $plugin . "<br>"; $plugin . "<br>"; 在foreach循環中插入一行,所有插件路徑都將消失,這將有助於調試。

到目前為止,這是我的發現。

注意:有關官方調試方法,請參閱Debugging_in_WordPress

編輯1:

我已經在本地主機上嘗試過PHP版本測試,並且運行的PHP版本比服務器更早,它不會產生錯誤。

而且我發現生產服務器中存在一個問題,生成406錯誤。 看來他們的(服務器)設置有問題。 然后我測試了一段時間(5小時),它似乎已解決。 因此,根據解決方案發現期間其他用戶的經驗,服務器設置,臨時目錄中的PHP緩存也是造成問題的原因。 我安裝了另一套新的Wordpress,以確保未修改原始系統文件。 5小時前,它也會生成帶有奇怪的406錯誤的錯誤。 然后問題消失了,先前的插件全部激活后,不再顯示白屏。

要通過運行以下命令來測試HTTP 406錯誤: http : //www.example.com/wp-content/如果您看到的不是空白頁面(例如“處理錯誤...”),則應該為空白頁面。 。 它很可能遇到服務器問題。

總結可能的檢查點:

  • 服務器設置,如果在運行http://www.example.com/wp-content/時出現HTTP 406錯誤
  • PHP緩存(會話臨時文件)或cookie,清理它們
  • PHP版本(開發與生產環境)
  • .htaccess中的Apache設置,請確保沒有任何自定義重定向影響
  • 如果一個或兩個特定的插件沖突。 檢查在init hook階段是否有任何沖突。 並檢查其名稱空間是否沖突。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM