I stored several values in new fields of my plugin in the settings.php
However now I'm trying to put those values that I made onscreen, and I couldn't find a way to do that in moodle. Is there a way to do this in Moodle?
Any help is greatly appreciated. Thanks!
If you named the setting in settings.php something like 'PLUGINNAME/SETTINGNAME' (e.g. in the enrol_manual core plugin has a setting called 'enrol_manual/expiredaction'), then you can retrieve a single setting via:
$value = get_config('PLUGINNAME', 'SETTINGNAME');
$value = get_config('enrol_maual', 'expiredaction'); // For example.
If you want all the settings for a given plugin, then you can call:
$values = get_config('PLUGINNAME');
$values = get_config('enrol_manual'); // For example.
If, however, you've followed the bad practice of several of the settings for older core plugins, and the setting is called something like 'MYPLUGIN_SETTINGNAME', then you can retrieve the setting by calling:
$value = get_config('core', 'PLUGINNAME_SETTINGNAME');
$value = get_config('core', 'forum_displaymode'); // For example.
OR
global $CFG;
$value = $CFG->PLUGINNAME_SETTINGNAME;
$value = $CFG->forum_displaymode; // For example.
Naming settings without the '/' is bad, as it means the settings are loaded into the main $CFG global, which is already pretty bloated. Organising them into plugins also means all the plugin settings can be loaded as a simple object.
Related
I am developing on a Wordpress website with my own php coding. So far I am using the Snippets plugin which I like most for adding PHP code to existing wordpress sites.
The only thing I would like to know is how i can use something like global constants to avoid hard-coded values. Because I am using the same values again and again. What is the best way?
Thank you for any help.
best,
It is not recommended to create your own global variables on Wordpress (read this). But you you can achieve this by defining global variables.
function my_globals() {
global $myglobals;
// We define it as an array so you can store multiple values. If only needed one value you can directly set it
$myglobals = array();
$myglobals['first'] = 'This is first content';
$myglobals['second'] = 'This is second content';
}
add_action( 'after_setup_theme', 'my_globals' );
Now you can call your global using:
global $myglobals;
echo $myglobal['first'];
echo $myglobal['last'];
I am currently trying to add the username of the currently logged in FE user as a constant. I have tried the following but it does not seem to work.
Username = TSFE:fe_user|user|username
I am using this as a starting point
I would like to know how get the FE username into a constant so I can call it when ever I need for the rest of my typoscript.
This is the bit of typoscript that I am trying to get the username into, at the moment it just adds the company name which is defined as a constant else where, but I would like to add int he username also. I have tried adding the global variable for the username but no joy.
[usergroup = *]
config.tx_we_google_analytics {
_setCustomVar = 1, 'Client', '{$companyName} - {TSFE:fe_user|user|username}',2
}
[end]
Many Thanks in Advance
Bob
I wouldn't recommend using an extension to insert something as basic as a Google Analytics Snippet. Each extension slows down the system and makes it less maintainable.
I don't know what your customVar does, but you might try something like
temp.analytics = COA
temp.analytics {
10 = TEXT
10.value (
first part of GA snippet
)
20 = TEXT
20.data = TSFE:fe_user|user|username
30 = TEXT
30.value (
rest of GA Snippet
)
}
Then insert your snippet where you want, e.g. headerData.99 < temp.analyticsand you're done.
Or even try with a wrap and insertData=1.
As Artur says, if data doesn't render in .wrap, always try stdWrap.wrap - it tends to add functionality (although afaik there have been efforts to pass every wrap through stdWrap by default, but I don't think it's fully implemented).
You can use the appropriate global variable inside TypoScript setup directly, there is no need for a constant I think. The example you provided is the global variable I am talking about, which you can use in any getText context inside typoscript setup.
e.g.
temp.userName = TEXT
temp.userName.data = TSFE:fe_user|user|username
TypoScript constants allow only direct value assignment, it is not possible to assign any other "variable" to a constant IMHO.
Try this. If you are lucky it could work.
[usergroup = *]
config.tx_we_google_analytics {
_setCustomVar.cObject = TEXT
_setCustomVar.cObject.value = 1, 'Client', '{$companyName} - {TSFE:fe_user|user|username}',2
_setCustomVar.cObject.insertData = 1
}
[end]
If it doesn't, you will have to modify the plugin code to process the config value by stdWrap. Thsi code works from within the plugin controller
$configValue = $this->cObj->stdWrap('',$GLOBALS['TSFE']->tmpl->setup['config.']['tx_we_google_analytics.']['_setCustomVar.']);
or simpler (stil within the controller)
$configValue = $this->cObj->stdWrap('',$this->conf['_setCustomVar.']);
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 have been looking through code and forums for hours. Does any one know how to make it so that by default the Global features drop down is set to off on the options_name_manager file so that the global features are hidden. I have a customer who doesn't need to see those options and so I want to hide it.
I have done some extensive googling and come up blank. Any help is appreciated. I have even tried hard coding the variable which changes the drop down but doesn't hide the section that it needs to!!
I admin/options_name_manager.php on line 13 in 1.3.9 (right above where the $_GET['action'] is done) do this:
$_SESSION['option_names_values_copier'] = 0;
-- updated based on changed requirements
If you want to default it to off but retain the ability to change it, do something more like this:
if (!isset($_SESSION['option_names_values_copier'])) {
$_SESSION['option_names_values_copier'] = 0;
}
Then the setting will only be made if it isn't already set.
Ok, here's the deal: I am constructing a Drupal website that has several different sections. Each section is a view that displays a content type. (Each section has it's own content type) For example, I have a view that points to ?q=blog which displays content type blog.
All the sections look a little different than each other. Not like 'website-within-a-website' different but different enough that they can't all use the same template file and each be modified with CSS. Each section needs it's own page.tpl.php.
Unfortunately, AFAIK Drupal theme's .info files can only either assign one page.tpl.php for the entire theme or assign a page-node-####.tpl.php for each node. There is going to be lots of content on this website so setting Drupal to make a new identical page-node-####.tpl.php for every created node would get unmanagable very fast.
To solve this problem, I am going to use pathauto to create an alias for each content type. For example, all nodes of content type blog are given an alias ?q=blog/[post title]. Modify template.php to use page-blog.tpl.php for any page who's alias starts with the word 'blog'.
Other people have tried doing this sort of thing and have created functions such as the one described. Unfortunately, all the ones I have seen are for Drupal 6 or below. I have tried modifying existing ones with no success. So far, though, I think this is on the right track:
function basic_preprocess_page(&$vars, $hook) {
...
if( module_exists('path') ) {
$alias = drupal_get_path_alias( $_GET['q'] );
$site_section = "blog";
if( strpos( $alias, $site_section ) === 0 ) {
$VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE = "/path/to/page-blog.php";
}
}
}
I cannot find $VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE does anyone know what it is?
Maybe my site is structured badly. If anyone knows how to restructure my site so I can more easily make a theme with seperate sections please share how!
Thanks a million! (c:
EDIT: Perhaps I need to use template suggestions instead. Does anyone know the function or variable to use to set this?
They changed the name of this array key in D7 and I haven't seen it documented anywhere. I finally figured this out after a good bit of debugging. You can override the theme template in template.php with a hook_preprocess_page() like so:
function myTheme_preprocess_page(&$vars) {
global $node;
if ($node->type == 'blog') {
$vars['theme_hook_suggestions'] = array('my__blog_template'); // use my--blog-template.tpl.php, note '-' = '_'
}
elseif ($node->type == 'articles') {
$vars['theme_hook_suggestions'] = array('article__node_template'); // use article--node-template.tpl.php
}
}
Oh and don't forget to flush the Drupal caches after making changes to your template.php.
Ok, I found it:
http://drupal.org/node/223440#comment-991840
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$variables['template_files'][] = $template_filename;
}
}
Credit to this function goes to user mfb.
I had a lot of trouble with this so I will explain it here in case anyone finds it useful.
This function goes in your template.php. It needs to be part of the <theme name>_preprocess_page function. What it does is it takes the alias and then explodes it into a bunch of different components. For example if you are on a page with the alias ?q=blog/blog-post-title it would be exploded into blog and blog-post-title. It then turns each component into a name for a template suggestion. It puts each template suggestion into the template_files[] array (inside the $variables[] array) so that the page now has two new template suggestions:
page-blog, and page-blog-blog-post-title
Template suggestions are alternate template files. In this case they are for pages, but they don't necessarily have to be. You can have template suggestions for anything you can think of including blocks, nodes and the like. Don't let the name 'template suggestion' fool you. Template suggestions will be used over default templates as long as they exist. I don't know why it was named like that. I think it should be renamed.
What you do, then, now that you've set up Drupal to look for a template suggestion that points to your alias, is create a new template file where all the rest are in your theme. In this case, let's say I want to theme my entire blog section. In the templates folder I should create a file named page--blog.tpl.php (note the --double hyphens--) with the layout I want.
Drupal will use the most specific template suggestion it can find so if you wanted you could make one blog post to look completely different than the rest of the site long as you make a template for it named page--blog--blog-post-title and put it in your theme's templates directory. (again, note the double hyphens.)