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');
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've created a module for Joomla that fetches some data from a database and creates a table with it. I added JPagination to my module and I got the footer buttons to show and everything.
public function addPagination($params)
{
$count = $params->get("count");
$multiPage = $params->get("multiple_pages");
//Add controls for changing pages
if($multiPage)
{
jimport('joomla.html.pagination');
$limitStart = 0;
$pagination = new JPagination(count($this->vacanciesRows) , $limitStart, $count);
echo $pagination->getListFooter();
}
}
but when I click some of the pages (all except the first one) I'm getting error 404. I'm sure I've missed something but I have very little to none experience with Joomla. I'll include pastebins with my helper.php and my mod_xxx_xxx.php
A module can't have a pagination. It has no own URL. Only components have that. If you check the links your module creates, you'll notice that they are invalid. You can try to do Ajax magic but then you need a component providing the data.
In Joomla only components can react to incoming URLs directly.
I'm trying to setup workflow in vtiger that export invoice to pdf once the invoice has certain status.
To do it, I was thinking of using the "Invoke Custom Function". There is a documentation on it, but it is not clear, like: where/which file to register the event manager??
I also found that the same questions was actually asked and solved by another person, but when i opened the link in the post, it direct me to "Page not found" error.
require_once 'include/utils/utils.php';
require 'modules/com_vtiger_workflow/VTEntityMethodManager.inc';
$emm = new VTEntityMethodManager($adb);
//$emm->addEntityMethod("Module Name","Label", "Path to file" , "Method Name" );
$emm->addEntityMethod("Invoice", "Update Inventory", "include/InventoryHandler.php", "handleInventoryProductRel");
Add these lines of code in a php file replace with your module name, label,path,method name and add the file to vtiger instance, run the file. It will be added in database table "com_vtiger_workflowtasks_entitymethod". You can use now your invoke custom function in workflow section for the particular module.
done all according to Answer before
<?php
require_once 'include/utils/utils.php';
require 'modules/com_vtiger_workflow/VTEntityMethodManager.inc';
$emm = new VTEntityMethodManager($adb);
#addEntityMethod("Module", "LABEL", "FILENAME", "FUNCTION_NAME");
$emm->addEntityMethod("Accounts", "Update Account","modules/Accounts/updateAccountData.php", "updateAccountData");
?>
2- This file was executed by http://IP/registerworkflow.php
and i can see Update Account in custom Script
3- create simple function
<?php
function updateAccountData($entity){
$file = '/tmp/people.txt';
$current = file_get_contents($file);
$current .= "John Smith\n";
file_put_contents($file, __LINE__.':'.print_r($current, true)."\r\n");
}
?>
4-file /tmp/people.txt was created by me and 777 chmod
5- email send was added to test workflow is executed
(so first step execute Update Account function second email )
But strange is that i can reseive imail but script not put data to a test file
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.
I was going to update the ecommerce plugin (Shopp) on my wordpress site and it asked me to deactivate it. once I did that I lost the entire site.
I am trying to activate the plugin through the php files, but not sure what I am doing and would like some help.
Does anyone know how I can activate the Shopp plugin (or any plugin for that matter) on my site through the php files?
This is the code I'm using to get the string:
$unserialized = unserialize('a:14:{i:0;s:19:"akismet/akismet.php";i:1;s:37:"breadcrumbs-plus/breadcrumbs-plus.php";i:2;s:35:"googleanalytics/googleanalytics.php";i:3;s:45:"grunion-contact-form/grunion-contact-form.php";i:4;s:43:"image-caption-links/image-caption-links.php";i:5;s:29:"image-widget/image-widget.php";i:6;s:13:"rate/rate.php";i:7;s:33:"restore-jquery/restore-jquery.php";i:8;s:41:"shopp-cache-helper/shopp-cache-helper.php";i:9;s:47:"shopp-default-breadcrumb-extender-sdbe/sdbe.php";i:10;s:33:"shopp-improved/shopp-improved.php";i:11;s:19:"shuffle/shuffle.php";i:12;s:19:"vslider/vslider.php";i:13;s:41:"wordpress-importer/wordpress-importer.php";}');
array_push($unserialized, 'shopp/shopp.php');
$serialized = serialize($unserialize);
echo $serialized;
The active plugins are not stored in a PHP file. It's stored in the database. Open the wp_options table in the database. Look for a row in which the value of the option_name field is active_plugins. In this row, look for the value of option_value. You'll see a serialized string containing the information of the active plugins.
Now, it might be a little bit confusing to edit the string straight away especially if you're not familiar how serialized strings are formatted. So, I suggest you copy the string and use PHP unserialize() function on it, which will then return an array. After that, use array_push() to add another element in which the value is the path to the plugins file (e.g. "akismet/akismet.php", in your case it might be "shopp/shopp.php"). Once you've add another element, use serialize() and copy the returned string and replace the old serialized string in the database.
$unserialized = unserialize('...');
array_push($unserialized, 'shopp/shopp.php');
$serialized = serialize($unserialized);
echo $serialized; // Copy this output back into the database
There are details on this site about how to programmatically activate and deactivate a plugin. Here is a snippet:
function toggle_plugin() {
// Full path to WordPress from the root
$wordpress_path = '/full/path/to/wordpress/';
// Absolute path to plugins dir
$plugin_path = $wordpress_path.'wp-content/plugins/';
// Absolute path to your specific plugin
$my_plugin = $plugin_path.'my_plugin/my_plugin.php';
// Check to see if plugin is already active
if(is_plugin_active($my_plugin)) {
// Deactivate plugin
// Note that deactivate_plugins() will also take an
// array of plugin paths as a parameter instead of
// just a single string.
deactivate_plugins($my_plugin);
}
else {
// Activate plugin
activate_plugin($my_plugin);
}
}
For everyone who has a plugin acting weird
The easiest way to regain access to your site when being locked out after deactivating, activating, installing, updating a plugin is:
Go to your webhost adminpanel (Cpanel, DirectAdmin)
Go to Files (Filemanager)
Go to //wp-content/ and rename your "plugins" folder to something else for instance "plugins_off"
Go to your WP-admin. You will have access again but no plugins visible.
Go back to your webhost adminpanel and rename "plugins_off" back to "plugins".
Now your plugins will all be listed again in WP-admin, but all deactivated.
Take it from there.
There is no need to add PHP code.
Like RST wrote: No need for coding to restore site that crashed after you deactivate a plugin. There's a good reason the site crashed because other plugins depended on that deactivated plugin (like WooCommerce).
The easiest way to restore the site and gain access to WP admin is simply to rename the plugins folder, refresh the page, go back and rename the folder back to "plugins" then hit refresh again. All plugins will be back as before only they are all deactivated. Now all you need to do is to activate them again. Do it one by one to be sure nothing else crashes.
Here is working code
(just uncomment "activate" line):
function MY_toggle_plugins() {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$temp_files1 = glob(WP_PLUGIN_DIR.'/*');
$activated=array();
$already_active=array();
foreach($temp_files1 as $file1){
if(is_dir($file1)) {
$temp_files2 = glob($file1 . '/*');
foreach($temp_files2 as $file2){
if(is_file($file2) && stripos(file_get_contents($file2),'Plugin Name:')!==false) {
$plugin_name_full=basename(dirname($file2)).'/'.basename($file2);
if(is_plugin_active($plugin_name_full)) {
array_push($already_active, $plugin_name_full);
//deactivate_plugins($plugin_name_full);
}
else{
array_push($activated, $plugin_name_full);
//activate_plugin($plugin_name_full);
}
}
}
}
}
echo 'You have activated these plugins:<br/><br/>'.serialize($activated).'<br/><br/>These were already active:<br/><br/>'.serialize($already_active); exit;
}
//execute
MY_toggle_plugins();