I have a problem with my custom theme for wordpress. I made a custom theme locally on my PC where I installed Wordpress. Now inside my theme is another folder let's say php, and inside this folder is a php file that will fetch data from the database which I want to run later on. The code in this php file is as follows:
<?php
define('WP_USE_THEMES', false);
require('/wp-blog-header.php');
header('HTTP/1.1 200 OK');
global $wpdb;
$rs = $wpdb->get_results("select * from wp_users");
$user_db=$rs[0]->user_nicename;
echo $user_db;
?>
The codes above works on my local machine, where it prints out the user_nicename field from my local database (phpMyAdmin). Now when I upload and install the custom theme I made and access this php file the browser displays nothing. Is something missing in define or require so I can successfully connect to the Wordpress database? Any help help would be much appreciated.
Please note that theme is located at C:\wamp\www\wordpress\wp-content\themes\custom_theme and the php file to connect to database is here: C:\wamp\www\wordpress\wp-content\themes\custom_theme\php\connect.php
Instead of using the above code, why not just the function get_userdata on your template where you want to display the user nicename.
<?php
$user = get_userdata( $userid );
echo $user->user_nicename;
?>
See http://codex.wordpress.org/Function_Reference/get_userdata for details and look at the "Related" section at the bottom for other related uses.
Related
I'm new to moodle plugin development and am trying to create a plugin that displays a page to the admin where I can add my on php code.
In brief, what I want the plugin to do I have already achieved in a standard php file that I upload to the moodle root. From here you can call the file e.g. yourdomain.co.uk/moodlelocation/myfile.php and it will run as expected.
The problem with this is it isn't secure since anyone can load the myfile.php and in turn run the scripts on the page. It also means any one else using this script (it will be given away for free when complete) would need to FTP into their hosting and upload two php files to their moodle install.
Due to this, I thought a plugin (a very very basic plugin) may be the best solution. They could then load the page in the admin via the "site administration". e.g. site administration > Development > MyPlugin. I am assuming I could then also restrict the plugin's main page to admins only (??).
So to recap, I can create a php page that has my script all rocking and rolling BUT I need to make this into a plugin.
I did some reading and I think a "local" plugin was the easiest way to go (??).
I have managed to get the local plugin up and running using the below in local/webguides/inex.php :
<?php
// Standard config file and local library.
require_once(__DIR__ . '/../../config.php');
// Setting up the page.
$PAGE->set_context(context_system::instance());
$PAGE->set_pagelayout('standard');
$PAGE->set_title("webguides");
$PAGE->set_heading("webguides");
$PAGE->set_url(new moodle_url('/local/webguides/index.php'));
// Ouput the page header.
echo $OUTPUT->header();
echo 'MY php CODE here etc';
?>
This works fine but with two problems:
Anyone can access it via http://domain/local/webguides/index.php
There is no link to it in the site administration (so the user would need to type the URL in).
Can anyone shed any light how I would achieve the two steps above?
Thanks in advance
p.s. ideally I'd like to keep the plugin to as few files as possible so if the required code could be added to the local/webguides/index.php file it would be preferred.
You need to create a capability, then require that capability before displaying the page.
First, have a look at local/readme.txt - this gives an overview of the files needed for a local plugin.
Or read the documentation at https://docs.moodle.org/dev/Local_plugins
Also have a look at existing local plugins so you can see how they are created - https://moodle.org/plugins/?q=type:local
At a bare minimum, you need
local/webguides/db/access.php - this will have the capability
local/webguides/lang/en/local_webguides.php
local/webguides/version.php
Plus your index file
local/webguides/index.php
In the db/access.php file have something like
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
'local/webguides:view' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
),
),
);
You might also need 'riskbitmask' => RISK_XXX depending on if there are any risks in you code. Such as RISK_CONFIG, RISK_PERSONAL, etc.
In lang/en/local_webguides.php have something like
defined('MOODLE_INTERNAL') || die();
$string['pluginname'] = 'Webguides';
$string['webguides:view'] = 'Able to view webguids';
In version.php have something like
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2020051901; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2015051109; // Requires this Moodle version.
$plugin->component = 'local_webguides'; // Full name of the plugin (used for diagnostics).
Replace 2015051109 with the version of Moodle you are using - this will be in version.php in the root folder.
Then in your index.php file use this near the top.
require_capability('local/webguides:view', context_system::instance());
So only users with that capability will have access to the page.
EDIT:
You can add a link via settings.php using something like
defined('MOODLE_INTERNAL') || die;
if ($hassiteconfig) {
$page = new admin_externalpage(
'local_webguides',
get_string('pluginname', 'local_webguides'),
new moodle_url('/local/webguides/index.php'),
'local/webguides:view'
);
$ADMIN->add('localplugins', $page);
}
Then in your index page ad this
require_once($CFG->libdir.'/adminlib.php');
and remove require_login() and require_capability() and replace with
admin_externalpage_setup('local_webguides');
I have a ZendFW application and WPMU installed. Admins at Zend app has an interface where they can create a new MU site.
I included wp-load.php and then called wpmu_create_blog and so on...
Once I updated the WP to 3.9 I got error establishing database connection.
This test code works OK with 3.8 but gives db error when tried WP 3.9.
blog38 is WP 3.8
<?php
include "../blog38/wp-load.php";
global $wpdb;
echo "<pre>";
var_dump($wpdb->tables());
?>
blog39 is WP 3.9
<?php
include "../blog39/wp-load.php";
global $wpdb;
echo "<pre>";
var_dump($wpdb->tables());
?>
Does anyone know what the problem is? How to solve this connection error?
I posted it on WordPress discussion and submitted a ticket as well. The problem is in ms-setting.php file with new way they set $path and $current_site->path variables. In WP 3.8.3 they had
$current_site->path = $path = PATH_CURRENT_SITE;
and in WP 3.9 they set
$current_site->path = PATH_CURRENT_SITE;
and $path is determined by the $_SERVER['REQUEST_URI'] variable. So when you load wp-load.php file inside your application (and wordpress is in subdirectory) you have $path and $current_site->path variable different which ends up in no blog defined case, which gives Database connection error.
Current workaround is to override $_SERVER['REQUEST_URI'] = '/blog/'; before loading wp-load.php
More information can be found:
http://wordpress.org/support/topic/wordpress-39-multisite-db-connection-error
https://core.trac.wordpress.org/ticket/27999
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 ?
You need to include wp-load.php alone. It will load wp-include (and wpdb), plugins and your theme (functions.php). To use the database directly you would do this:
global $wpdb;
$wpdb->get_results('select ...');
If you need to access variable declared in functions.php, it's straight forward:
say.php
<?php
include 'var.php';
print_r( $moo );
?>
var.php
<?php
$moo= 'cow';
?>
output of say.php
$ php say.php
cow
Loading (include or require) functions.php after wp-load.php will result in redeclaration of the functions defined within functions.php (it will be loaded twice), causing PHP to crash.
Unless you are using wordpress features in your external query there is no need to include wordpress files.
Your external.php could look something like
function handy_prefix_my_processing_function()
{
//connect to database...
//Do something with data...
$result = $results
//return the result
return $result;
}
then in functions.php
function handy_prefix_get_my_variable()
{
//Load the external file and make its functions available
include_once( 'external.php' );
//Get my result
$result = handy_prefix_my_processing_function();
//Do something with the result....
}
I have a wordpress blog. I created a db table which stores dictionary information and I want to publish this data from a URL . (For ex: "myblogaddress.com/mytest.php")
I have been researching for 2 days but nothing works I tried.
In my page; I use the php code shown in blow.
<?php
global $wpdb;
$words = $wpdb->get_results("SELECT * FROM $wpdb->words")
echo $words[0]->ENG;
?>
I wonder that;
- Which directory does my php page to be into ?
- What I need to do (other config, permission etc.) to do what I want.
Regards.
If you're loading it from a standalone PHP file (ie not from within your WordPress theme), you'll have to call wp-load.php to initialise the WordPress variables (including $wpdb). Have a look at this answer, including the comment about only needing wp-load.php.
I'd consider using a relative path (what that would be would depend on where you put your page relative to WordPress) rather than using $_SERVER['DOCUMENT_ROOT'];, but that's just a personal preference.
EDIT
Rereading after seeing your comment, I've just realised $wpdb->words probably won't exist. Try
$words = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "words")
instead. That'll generate the table name correctly as wp_words. Of course, you'll need to populate it the same way.
hi i have a custom script that I call with ajax to retrieve some db info but for some reason it will not allow me to make the calls from this file. yet when i put the code in a page in the templates diretory lets say tpl_products_all_default.php they run fine. what do i need to do to be able to run queries from a custom script?
$sql = "select products_model from products where products_model = :productMdel:";
$sql = $db->bindVars($sql, ':productMdel:', 'C021', 'string');
$result = $db->Execute($sql);
if ($result->RecordCount() > 0) {
echo 'Model number = ' . $result->fields['products_model'];
} else {
echo 'Sorry, no record found for product number ' . $theProductId;
}
I may have an answer for you, though I'll admit it's not an optimal setup as it requires your custom file to be placed into the root directory.
If your custom file is being placed in the root (/your_custom_file.php) you can do the following to get access to the $db with the following require statement:
require('includes/application_top.php');
This will initialize all of the globals, and also call the includes/initsystem.php, which will spin through the values in the autoloader and include each script. The auto_loader can be viewed at includes/auto_loaders/config.core.php. In v1.5, you can see it finally includes the init_database.php script on lines 81-82. The init_database.php file finally initializes $db.
I initially ran into the same issue you had, and almost missed this setup because I had originally added my custom files to a custom directory like /my_module_extensions/my_file.php which failed. It seems the application_top.php loads everything with relative paths, so when executing under a directory other than the root, it would fail.
I hope this helps!
EDIT: Originally thought you were talking about an admin customization. I reworded this to relate to the public side. This also works from the admin side, if you need to extend the admin console.
Not true you can use an include from anywhere.
such as:
require_once('inc/php/application_top.php');
or
require_once('http://example.com/inc/php/application_top.php');