WordPress.org

Support

Support » Plugins and Hacks » Hacks » WordPress use external variable on functions.php

WordPress use external variable on functions.php

  • I have a custom php page outside of wordpress, this page is connecting also to a external database, and get resultat form a sql query on a variable

    i use require(‘../wp-blog-header.php’) and require(‘../wp-load.php’) to integrate with wordpress.

    now i need to use this variable on the functions.php of my wordpress theme.

    i do on my external page :

    <head>
    <connect to my database>
    <my query>
    <get resulte on $variable>
    <require('../wp-blog-header.php')>
    <require('../wp-load.php')>

    but when i use $variable on my functions.php it do not work ?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    @bcworkz

    Your variable is out of scope for functions on functions.php. The easiest thing to do is declare it global on both the external page and in the function on functions.php using it.

    FYI, what you are doing is generally considered very sloppy coding for the WP environment. It’s OK if this is for your own site which you maintain, but virtually unusable if you may want others to use your code. Relative references to required WP core files is considered unacceptable. For external code to use WP resources the page either needs to be a page template or requests need to be sent through admin-ajax.php or admin-post.php.

    It’s also not a good idea to directly edit a theme’s functions.php file, your custom code should reside in a child theme or plugin and interface with WP through filters and actions. You may very well be doing this, but it’s worth mentioning in case you are not.

    You don’t have to do any of this, it’s your site. But if you think you might like to share your efforts with the WP community, or you simply take pride in well written code, it’s best to follow community established practices.

Viewing 1 replies (of 1 total)
  • The topic ‘WordPress use external variable on functions.php’ is closed to new replies.
Skip to toolbar