0

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);
  • what is in dmd_second.php? – Rohit Kishore Jun 1 '16 at 12:50
  • in dmd_second.php is a wp function get_option(). The main file is the main file of my plugin. – cgee Jun 1 '16 at 12:59
  • have you defined get_option function in main file? – Rohit Kishore Jun 1 '16 at 13:01
  • get_option is a wp function. I don't need to define it. – cgee Jun 1 '16 at 13:02
  • okay. can you show code for both files? – Rohit Kishore Jun 1 '16 at 13:02
0

I'm not sure if you're trying to use WordPress functions outside of the actual WordPress application, but to accomplish this you simply need to do:

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

at the top of your file, if the path to the WordPress application is in fact $_SERVER['DOCUMENT_ROOT'] then you would do

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

  • read my question. I try it and it works. But this solution is bad. – cgee Jun 1 '16 at 12:58
  • it sounds like you're not within the WordPress application. what exactly are you trying to accomplish? – setterGetter Jun 1 '16 at 12:59
  • I program a plugin. dmd_main.php is the main file and the wp functions in it works fine.. but in the second file I can't use the wp functions. – cgee Jun 1 '16 at 13:00
  • What is the second file? What is the URL you are using to access the second file? – setterGetter Jun 1 '16 at 13:05
  • I edit my question and include both files. – cgee Jun 1 '16 at 13:12
0

The fact that the plugin is installed and activated and has it's PHP code executed doesn't mean that it's suddenly part of WordPress. You should let your plugin initialise on a WordPress hook, meaning that the code will not be executed untill WordPresshas reached a certain stage in the execution process.

You're actually already doing a lot of these hooks so good job!.

Try to replace the last line in dmd_main.php from $a = new dmd_pages(); to

add_action( 'init', 'wpse_228476_dmd_init' );
function wpse_228476_dmd_init() {
    $a = new dmd_pages();
}
  • I include it but I get still this errormessage: Fatal error: Call to undefined function get_option() in /www/htdocs/w01365c3/ak.dimadirekt.com/wp-content/plugins/dmd-pages/saveData.php on line 51 – cgee Jun 1 '16 at 13:23
  • That is right in the "big picture" but get_option specifically should be available in any place and any time in plugins code. – Mark Kaplun Jun 1 '16 at 14:05
  • The get_option() function is definitely available on init so there must be something else going on. You are also now using saveData.php which is not mentioned before. Can you update your post with the entire plugin code and correct filenames? – Jeffrey von Grumbkow Jun 1 '16 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.