I’m new to PHP and I’m teaching myself through messing around with WordPress hooks and whatnot, so don’t make fun of my attempts please. I’m making a plugin that loads a JS and CSS file on each page from within the plugin directory.

That part works fine so far, but the next thing is to load some HTML divs onto each page from a file in the plugin folder as well. I’m having trouble figuring out how to do this successfully, lots of warnings and failures.

Here’s the plugin code so far:

<?php
   /*
        my plugin info
   */

function adsense_unblock_divs() {
    $asubHTML = file_get_contents(plugins_url('/html/php.html',__FILE__ ));
    include_once('$asubHTML');
}
add_action('wp_footer', 'adsense_unblock_divs');


   function adsense_unblock() {
wp_register_style('adunblock_CSS', plugins_url('/css/adunblock.css',__FILE__ ));
wp_enqueue_style('adunblock_CSS');
wp_register_script( 'adunblock_JS', plugins_url('/js/adunblock.js',__FILE__ ), array('jquery'));
wp_enqueue_script('adunblock_JS');
}
    add_action('wp_enqueue_scripts', "adsense_unblock");


    /* Settings Page */

add_action( 'admin_menu', 'adsense_unblock_menu' );

function adsense_unblock_menu() {
    add_options_page( 'AdSense Unblocker Settings', 'AdSense Unblocker', 'manage_options', 'ADSU-123', 'adsense_unblock_options' );
}

function adsense_unblock_options() {
    if ( !current_user_can( 'manage_options' ) )  {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }
    echo include_once('adsense_unblock_settings.php');
}

?>

Everything works fine except this first function:

function adsense_unblock_divs() {
    $asubHTML = file_get_contents(plugins_url('/html/php.html',__FILE__ ));
    include_once('$asubHTML');
}
add_action('wp_footer', 'adsense_unblock_divs');

That’s giving me a total failure to load page (500 or 300 error, I don’t recall exactly which). I’ve tried just doing just a simple include as well like this:

function adsense_unblock_divs() {
    include('/html/HTML.php');
}
add_action('wp_footer', 'adsense_unblock_divs');

But that gives me a warning error that the file doesn’t exist in that directory.

The contents of the PHP file is just this:

<?php
echo    '<section class="asub00" style="display:none;">
    <div class="asub00_msgWin" style="display:none;"></div>
    </section>'
?>

I need the HTML in a separate file for editing through the settings page. I’m sure I’m making a stupid mistake here but I’ve just not been able to figure it out. How does one include some divs in a PHP file to the body content of wordpress pages via a plugin?

Read more here: PHP/WordPress – Include HTML from outside file?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: