Site icon Hip-Hop Website Design and Development

wordpress plugin require_once file – can’t user wordpress functions

I read a lot about the require_once in stackoverflow.

But no anwser solved my problem.

I’ve a plugin with two files.

dmd_main.php //main file

dmd_second.php //some WP functions

In my main file I’ve included this line:

require_once (plugin_dir_path(__FILE__) . 'includes/dmd_second.php');

But on my second file I still got the errormessage:

Call to undefined function get_option()

I read that this is the right way to work with the wordpress functions or not?

I’ve tried a lot and this code will work if I include it in my second file:

include_once($_SERVER['DOCUMENT_ROOT'].'wp-config.php' );

But this solution is realy bad.

Can somebody explain me how I can solve this problem?

EDIT:

Code from the main file:

<?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();

Code from the second file:

<?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);