WordPress插件Required_Once文件-无法使用WordPress函数

时间:2016-06-01 作者:cgee

我读了很多关于require_once 在stackoverflow中。

但没有人能解决我的问题。

我有一个带有两个文件的插件。

dmd_main.php //main file

dmd_second.php //some WP functions
在我的主文件中,我包含了这一行:

require_once (plugin_dir_path(__FILE__) . \'includes/dmd_second.php\');
但在我的第二个文件中,我仍然收到了错误信息:

调用未定义的函数get\\u option()

我听说这是使用wordpress函数的正确方法,还是不正确?

我已经尝试了很多,如果我将其包含在第二个文件中,此代码将起作用:

include_once($_SERVER[\'DOCUMENT_ROOT\'].\'wp-config.php\' );
但这个解决方案真的很糟糕。

有人能解释一下我如何解决这个问题吗?

编辑:

主文件中的代码:

<?php
/**
 * Plugin Name: dmd Pages
 *  
 * @package dmd_Pages
 *
 * @wordpress-plugin
 * Plugin URI: https://dimadirekt.de/
 * Author: digitalmarketingditrekt gmbh
 * Author URI: https://dimadirekt.de/
 * Description: This plugin loads a list of all published pages into a menu in the adminbar. You can edit pages faster and don\'t need to search in the dashboard=>pages.
 * Version: 1.
 **/

class dmd_pages{
    public function __construct(){ 
        require_once (plugin_dir_path(__FILE__) . \'includes/saveData.php\');
        $this->set_actions();
    }

    public function set_actions(){
        add_action( \'admin_enqueue_scripts\', array($this, \'dmd_custom_style_load\'), 99 ); 
        add_action( \'wp_enqueue_scripts\', array($this, \'dmd_enqueue_child_theme_styles\'), 99);
        add_action( \'admin_menu\', array($this, \'dmd_register_adminmenu\'));
        add_action( \'wp_before_admin_bar_render\', array($this, \'dmdPages\'));
    }  

    /*
     * Load style for the adminbar menu
     */
    public function dmd_custom_style_load() {
        wp_register_style( \'dmd-pages-css-admin\', plugins_url(\'./css/style.css\',  __FILE__));
        wp_enqueue_style( \'dmd-pages-css-admin\' );
        wp_enqueue_script( \'searchbox\', plugins_url(\'./js/jquery.hideseek.min.js\', __FILE__), true);
        wp_enqueue_script( \'liveAjax\', plugins_url(\'./js/liveAjax.js\', __FILE__), true);
    }

    public function dmd_enqueue_child_theme_styles() {
        wp_register_style( \'dmd-pages-css-fe\', plugins_url( \'./css/style.css\', __FILE__ ) );
        wp_enqueue_style( \'dmd-pages-css-fe\' );                         
        wp_enqueue_script( \'searchbox\', plugins_url(\'./js/jquery.hideseek.min.js\', __FILE__), true);
        wp_enqueue_script( \'liveAjax\', plugins_url(\'./js/liveAjax.js\', __FILE__), true);
    } 

    /*
     * Neues Menü anlegen
     */
    public function dmd_register_adminmenu() {
        add_menu_page( \'DMD Pages\', \'DMD Pages\', \'manage_options\', \'/dmd-pages/admin-tpl.php\', \'\');
    }  

    public function dmd_get_status_option(){
        $status = explode(",", get_option(\'dmd-pages-settings\'));

        return $status;
    }

     public function dmdPages() {

        if ( !is_super_admin() || !is_admin_bar_showing() )
                return; 

         global $wpdb;
         global $wp_admin_bar;

        /* 
         * get all posts 
         */
                $option = $this->dmd_get_status_option();

                $querystring = \'SELECT ID, post_title, post_type FROM \'.$wpdb->prefix.\'posts WHERE 1=1 AND 1=2 \';

                if($option[0] == 1){
                    $querystring .= \'UNION SELECT ID, post_title, post_type FROM \'.$wpdb->prefix.\'posts WHERE post_status="publish" AND post_type="post"\';
                }

                if($option[1] == 1){
                    $querystring .= \'UNION SELECT ID, post_title, post_type FROM \'.$wpdb->prefix.\'posts WHERE post_status="publish" AND post_type="page"\';
                }

                if($option[3] == 1){
                    $querystring .= \'UNION SELECT id, name as post_title, default_template as post_type FROM \'.$wpdb->prefix.\'frm_forms WHERE status = "published" ORDER BY post_title ASC\';
                }

                $results = $wpdb->get_results($querystring);        

        /*
         * Create new menu in adminbar 
         */
            $wp_admin_bar->add_node(array(
                    \'id\'    => \'FastMenu\',
                    \'title\' => \'FastMenu\'
            ));

                if($option[2] == 1){
                    $wp_admin_bar->add_node( array(
                        \'id\'    => \'live-search\',
                        \'title\' => \'live search\',
                        \'parent\'=>\'FastMenu\',
                        \'meta\'=> array( \'html\' => \'<input type="text" name="search" class="search tt-query" data-list=".searchclass">\',\'target\' => \'_blank\', \'class\' => \'dmd_livesearch\' ) 
                    ));             
                }
        /*
         * Create submenu in the adminbar
         */

        if(isset($results))
         {  
            foreach ($results as $post){
                $site = admin_url();
                $url = $site.\'post.php?post=\'.$post->ID.\'&action=edit\';

                        switch($post->post_type){
                            case \'post\':
                                $this->dmd_create_submenu($post_title = $post->post_title, $post_type = $post->post_type, $url);
                                break;
                            case \'page\':
                                $this->dmd_create_submenu($post_title = $post->post_title, $post_type = $post->post_type, $url);
                                break;
                        }

                        if($post->post_type != \'page\' && $post->post_type != \'post\'){
                            $url = $site.\'admin.php?page=formidable&frm_action=edit&id=\'.$post->ID;

                            $this->dmd_create_submenu($post_title = $post->post_title, $post_type = \'formidable\', $url);
                        }               
            }
          } 
    }

    /*
     * Funktion zum Erstellen des Submenüs
     */
    public function dmd_create_submenu($post_title, $post_type, $url){
        global $wp_admin_bar;

        $post_type = \'dmd_\'.$post_type.\' searchclass\';

        $wp_admin_bar->add_menu( array(
                \'id\'    => $post_title,
                \'title\' => $post_title,
                \'href\'  => $url,
                \'parent\'=>\'FastMenu\',
                \'meta\'=> array(\'target\' => \'_blank\', \'class\' => $post_type)
            )
        );
    }     
}

$a = new dmd_pages();
第二个文件中的代码:

<?php 
/*
 * Include wp-config um WP Funktionen verwenden zu können
 */
//include_once($_SERVER[\'DOCUMENT_ROOT\'].\'wp-config.php\' );

/*
 * POSTS in Variablen abspeichern.
 * Es werden die Checkboxen übergeben mittels 1 oder 0.
 */
$dmd_posts = $_POST[\'post\'];
$dmd_pages = $_POST[\'page\'];
$dmd_searchbox = $_POST[\'searchbox\'];
$dmd_formidable = $_POST[\'formidable\'];

$dmd_setting_values = $dmd_posts.\',\'.$dmd_pages.\',\'.$dmd_searchbox.\',\'.$dmd_formidable;

/*
 * Prüfen ob der Key vorhanden ist mittels cURL.
 * Die Keydaten liegen auf einem separaten Server.
 */
function dmd_check_key($arg){
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => \'https://xxx.de/dmd-pages-pro/dmd_key_generator.php?key=\'.$arg.\'&website=\'.$_SERVER[\'HTTP_HOST\'],
        CURLOPT_USERAGENT => \'Codular Sample cURL Request\',
        CURLOPT_VERBOSE => 1,
        CURLOPT_SSL_VERIFYPEER => false
    ));
    curl_setopt($curl, CURLOPT_STDERR, fopen("curl_debug.txt", "w+"));
    $resp = curl_exec($curl);
    curl_close($curl);

    return $resp;
}

if(isset($_POST[\'key\'])){
    $x = dmd_check_key($_POST[\'key\']);

    if($x == true){
        if(!get_option(\'dmd-pages-key-status\')){add_option(\'dmd-pages-key-status\', \'true\');}else{update_option(\'dmd-pages-key-status\', \'true\');}    
        if(!get_option(\'dmd-pages-key\')){add_option(\'dmd-pages-key\', $_POST[\'key\']);}else{update_option(\'dmd-pages-key\', $_POST[\'key\']);}            
    }
}

/*
 * Die Einstellungen im WP DMD Pages Admincenter werden abgespeichert
 */
function saveMethod($dmd_setting_values){
    if(!get_option(\'dmd-pages-settings\')){
        add_option(\'dmd-pages-settings\', $dmd_setting_values);
    }else{
        update_option(\'dmd-pages-settings\', $dmd_setting_values);
    }    
}
saveMethod($dmd_setting_values);

2 个回复
SO网友:setterGetter

我不确定您是否试图在实际WordPress应用程序之外使用WordPress功能,但要实现这一点,您只需执行以下操作:

require( \'path-to-wordpress/wp-load.php\' );

如果WordPress应用程序的路径实际上是$_SERVER[\'DOCUMENT_ROOT\'] 那你会的

require ( $_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-load.php\' );

SO网友:Jeffrey von Grumbkow

插件已经安装并激活,并且执行了PHP代码,这并不意味着它突然成为WordPress的一部分。您应该让您的插件在WordPress挂钩上初始化,这意味着在WordPress达到执行过程中的某个阶段之前,代码不会被执行。

实际上你已经做了很多这样的钩子了,干得好!。

尝试替换dmd\\U main中的最后一行。php发件人$a = new dmd_pages();

add_action( \'init\', \'wpse_228476_dmd_init\' );
function wpse_228476_dmd_init() {
    $a = new dmd_pages();
}

相关推荐