• Resolved djhowden

    (@djhowden)


    I really cant understand why this has happened. I’m developing a plugin and have an admin display page in /plugins/myfolder/admin/partials/admin-general.php and the only piece of code I have on it is bloginfo()

    the page loads but will not find the global scope of wordpress and it is not available. No matter what function I call it var_dumps as null. The error is not caught by Query Monitor and it seems as if I am outside the scope of WP yet it is managing to find the page.

    Is there a way to bring the page into the scope of wordpress? there is no code as such as all I’m using is <?php bloginfo(‘name’) ?>

    I am confused because WordPress will ouput the HTML and create the page within admin but I have no scope within WP and can not access the $post loop etc

    any idea’s anyone please ๐Ÿ™‚

Viewing 10 replies - 1 through 10 (of 10 total)
  • I’m trying to find a way to undo things on my plugins in case I get a chance to replicate what you are experiencing and help but without luck. If the plugin is loaded correctly then it should have access to the core functions either way without extra coding.

    What I would only suggest at this point is to either copy paste the code bellow and see if that works. If it does then you have to follow your plugin line by line in case you are missing an include or a function name depending on what/how you are building it. If it doesn’t work then it’s a whole different story.

    It is basically an 1 file test plugin just made it up for convenience of back-testing. Create a folder test/test.php on your plugins or whatever fits your needs to see if this bloginfo works for you, it will simply create a new menu on your admin panel to call bloginfo inside.

    
    <?php
    /*
    Plugin Name: test
    Plugin URI: https://bloginfotest.test/
    Description: test
    Version: 0.0
    Author: bloginfotest
    Author URI: https://bloginfotest.test
    Text Domain: bloginfotest
    */
    
    function bloginfotest_admin_menu() {
    	add_menu_page(
    		'Bloginfo Test',
    		'Bloginfo Test',
    		'manage_options',
    		'bloginfo-test',
    		'bloginfo_test_settings_page',
    		'dashicons-format-status',
    		'80'
    	);
    }// end bloginfotest_admin_menu
    add_action( 'admin_menu', 'bloginfotest_admin_menu' );
    
    function bloginfo_test_settings_page() {
    	echo '<h1>';
    	bloginfo('name');
    	echo '</h1>';
    }// end bloginfo_test_settings_page
    
    Thread Starter djhowden

    (@djhowden)

    I tried to edit it and lost the code lol here is my original message

    Thanks for that because it works so it must be my code.

    I’ve created the code in a class and am calling it from a class loader (I’m using Bootstrap Plugin)

    public function pwg_admin_page(){
    add_menu_page(
    __(‘PWG Admin’, ‘pwg’), //page title
    ‘PWG Options’, // menu title
    ‘manage_options’, //capabilities
    ‘pwg_admin’, //menu slug
    [$this , ‘pwg_show_admin_page’], //function
    ‘dashicons-filter’,
    ‘5.0’ //menu position
    );
    }

    which will call this

    public function pwg_show_admin_page(){
    include plugin_dir_url( __FILE__ ) . ‘partials/pwg-admin-display.php’;
    }

    and is added as an action from the loader class

    $this->loader->add_action( ‘admin_menu’, $plugin_admin, ‘pwg_admin_page’);

    and error is

    Fatal error: Call to undefined function bloginfo() in C:\xampp\htdocs\wordpress\wp-content\plugins\pwg\admin\partials\pwg-admin-display.php on line 20

    and the HTML on line 20 is

    <h1>General Settings</h1>
    <?php bloginfo(‘name’); ?>

    As I say the page will load without error with HTML and PHP but I have not got the WP class or I am out of it’s scope (not sure how you say it) so WordPress is processing it and Query Monitor says it was called within the admin_menu hook and reports no errors.

    It is baffling and is there something I have missed because I’m using classes?

    I’ve just re-done my test code into a really simple class ( just in case ) and I am still able to easily call a function inside it that returns the bloginfo(‘name’) and everything else i queued up without problems or errors.

    Unfortunately I’m not aware of the Bootstrap Plugin you are referring to and what it does / is set up I believe I won’t be much of an assistance into that.

    Since everything else is loaded though as you say everything should’ve been working either way. I’ve never personally heard of needing anything extra to access WP functions.

    Thread Starter djhowden

    (@djhowden)

    me too which is why I’m baffled ๐Ÿ™‚ I’ll take it out of the class and load it procedural and see if it works.
    The bootstrap is a simple set of classes to organise code for plugins in a professional way. It took me a couple of days to work it out but it’s fairly simple and logical and other than this page and sub menu pages (its the same for them too) it’s been a great way to build a plugin in an OOP way ๐Ÿ™‚ thanks for your thoughts and help, maybe someone else has come across this and they can help. It’s not common so not WordPress

    hmmmmm it’s a head scratcher

    include plugin_dir_url( __FILE__ ) . โ€˜partials/pwg-admin-display.phpโ€™;

    plugin_dir_url() returns a url, not a path.

    Whatever $this->loader is, try your code without it using standard actions.

    Thread Starter djhowden

    (@djhowden)

    Hi ๐Ÿ™‚ yes I want the url for the file to include into the admin page, which works and the page does indeed load and I can write HTML and PHP and it still works, however when I want to use a core function of wordpress the functions are not defined which means my page has lost scope (i think thats what you say)

    I got on with some other stuff but am planning to run it outside the class I’m using (Loader) but like I say the code works and the page appears and will print HTML and work with PHP but not wordpress lol not really laughing

    Thread Starter djhowden

    (@djhowden)

    OK I managed to get both working (class and procedural) and both same result WP not declared and yet the page appears. Maybe last resort… reboot lol not laughing but sniggering. I have reinstalled etc and running out of options other than do I need the option page? lol get away

    Thread Starter djhowden

    (@djhowden)

    path or url? sorry I missed that. Im not sure which one? I thought it was ok but if the url is a http request it can be outside wordpress and so… outside scope hmmm will test tomorrow and let you know

    @pross is correct I didn’t even notice my eyes where seeing plugin_dir so my brain was saying it’s ok and never read it to the end I guess :S ..
    this include plugin_dir_url( __FILE__ ) . โ€˜partials/pwg-admin-display.phpโ€™; should be include( plugin_dir_path( __FILE__ ) . 'partials/pwg-admin-display.php' ); instead. Same goes for all includes / requires etc you want to do. More reference at https://developer.ww.wp.xz.cn/reference/functions/plugin_dir_path/

    Thread Starter djhowden

    (@djhowden)

    you beautiful people ๐Ÿ™‚ thank you thank you lol I would have stared at the screen all day if it were not for that ๐Ÿ™‚ makes sense too when I think about it. Thanks

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘call to undefined function bloginfo()’ is closed to new replies.